数据库(学习整理)----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 ...
随机推荐
- Git报错:insufficient permission for adding an object to repository database .git/objects
在本地搭建Git服务器后,在开发机上push新代码,发现Git提示: insufficient permission for adding an object to repository databa ...
- HDOJ/HDU 1161 Eddy's mistakes(大写字母转换成小写字母)
Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for e ...
- Problem 2214 Knapsack problem 福建第六届省赛
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2214 题目大意:给你T组数据,每组有n个物品,一个背包容量B,每件有体积和价值.问你这个背包容纳的物品最大价值 ...
- vijosP1687 细菌总数
vijosP1687 细菌总数 链接:https://vijos.org/p/1687 [思路] 错排公式+高精度. 题目要求排列数目而且不能有Pi==i的情况出现,可以看出这正是1,2,3,4,5, ...
- ios 中介者模式
中介设计模式在ios中普片应用于视图迁移 1,从xib中生成object对象,中介类为n个不同对象 @property(nonatomic,retain)IBOutlet NSObject *ob; ...
- PyDev+eclipse的编码问题
1.在代码的开始声明编码为utf-8
- iOS OC开发代码规范
1.变量.类名.函数名 使用驼峰命名法 2.尽量使用完整的单词命名,尽量不采用 缩写单词 3.类名使用大写字母打头,前缀统一加上HH 例如:HHHomePageController 4.类的成员变量使 ...
- (求助大牛)关于vs2010上的AVS代码bug问题~~
问题1:就是解码端,出现错误,找到bug所在地了,见下图: memcpy出错了,跳到下图了.可是错误显示的我不懂,求解释一下就ok了,小女子在此谢过了~~哎,调bug的能力弱爆了!! 大家看看吧~~是 ...
- [Redux] Colocating Selectors with Reducers
We will learn how to encapsulate the knowledge about the state shape in the reducer files, so that t ...
- visualvm
http://visualvm.java.net/features.html http://visualvm.java.net/zh_CN/gettingstarted.html