oracle数据库(八)六种约束及列表级约束

文章目录


约束命名
规范:约束类型_字段名
主键: PK_stuno
检查约束: CK_字段名
唯一约束: UQ_字段名
非空约束: NN_字段名
外键约束: FK_子表_父表
默认约束: 一般不需要命名

加约束名: constraint 约束名

create table student(
stuno number(3) constraint PK_stuno primary key ,
stuname varchar2(10) constraint NN_stuname not null constraint UQ_stuname unique ,
stuaddress varchar2(20) default ‘陕西西安’ constraint CK_stuaddress check(length(stuaddress)>2),
stubid number(3)
);

insert into student values(1, ‘zzz’,default,1) ;

注意事项:
约束名是 多个表公用的 (多个表中的约束 不能重名)

create table student2(
id number(3) constraint PK_stuno primary key ,
name varchar2(10)
);

表级约束

create table student2(
stuno number(3) ,
stuname varchar2(10) ,
stuaddress varchar2(20) ,
stubid number(3),
constraint PK_sno primary key(stuno) ,
constraint UQ_sname_subid unique(stuname,stubid),
constraint CK_saddress check( length(stuAddress)>2)
);

外键

create table student3(
stuno number(3) ,
stuname varchar2(10) ,
stuaddress varchar2(20) ,
subid number(3)
);

insert into student3(stuno,stuname,subid) values(1,‘zs’,1);
insert into student3(stuno,stuname,subid) values(2,‘ls’,1);
insert into student3(stuno,stuname,subid) values(3,‘ww’,2);

create table sub
(
sid number(3),
sname varchar2(10)
);

insert into sub values(1,‘java’);
insert into sub values(2,‘python’);

尝试插入非法数据:
insert into student3(stuno,stuname,subid) values(4,‘zl’,3);

给表加外键:
创建表的同时 增加外键
drop table student3 ;

create table student3(
stuno number(3) ,
stuname varchar2(10) ,
stuaddress varchar2(20) ,
subid number(3) ,
constraint FK_student3_sub foreign key(subid) references sub(sid)
);

其中,解释: constraint FK_student3_sub foreign key(subid) references sub(sid) ;

直接创建外建时报错:此列列表的【唯一关键字或主键】不匹配 ,含义是:外键所指向的 字段 必须先是 主键 或者唯一约束的键。

定义外键 需要4个参数: 两个表名 ,两个字段名

insert into student3(stuno,stuname,subid) values(1,‘zs’,1);
insert into student3(stuno,stuname,subid) values(2,‘ls’,1);
insert into student3(stuno,stuname,subid) values(3,‘ww’,2);

drop table sub;

create table sub
(
sid number(3) unique,
sname varchar2(10)
);
insert into sub values(1,‘java’);
insert into sub values(2,‘python’);

insert into student3(stuno,stuname,subid) values(1,‘zs’,1);
insert into student3(stuno,stuname,subid) values(2,‘ls’,1);
insert into student3(stuno,stuname,subid) values(3,‘ww’,2);
insert into student3(stuno,stuname,subid) values(4,‘zl’,3);

外键含义: A.a ->B.b字段, a中的数据 必须来自于b中。

如果删除父表中 外键所指向的列, 2个策略:级联删除|级联置空

drop table student3;
create table student3(
stuno number(3) ,
stuname varchar2(10) ,
stuaddress varchar2(20) ,
subid number(3) ,
constraint FK_student3_sub foreign key(subid) references sub(sid) on delete cascade【或set null 】
);

insert into student3(stuno,stuname,subid) values(1,‘zs’,1);
insert into student3(stuno,stuname,subid) values(2,‘ls’,1);
insert into student3(stuno,stuname,subid) values(3,‘ww’,2);

级联删除
drop table student3;
create table student3(
stuno number(3) ,
stuname varchar2(10) ,
stuaddress varchar2(20) ,
subid number(3) ,
constraint FK_student3_sub foreign key(subid) references sub(sid) on delete cascade
);


级联置空
drop table student3;
create table student3(
stuno number(3) ,
stuname varchar2(10) ,
stuaddress varchar2(20) ,
subid number(3) ,
constraint FK_student3_sub foreign key(subid) references sub(sid) on delete set null
);
insert into student3(stuno,stuname,subid) values(1,‘zs’,1);
insert into student3(stuno,stuname,subid) values(2,‘ls’,1);

级联删除:当删除父表中的数据时,子表 会跟着删除相对应的数据;
级联置空:当删除父表中的数据时,子表 会将 相对应的 那一字段的值设置为Null,其他字段不影响;

外键使用建议:
1.当父表中没有相对应数据时,不要向子表增加数据(如果sub表没有编号为2的课程,那么子表student不要去选择2号课程)
2.不要更改父表的数据,导致子表孤立
3.建议:在创建外键时 直接设置成 级联删除 或级联置空
4.删除表? 先删除子表,再删除父表

追加约束:在创建表示忘了加约束,后续可以追加约束

1.唯一、主键、检查、外键约束
alter table 表名 add constraint 约束名 约束类型I

create table student4(
stuno number(3) ,
stuname varchar2(10) ,
stuaddress varchar2(20) ,
subid number(3)
);
alter table student4 add constraint UQ_stuaddress4 unique(stuaddress);
alter table student4 add constraint PK_stuno4 primary key (stuno );

alter table student4 add constraint CK_stuname4 check(length(stuname)>2);

alter table student4 add constraint FK_student4_sub foreign key(subid) references sub(sid);

不适用于 默认、非空
alter table student4 add constraint NN_stuname not null(stuname);
alter table student4 add constraint DF_stuname default ‘hello’ ;

  1. 默认、非空

alter table 表名 motidy 字段名 constraint 约束名 约束类型

非空

alter table student4 modify stuname constraint NN_stuname4 not null ;

默认(建议默认约束不起名字,不写constraint)
alter table student4 modify stuname default ‘没名字’ ;

删除约束:
alter table 表名 drop constraint 约束名;
alter table student4 drop constraint UQ_stuaddress4;
alter table student4 drop constraint PK_stuno4;
alter table student4 drop constraint CK_stuname4;
alter table student4 drop constraint FK_student4_sub;
alter table student4 drop constraint NN_stuname4;

特殊情况: 
	默认约束(删除默认约束: 将默认约束置为null)
alter table student4  modify  stuname  default null   ;

完整性约束:保证数据的正确性、相容性、防止数据冗余等。
域完整性: 列。数据类型,非空约束,检查约束,外键约束

实体完整性:行。主键约束、唯一约束

引用完整性:不同表之间。外键

自定义完整性:触发器(当执行换一个操作时,会自动触发另一个操作)。例如:自定义需求 学生的上学时间 必须在出生日期之后。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值