数据库(学习整理)----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 ...
随机推荐
- devi into python 笔记(六)正则表达式 原始字符串
字符串函数replace: #string.replace: #字符串的replace方法:替换子串,不改变原来的字符串 s = "broad road" #打印出来会发现不单单是 ...
- IIS 7.0, ASP.NET, pipelines, modules, handlers, and preconditions
1.0 What is the IIS Pipeline Conceptually, the IIS pipeline is a state machine with the following st ...
- sql 将Null 值转化成空字符串
当Null + 任何字符串时,都等于Null. 因些用函数IsNull(字段名,''),如果字段名中的值是Null时,那么这个字段名的值是''. 例如::select code + IsNull('- ...
- 细说webpack之流程篇
引言 目前,几乎所有业务的开发构建都会用到 webpack .的确,作为模块加载和打包神器,只需配置几个文件,加载各种 loader 就可以享受无痛流程化开发.但对于 webpack 这样一个复杂度较 ...
- 在 slua 中使用更新的面向对象方案
上一篇记录了我使用 Slua.Class 来实现面向对象扩展 C# 中得类,但实际使用中,更多地情况是直接在 lua 中定义基类然后扩展,于是触发了我重新思考下是否两种形式应该统一用一种,目前的方案中 ...
- 重新安装phpMyAdmin无法运行的解决一例
重新下载phpMyAdmin,并解压覆盖老的版本. 浏览器打开显示 http 500 查看服务器日志显示主要如下: PHP Fatal error: PMA\\libraries\\ThemeMana ...
- 【解决】Internet访问看似正常(无叹号受限)却打不开网页
嘛╮(╯▽╰)╭ 可能是前几天中了一等奖败了人品 .. 今天果断受点小挫折 事情是这样的:昨晚电脑在不插电的情况下打了一小时“剑灵”,有点烫,电量剩30%,关机睡觉,今早发现上不去网页了! 桌面右下角 ...
- win8下在microsoft visual studio 2012利用ODP.NET连接ORACLE 12c
老板要求我搭个ASP.NET框架,并且连接上ORACLE数据库,听起来好像挺简单的,但就是连第一步连接ORACLE我都搞了两天╮(╯▽╰)╭ 首先,项目书上要求用ORACLE 10G,可我自己的本本装 ...
- Using Java SecurityManager to grant/deny access to system functions
In Java it is possible to restrict access to specific functions like reading/writing files and syste ...
- SSH开源框架考试题
一.选择题 1.不属于Action接口中定义的字符串常量的是____B___. A.SUCCESS B.FAILURE C.ERROR ...