【oracle资料整理】--【8】约束
来源:互联网 发布:mp5播放器软件下载 编辑:程序博客网 时间:2024/05/17 22:20
约束
主键约束 -- 每个表要有主键,唯一的标识一行数据
非空约束
唯一性约束
外键约束
检查约束
脚本(SCRIPT)
create table cla( --班级表
id number(2) primary key, --班级编号
cname varchar2(20) not null --班级名字
);
create table stu( --学生表
xh number(4) primary key, --学号是主键
xm varchar2(20) not null, --姓名非空
age number(2) check (age between 10 and 90),--年龄在10到90之间(10<= age <=90 )
birthday date,
shenfenzheng number(18) unique, --身份证唯一
classid number(2) references cla(id) -- 班级编号外键
--(引用的一定是主键或唯一性约束的字段)
);
<1>建立表的同时使用约束
create table student( --学生表
xh number(4) primary key, --学号主键
xm varchar2(10) not null, --姓名不能为空
sex char(2) check (sex in ('男','女')), --性别
birthday date unique, --日期
sal number(7,2) check (sal between 500 and 1000),--奖学金 sal >=500 and sal <=1000
classid number(2) references cla(id)
); --必须要先有cla表才对
--一定先建立班级cla表
主键约束 primary key
not null
check
unique 唯一约束
create table student( --学生表
xh number(4) constraint pk_stu primary key, --学号主键
xm varchar2(10) constraint nn_stu not null, --姓名不能为空
sex char(2) constraint ck_stu_sex check (sex in ('男','女')), --性别
birthday date constraint uq_bir unique, --日期
sal number(7,2) constraint ck_sal check (sal between 500 and 1000)--奖学金 sal >=500 and sal <=1000
);
<2>建立约束的同时给约束指定名字,便于删除
create table cla( --班级表
id number(2) constraint pk_cla primary key, --班级编号
cname varchar2(20) constraint nn_cla not null --班级名字
);
create table stu( --学生表
xh number(4) constraint pk_stu primary key, --学号是主键
xm varchar2(20) constraint nn_stu not null, --姓名非空
age number(2) constraint ck_stu check (age between 10 and 90),--年龄在10到90之间(10<= age <=90 )
birthday date,
shenfenzheng number(18) constraint uq_stu unique, --身份证唯一
classid number(2) constraint fk_stu references cla(id) -- 班级编号外键
--(引用的一定是另外表的主键或唯一性约束的字段)
);
<3>建完表后加约束
学生表student
create table student( --学生表
xh number(4), --学号
xm varchar2(10), --姓名
sex char(2), --性别
birthday date, --日期
sal number(7,2) --奖学金
);
加约束
加主键
alter table student add constraint pk_stu
primary key (xh);
加非空
alter table student modify (xm not null);
检查约束
alter table student add check(sex in ('男','女'));
alter table student add constraint ck_sal check(sal between 500 and 1000));
给student加班级字段
alter table student add (classid number(2));
班级表cla
create table cla( --班级表
id number(2), --班级编号
cname varchar2(20) --班级名字
);
添加 主键
alter table cla add constraint pk_cla
primary key (id);
加 not null
alter table cla modify
(cname not null);
学生表student
create table student( --学生表
xh number(4) ,
xm varchar2(20) , --姓名非空
age number(2),--年龄在10到90之间(10<= age <=90 )
birthday date,
shenfenzheng number(18), --身份证唯一
classid number(2) -- 班级编号外键
--(引用的一定是另外表的主键或唯一性约束的字段)
);
加外键约束
alter table student add constraint fk_stu
foreign key (classid) references cla(id);
加主键
alter table student add constraint pk_stu
primary key (xh);
加not null
alter table student modify(xm not null);
加检查约束
alter table student add constraint cc_age
check (age >= 10 and age <=90);
加唯一约束
alter table student add constraint
uq_sfz unique(shenfenzheng);
加外键约束
alter table student add constraint
fk_stu foreign key (classid)
references cla(id);
如何删除约束
alter table student drop constraint
fk_stu;
可以用一个统一的格式来删除
alter table 表名 drop constraint 约束名
<4>如何查看约束?? 约束一定加在表上
一个表上到底有哪些约束???
select constraint_name,constraint_type
from user_constraints
where table_name = 'STUDENT'
--查看表上有什么约束
select * from user_constraints;
--查看约束作用在什么字段上
select * from user_cons_columns
where CONSTRAINT_NAME='PK_STU';
user_constraints数据字典表
<5>约束是如何起作用的??
create table cla( --班级表
id number(2) constraint pk_cla primary key, --班级编号
cname varchar2(20) constraint nn_cla not null --班级名字
);
create table stu( --学生表
xh number(4) constraint pk_stu primary key, --学号是主键
xm varchar2(20) constraint nn_stu not null, --姓名非空
age number(2) constraint ck_stu check (age between 10 and 90),--年龄在10到90之间(10<= age <=90 )
birthday date,
shenfenzheng number(18) constraint uq_stu unique, --身份证唯一
classid number(2) constraint fk_stu references cla(id) -- 班级编号外键
--(引用的一定是另外表的主键或唯一性约束unique的字段)
);
主键 = 非空 + 唯一
非空
唯一 = 有值的话 值要不同
null的话 都是可以的
外键 = 有值 一定要在被引用的表的数据中
null的话 是可以的
- 【oracle资料整理】--【8】约束
- Oracle资料整理
- Oracle资料整理
- 【oracle资料整理】--【12】索引
- 【oracle资料整理】--【14】游标
- 【oracle资料整理】--【16】触发器
- Informix数据表结构分析资料整理之约束查询代码
- Informix数据表结构分析资料整理之约束查询代码
- Informix数据表结构分析资料整理之约束查询代码
- JDBC连接oracle数据库(资料整理)
- 【oracle资料整理】--【2】如何建表
- 【oracle资料整理】--【4】基本查询
- 【oracle资料整理】--【6】分组函数
- 【oracle资料整理】--【10】视图 (view)
- 【oracle资料整理】--【11】同义词synonym
- 【oracle资料整理】--【13】PL/SQL块
- 收集整理Oracle权限的资料
- oracle嵌套表--整理的学习资料
- 【oracle资料整理】--【6】分组函数
- ASP.NET Cookies
- 【oracle资料整理】--【7】子查询,层次查询,TOP 前几行,分页查询
- Groovy探索之闭包 一
- Protel 99se快捷键大全
- 【oracle资料整理】--【8】约束
- 醒世格言
- 【oracle资料整理】--【9】序列sequence --- 自动编号 ---- IDENTITY (SQLSERVER)
- [学习笔记]小型论坛功能——连接数据库 [1]
- 【oracle资料整理】--【10】视图 (view)
- jvm装入原理以及其启动参数
- SQL 行转列一题
- 为非Administrators组的用户设置IIS管理权限
- c#读写app.cofig文件