约束

79 阅读1分钟

1.约束分类:

#not null 非空约束

#unique 唯一性约束

#primary key 主键约束

#foreign KEY 外键约束

#check 检查约束

#default 默认值约束

image.png

1.2非空约束

image.png

1.3唯一性(unique)

image.png

image.png

1.4主键约束==(非空+唯一)

自增(auto_increment)----->必须是唯一性字段

image.png

image.png

1.5外键约束(引用完整性)

image.png

被关联的字段必须有唯一性约束(unique/primary key)

create table employees(
id int primary key auto_increment,
department_id int,
salary declmal(10,2)
)

alter table employees
modify department_id int unique;------>添加唯一性

create table departments(
department_id int primary key,
department_name varchar(20),

foreign key(department_id) references employess(department_id)---------->外键约束


foreign key (子表字段) references 主表(字段);
)