数据库(学习整理)----3--Oracle创建表和设置约束
- BBS论坛表设计
- 包含的表:BBSusers(用户表),BBSsection(版块表),BBStopic(主贴表),BBSreply(跟帖表)
- 表结构
1)BBSusers
|
字段名 |
字段说明 |
数据类型 |
字段约束 |
备注 |
|
U_ID |
用户ID |
number |
主键,非空 |
非空 |
|
UName |
用户名 |
Varchar2(32) |
非空 |
|
|
UPassword |
密码 |
Varchar2(16) |
默认6个8 |
|
|
UEmail |
|
Varchar2(32) |
默认p@p.com,必须有“@”和“.”,“@”必须在“.”前面 |
|
|
UBirthday |
生日 |
Date |
非空 |
|
|
USex |
性别 |
Char(2) |
默认’男’ |
非空 |
|
UClass |
用户等级 |
Number |
值只能是1,2,3中任意一个,默认1 |
|
|
UStatement |
备注 |
Varchar2(255) |
||
|
URegDate |
注册时间 |
Date |
非空 |
|
|
UState |
用户状态 |
Number |
值只能是1,2,3,4中任意一个 |
非空 |
|
UPoint |
积分 |
Number |
不能为负。默认20 |
2)BBSsection
|
字段名 |
字段说明 |
数据类型 |
字段约束 |
备注 |
|
S_ID |
版块ID |
Number |
主键,非空 |
非空 |
|
SName |
版块名称 |
Varchar2(32) |
非空 |
|
|
SMasterID |
用户ID |
Number |
外键,关联用户表主键 |
非空 |
|
SStatement |
备注 |
Varchar2(255) |
||
|
SClickCount |
点击次数 |
Number |
不能为负 |
非空 |
|
STopicCount |
主贴数目 |
Number |
不能为负。默认为0 |
3)BBStopic
|
字段名 |
字段说明 |
数据类型 |
字段约束 |
备注 |
|
TID |
主贴ID |
Number |
主键,非空 |
非空 |
|
TNumber |
主贴标号 |
Varchar2(32) |
必须以”t”开头的8个字符 |
非空 |
|
TSID |
版块ID |
Number |
外键,关联版块ID |
非空 |
|
TUID |
用户ID |
Number |
外键,关联用户ID |
非空 |
|
TTopic |
主贴标题 |
Varchar2(255) |
非空 |
|
|
TContents |
主贴正文 |
Varchar2(2000) |
非空 |
|
|
TTime |
发帖时间 |
Date |
非空 |
|
|
TClickCount |
点击数量 |
Number |
不能为负,默认0 |
|
|
TFlag |
主贴状态 |
Number |
只能为1,2,3 |
非空 |
|
TLastCLickT |
最后点击时间 |
Date |
非空 |
4)BBSreply
|
字段名 |
字段说明 |
数据类型 |
字段约束 |
备注 |
|
RID |
跟帖表主键 |
Number |
主键,非空 |
非空 |
|
RNumber |
跟贴标号 |
Varchar2(32) |
必须以”r”开头的8个字符 |
非空 |
|
RTID |
主贴ID |
Number |
外键,关联主贴ID |
非空 |
|
RSID |
版块ID |
Number |
外键,关联版块ID |
非空 |
|
RUID |
用户ID |
Number |
外键,关联用户ID |
非空 |
|
RTopic |
跟帖标题 |
Varchar2(255) |
非空 |
|
|
RContents |
跟帖正文 |
Varchar2(2000) |
非空 |
|
|
RTime |
发帖时间 |
Date |
非空 |
|
|
RClickCount |
点击数量 |
Number |
不能为负,默认0 |
2、Oracle建表和设置约束:
-----------------------------【1】----------------------------------------------
------创建:用户表
create table BBSusers
(
U_ID number, -----用户ID
UName Varchar2(32), -----用户名
UPassword Varchar2(16),-----密码
UEmail Varchar2(32), -----email
UBirthday Date, -----生日
USex Char(2), -----性别
UClass Number, -----用户等级
UStatement Varchar2(255),---备注
URegDate Date, -----注册时间
UState Number, -----用户状态
UPoint Number -----积分
);
------创建:表约束
----(1)主键,非空
alter table BBSusers
add constraint PK_BBSusers_uid primary key(U_ID);
alter table BBSusers
modify(U_ID number not null); ---用户名 :非空
alter table BBSusers
modify(UName number not null); ---密码:默认6个8
alter table BBSusers
modify(UPassword Varchar2(16) default '888888'); ---email:默认p@p.com,必须有“@”和“.”,“@”必须在“.”前面
alter table BBSusers
add constraint CK_BBSusers_UEmail check(UEmail like '%@%.%');
alter table BBSusers
modify(UEmail Varchar2(32) default 'p@p.com'); ---生日:非空
alter table BBSusers
modify(UBirthday number not null); ---性别:默认’男’ 非空
alter table BBSusers
modify(USex Char(2) default '男' not null); ---用户等级:值只能是1,2,3中任意一个,默认1
alter table BBSusers
modify(UClass number default 1 check(UClass in(1,2,3)));
---注册时间:非空
alter table BBSusers
modify(URegDate Date not null);
---用户状态:值只能是1,2,3,4中任意一个 非空
alter table BBSusers
modify(UState number check(UState in(1,2,3,4)) not null);
---积分:不能为负。默认20
alter table BBSusers
modify(UPoint number default 20 check(UPoint>0)); ---------------------------------------------------------------------------
-----------------------------【2】----------------------------------------------
-----创建:版块表
create table BBSsection
(
S_ID number, -----版块ID(主键)
SName Varchar2(32), -----版块名称
SMasterID Number, -----用户ID(外键)
SStatement Varchar2(255),-----备注
SClickCount Number, -----点击次数
STopicCount Number -----主贴数目
); -----设置:约束 -----(1):版块ID 主键,非空
alter table BBSsection
add constraint PK_BBSsection_sid primary key(S_ID);
alter table BBSsection
modify(S_ID number not null); -----版块名称:非空
alter table BBSsection
modify(SName number not null); -----用户ID:外键,关联用户表主键 非空
alter table BBSsection
add constraint FK_BBSsection_SMasterID foreign key(SMasterID) references BBSusers(U_ID);
alter table BBSsection
modify(SMasterID number not null); -----点击次数:不能为负 非空
alter table BBSsection
modify(SClickCount check(SClickCount>0) not null); ----主贴数目:不能为负。默认为0
alter table BBSsection
modify(STopicCount default 0 check(STopicCount>0));
---------------------------------------------------------------------------
-----------------------------【3】----------------------------------------------
-----创建:主贴表
create table BBStopic
(
TID number, -----主贴ID(主键)
TNumber Varchar2(32), -----主贴标号
TSID Number, -----版块ID(外键)
TUID Number, -----用户ID(外键)
TTopic Varchar2(255), -----主贴标题
TContents Varchar2(2000),-----主贴正文
TTime Date, -----发帖时间
TClickCount Number, -----点击数量
TFlag Number, -----主贴状态
TLastCLickT Date -----最后点击时间
); ------设置:约束
-----(1)主贴ID:主键,非空
alter table BBStopic
add constraint PK_BBStopic_sid primary key(TID);
alter table BBStopic
modify(TID number not null); -----主贴标号:必须以”t”开头的8个字符 非空
alter table BBStopic
add constraint CK_BBStopic_TNumber check(TNumber like 't%' and length(TNumber)=8);
alter table BBStopic
modify(TNumber not null);
-----版块ID:外键,关联版块ID 非空
alter table BBStopic
add constraint FK_BBStopic_TSID foreign key(TSID) references BBSsection(S_ID);
alter table BBStopic
modify(TSID number not null); -----用户ID:外键,关联用户ID 非空
alter table BBStopic
add constraint FK_BBStopic_TUID foreign key(TUID) references BBSusers(U_ID);
alter table BBStopic
modify(TUID number not null);
-----主贴标题:非空
alter table BBStopic
modify(TTopic Varchar2(255) not null);
-----主贴正文:非空
alter table BBStopic
modify(TContents Varchar2(2000) not null);
-----发帖时间:非空
alter table BBStopic
modify(TTime Date not null); -----点击数量:不能为负,默认0
alter table BBStopic
modify(TClickCount number default 0 check(TClickCount>0) ); ------主贴状态:只能为1,2,3 非空
alter table BBStopic
modify(TFlag number check(TFlag in(1,2,3)) not null ); -----最后点击时间:非空
alter table BBStopic
modify(TLastCLickT Date not null); ---------------------------------------------------------------------------
-----------------------------【4】----------------------------------------------
-----创建:跟帖表
create table BBSreply
(
RID number, -----跟帖表主键(主键)
RNumber Varchar2(32), -----跟贴标号
RTID Number, -----主贴ID(外键)
RSID Number, -----版块ID(外键)
RUID Number, -----用户ID(外键)
RTopic Varchar2(255), -----跟帖标题
RContents Varchar2(2000),-----跟帖正文
RTime Date, -----发帖时间
RClickCount Number -----点击数量
); ------设置:约束
-----(1)跟帖表主键:跟帖表主键
alter table BBSreply
add constraint PK_BBSreply_RID primary key(RID);
alter table BBSreply
modify(RID number not null); -----跟贴标号:必须以”r”开头的8个字符 非空
alter table BBSreply
add constraint CK_BBSreply_RNumber check(RNumber like 'r%' and length(RNumber)=8);
alter table BBSreply
modify(RNumber not null); -----主贴ID:外键,关联主贴ID 非空
alter table BBSreply
add constraint FK_BBSreply_RTID foreign key(RTID) references BBStopic(TID);
alter table BBSreply
modify(RTID number not null); -----版块ID:外键,关联版块ID 非空
alter table BBSreply
add constraint FK_BBSreply_RSID foreign key(RSID) references BBSsection(S_ID);
alter table BBSreply
modify(RSID number not null);
-----用户ID:外键,关联用户ID 非空
alter table BBSreply
add constraint FK_BBSreply_RUID foreign key(RUID) references BBSusers(U_ID);
alter table BBSreply
modify(RUID number not null); -----跟帖标题:非空
alter table BBSreply
modify(RTopic number not null);
-----跟帖正文:非空
alter table BBSreply
modify(RContents number not null);
-----发帖时间:非空
alter table BBSreply
modify(RTime number not null);
-----点击数量:不能为负,默认0
alter table BBSreply
modify(RClickCount number default 0 check(RClickCount>0) );
数据库(学习整理)----3--Oracle创建表和设置约束的更多相关文章
- Oracle 创建表并设置主键自增
创建数据库 CREATE TABLE STUDENT(ID NUMBER PRIMARY KEY, NAME VARCHAR(200) NOT NULL, SEX VARCHAR(200), CREA ...
- oracle 创建表 外键约束
create table usertable( id int primary key, username ) not null, birthday date, sex ), address ) ); ...
- Oracle学习笔记_05_ 一个创建表空间、创建用户、授权的完整过程
一.完整命令 su - oracle sqlplus /nolog conn /as sysdba create tablespace scaninvoice logging datafile '/u ...
- Oracle创建表空间、用户管理、角色管理
内容:Oracle创建表空间.用户管理.角色管理 1.用系统用户登录Oracle 默认的系统用户: sys/system.sysman.scott sys:权限最大,超级用户,可以完成所有任务, 默认 ...
- Oracle创建表空间和表
创建表空间和表ORACLE物理上是由磁盘上的以下几种文件:数据文件和控制文件和LOGFILE构成的oracle中的表就是一张存储数据的表.表空间是逻辑上的划分.方便管理的.数据表空间 (Tablesp ...
- Oracle创建表语句(Create table)语法详解及示例、、 C# 调用Oracle 存储过程返回数据集 实例
Oracle创建表语句(Create table)语法详解及示例 2010-06-28 13:59:13| 分类: Oracle PL/SQL|字号 订阅 创建表(Create table)语法详解 ...
- oracle创建表之前判断表是否存在,如果存在则删除已有表
Mysql 创建表之前判断表是否存在,如果存在则删除已有表 DROP TABLE IF EXISTS sys_area; CREATE TABLE sys_area ( id int NOT NULL ...
- Oracle 创建表 Create Table...
一.创建表 主键约束primary key 约束条件,唯一且非空,一个表中只能有一个主键:有多个字段联合作为主键时,合在一起唯一标识记录,叫做联合主键. 外键约束 foreign key 受另外一张表 ...
- 使用navicat操作PostPreSql创建表并设置主键自增和触发器
使用navicat操作PostPreSql创建表并设置主键自增和触发器 1).创建递增序列 2).创建表,使用序列,设置主键递增 3)定义触发函数 自动生成时间戳函数 CREATE OR REPLAC ...
随机推荐
- selenium webdriver(2)---页面对象定位
webdriver的元素定位很灵活,提供了多种定位方式: Id LinkText PartialLinkText Name TagName Xpath ClassName CssSelector 这些 ...
- ARM学习笔记2——分支跳转指令
一.Arm指令条件码和条件助记符 二.跳转指令B 1.作用 跳转指令B使程序跳转到指定的地址执行程序(跳转范围是PC-32MB到PC+32MB) 2.指令格式(注:B后面如果有条件,条件就是紧跟在B后 ...
- SLua 中继承 C# 类接口 Slua.Class 的一个 Bug。
由于目前要把大量的代码移植到 lua 中(真是够虐心的),面向对象肯定少不了,项目的代码都是这么设计的,于是就测试 Slua.Class 接口来扩展 C# 的类,发现有点问题,给作者提交了一个 Iss ...
- webstorage调查资料汇总
在调查webstorage的过程中,一步一步了解了各种缓存或存储机制,local storage本地存储,application cache离线应用存储,http cache是http本身自带的缓存机 ...
- ACM1229_还是A+B(求A的第K位的数公式:A%((int)(pow(10,K)))
#include<stdio.h> #include<math.h> int main() { int A,k,B,sum,c,d; while(scanf("%d% ...
- 一段网上java常见escape和unescape方法的BUG
escape编码和unescape编码,就是将一个字符转换为16进制unicode编码,前面加%字符进行标识. 此处不再多做解释,参考这里:http://www.jb51.net/article/23 ...
- Caffe 编译
Compilation Now that you have the prerequisites, edit your Makefile.config to change the paths for y ...
- hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- JAVA 调用Axis2 code generator 生成的webservice
以下代码为调用 JAVA 调用Axis2 code generator 生成的webservice的代码. package test; import java.rmi.RemoteException; ...
- Centos安装成功mysql-5.5.37
摘自http://www.2cto.com/os/201404/296364.html(原文请关注) 这几天在centos下装mysql,这里记录一下安装的过程,方便以后查阅 Mysql5.5.37安 ...