Oracle中的约束

1.概况

对数据的限制条件: 数据类型

2.常见约束类型

检查约束(check): name > 4 限制长度
唯一约束(Unique): id:1 2 3 4 null 某个字段字段只能是一个值
主键约束(Primary key): 类似唯一约束(唯一)
外键约束(Foreign Key): 两张表 学生表 课程表(1 2 3)
非空约束(Not null): 不能为null
默认约束(Default): adress: 西安

主键和唯一的区别
a.【主键不能为Null】,唯一 可以为null
b.主键可以是复合主键,也可以是单值主键(id)
c.一张表中 只能设置一次主键(复合主键),但唯一键可以设置多次

3. 约束分类

列级约束表级约束
作用于一个列作用于一个列/多列
列的后面表的后面
可以有多个约束(空格)逗号(,)
全部6个4个(主键,外键,唯一检查)

3.1 列级约束

create table student(
 stuno number(3) primary key  ,
 stuname varchar2(10) not null unique ,
 stuaddress varchar2(20) default '陕西西安' check(length(stuaddress)>2),
 stubid number(3)
);

-default代表使用 默认约束的值
insert into student values(5, ‘xx’, default ,5) ;

主键: 非空+唯一
insert into student values(1, ‘zs’,‘h北京’,2) ;
insert into student values(2, ‘zs1’,‘h北京’,2) ;

注意事项:
a.报错:违反唯一约束条件 可能主键报错 也可能唯一约束报错
b.如果有多个约束,default必须放在第一位
c.check约束: 如何编写 :和使用where完全相同
d.唯一约束: 可以是Null,但不适用于Null(可以有多个null)

约束命名
规范:约束类型_字段名
主键: 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)
);

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

3.2 表级约束

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)
);

4.外键

在这里插入图片描述

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个参数: 两个表名 ,两个字段名

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

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

如果删除父表中 外键所指向的列, 2个策略:级联删除|级联置空
级联删除:当删除父表中的数据时,子表 会跟着删除相对应的数据;
级联置空:当删除父表中的数据时,子表 会将 相对应的 那一字段的值设置为Null,其他字段不影响;

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

4.1 级联删除

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
);

4.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 set null 
);

5.追加约束

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

5.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’ ;

5.2 默认,非空

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

alter table student4 modify stuname constraint NN_stuname4 not null ;

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

6.删除约束

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   ;

7.完整性约束

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

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值