学习视频: https://www.bilibili.com/video/BV1tJ411r7EC?p=35
数据的完整性:保证插入表格的数据必须正确。包括实体完整性、区域完整性、引用完整性、自定义完整性。
约束:约束的作用是为了保证数据的完整性,类型包括主键约束(PK)、唯一约束()、检查约束、外键约束。主键和唯一约束的区别是主键不能为空。命名规则:约束类型_约束名。
列级约束:针对某一列设置的约束。表级约束和列级约束区别相关了解:https://blog.csdn.net/hanxuemin12345/article/details/7828206
--设置主键的简洁写法
create table user1(
id number(5) primary key,
name varchar2(20)
)
--设置主键的完整写法
create table user1(
id number(5) constraint pk_id primary key,
name varchar2(20)
)
--添加主键约束、唯一约束、非空约束
create table user3(
id number(3) constraint pk_user3_id primary key,
name varchar2(20) constraint uq_user3_name unique,
age number(3) constraint nn_user3_age not null
)
insert into user3 values(1,"bibi",34);
insert into user3 values(2,"bibiiii",34);
insert into user3 values(3,"bibi",null);
检查约束:
alter table user3 add salary number(8,2);
alter table user3 add constraint ck_user3_salary check (salary between 5000 and 20000);
表级约束:在定义表格的某些列之后,再为某些列添加约束。非空约束只能是列级约束。
create table user3(
id number(3) ,
name varchar2(20) ,
age number(3) not null,
constraint pk_user3_id primary key (id),
constraint uq_user3_name unique (name)
)
create table user3(
id number(3) ,
name varchar2(20) ,
age number(3) not null,
constraint uq_user3_name unique (id,name)
)
View Code
表定义之后,添加、移除约束:
alter table user3 add constraint pk_usesr3_id primary key (id);
alter table user3 drop constraint pk_user3_id;
外键约束:本表这列的值取决于另一个表的主键。删除主表关联数据是,必须先把从表的外键关联数据删除或者置空。删除主表之前必须先把从表删除掉或者主表切断所有关联的外键。
create table user8
(
id number(3) primary key,
name varchar2(20) not null
);
insert into user8 values(1,"bb");
insert into user8 values(2,"bbb");
insert into user8 values(3,"bbbb");
create table order8
(
id number(10) primary key,
user_id number(3) constraint fk_order8_userid references user8(id), --外键,引用user8表的id
info varchar2(500)
);
insert into order8 values(1,1,null);
insert into order8 values(1,5,null);
create table user8
(
id number(3) primary key,
name varchar2(20) not null
);
insert into user8 values(1,"bb");
insert into user8 values(2,"bbb");
insert into user8 values(3,"bbbb");
create table order8
(
id number(10) primary key,
user_id number(3) constraint fk_order8_userid references user8(id), --外键,引用user8表的id
info varchar2(500)
)
insert into order8 values(1,1,null);
insert into order8 values(2,2,null);
insert into order8 values(3,3,null);
--删除关联数据 方法一,不常用
delete from order8;
delete from user8;
--方法二 置空
update order8 set user_id=null;
delete from user8;
--删除关联表 不常用
drop table order8;
drop table user8;
--方法二 删除掉所有关联的外键
drop table user8 cascade constraint;
View Code
级联操作:当删除主表中被参考数据时,从表关联外键字段数据自动设为null,用法 on delete set null。当删除主表被参考数据时,从表外键关联数据行自动删除,用法 on delete cascade。
create table order8
(
id number(10) primary key,
user_id number(3) references user8(id) on delete set null,
info varchar2(500)
)
create table order8
(
id number(10) primary key,
user_id number(3) references user8(id) on delete cascade,
info varchar2(500)
)
View Code
联合主键:多个字段组成一个主键
create table user8
(
id number(3) ,
name varchar2(20),
constraint pk_user8_id_name primary key (id,name)
);
insert into user8 values(1,"b");
insert into user8 values(1,"a");
insert into user8 values(1,"b");