数据库(学习整理)----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 ...
随机推荐
- 修改Oracle数据库用户的密码
修改数据库用户system密码的两个方法: 方法一: alter user system identified by password; 方法二: password system;
- 使用Notify 和 wait ,使用Linklist实现生产者消费者问题
ref:http://www.cnblogs.com/happyPawpaw/archive/2013/01/18/2865957.html 注释很清楚的, import java.util.Link ...
- 《锋利的Jquery第二版》读书笔记 第三章
DOM操作的分类 1.DOM Core不专属JavaScript,任何一种支持DOM的程序设计语言都可以使用它,也可以处理XML等标记语言编写出来的文档,getElementById().setAtt ...
- 【ImageMagick】ImageMagick命令行工具
[关于ImageMagick] [命令行工具] [源码安装] [二进位发布版本] [资源配置文件] [相关下载] ImageMagick命令行工具 [ convert | identify | mog ...
- Java从服务器上获取时间,动态在jsp页面显示
Java获取服务器时间,动态显示到jsp页面,大家都是到Java只能获取一次,到页面的时间是静态的,不过通过js和Java的合作,巧妙地实现此功能 本人是给电视做系统,客户要求页面能显示时间,因为电视 ...
- 读书雷达 l 业务分析师(BA)篇
http://chuansong.me/n/412991951441 ThoughtWorks BA社区从2011年起就建立了BA书橱,根据大家的推荐选择了来自软件需求.商业分析.设计思维.软技能,以 ...
- UIwebView实现html的离线缓存
1.html的缓存主要採取ASIHTTPRequest的缓存策略 (1).设置缓存策略 //设置缓存 ASIDownloadCache *cache=[[ASIDownloadCache alloc] ...
- [TypeScript] Configuring a New TypeScript Project
This lesson walks you through creating your first .tsconfig configuration file which will tell the T ...
- Entity FrameWork 指导文章
Entity FrameWork学习指导: 转:http://www.entityframeworktutorial.net/code-first/configure-many-to-many-rel ...
- pcap的pcap_dump()保存的文件格式
(2009-09-01 20:36:49) 转载▼ 标签: 杂谈 分类: 专业 首先是tcpdump文件格式 当你在Windows或者Linux环境下用tcpdump命令抓取数据包时,你将得到如下格式 ...