oracle 约束

一. 约束

  1. 检查约束 check

特殊检查约束 not null

  1. 唯一约束 unique
  2. 主键约束 primary key
  3. 外键约束 foreign key(副表名)  references 主表名(列名)

 

注意:约束的4个问题

  1. 子(从)表 [tb_student] 外键列 [clazz_id] 的值必须在父(主)表 [tb_clazz] 参照列 [id] 值的范围内,或者为空(也可以加非空约束,强制不允许为空)。
  2. 外键 [clazz_id] 参照的只能是主表 [tb_clazz] 主键或者唯一键,保证子表记录可以准确定位到被参照的记录。
  3. 当主表 [tb_clazz] 的记录被子表 [tb_student] 参照时,主表记录不允许被删除。

    解决方案:

    -- 1)先删干净 tb_student 中的所有数据,就可以删除 tb_clazz  使用 drop

    -- 2)将原有的学生,所关联的班级进行修改为其他班即可。 使用  update 语句

  4. 建表时可以增加以下设置:(了解,不严谨,慎用)

    -- 1)on delete cascade:当父表中的行被删除的时候,同时将子表中依靠的行删除。

    -- 2)on delete set null:当父表中的行被删除的时候,同时将子表中依靠的行的值转换为空值。

二. 列级约束

列级约束是指在创建表的列时,同时进行设置约束

例如:

--创建了一个班级表

create table tb_clazz (

       id int ,--主键

       ccode varchar2(10), --班级编号

       cname varchar2(50), --班级名字

       teacher varchar2(50) --班级老师

);

--创建了一个学生表

create table tb_student (

       id int primary key,--主键  

       stuname varchar2(20) not null,

       sex char(2) check(sex = '' or sex = ''),--检查约束

       age int check(age > 0 and age < 100),--检查约束

       address varchar2(100) default '广州黄埔', --默认值

       phone varchar2(11) unique,--唯一约束

       clazz_id int references tb_clazz(id)--外键约束

);

三. 表级约束

表级约束指的是在创建表中的列名之后,再进行设置约束

--创建了一个班级表

create table tb_clazz (

       id int,

       ccode varchar2(10), --班级编号

       cname varchar2(50), --班级名字

       teacher varchar2(50) --班级老师

--列完毕,开始表级约束

Primary key(id)

);

--创建了一个学生表

create table tb_student (

       id int,  

       stuname varchar2(20),

       sex char(2),

       age int,

       address varchar2(100) default '广州黄埔', --默认值

       phone varchar2(11),

       clazz_id int

--列完毕,开始表级约束

primary key(id),--主键

check(stuname is not null),--非空约束

check(sex=’’ or sex=’’),--检查约束

check(age>0 and age<100),--检查约束

unique(phone),--唯一约束

foreign key(clazz_id) references tb_clazz(id)--外键约束

);

注:最佳写法(一定要掌握,重点):外键约束写成表级约束,其他约束写成列级约束

四. 复合约束

--创建一个年月表,且为年月设置唯一约束

create table tb_ym(

       year char(4),

       month char(2),

       unique(year,month) --复合唯一约束

);

五. 约束维护

约束维护指的是对现有表的约束操作,包括添加约束,删除约束,激活约束,禁用约束,查看约束...

--创建一个学生表

create table tb_stud(

       id int,    

       stuname varchar2(20),

       sex char(2),  

       age int ,

       address varchar2(100) default '广州黄埔',

       phone varchar2(11),

       clazz_id int

);

 

--使用alter table 对现有表进行约束操作

 

--在现有表中添加约束

-- 1. 添加主键约束

alter table tb_student3 add primary key(id);

-- 2. 添加外键约束

alter table tb_student3 add foreign key(clazz_id) references tb_clazz(id);

-- 3. 添加非空约束

alter table tb_student3 add check(stuname is not null);

-- 4. 性别的检查约束

alter table tb_student3 add check(sex = '' or sex = '');

-- 5. 年龄的检查约束

alter table tb_student3 add check(age > 0 and age < 100);

-- 6. 电话的唯一约束

alter table tb_student3 add unique(phone);

 

--其他

-- 1. 删除约束,他是谁? SYS_C0011425:在左侧表中找到约束编号

alter table tb_student3 drop constraints SYS_C0011425;

-- 2. 禁用约束 enable   disable   

alter table tb_student3 disable constraints SYS_C0011427;

-- 3. 激活约束

alter table tb_student3 enable constraints SYS_C0011427;

 

-- 约束相关的数据字典

select * from tb_student3

where table_name = 'student';

select * from user_cons_columns

where table_name = 'student';

-- 查看约束

select c1.constraint_type,c1.table_name,c2.column_name,c2.constraint_name

from user_constraints c1,user_cons_columns c2

where c1.constraint_name = c2.constraint_name

and c1.table_name = 'student';

 

猜你喜欢

转载自www.cnblogs.com/whc0305/p/10145139.html