跳至主要內容

完整性约束/约束条件

张威大约 1 分钟mysqlmysql基础

MySQL完整性约束

主键约束

primary key不能重复;不能为空;能唯一标识一条记录

自增键约束

auto_increment,不能用于字符串

唯一键约束

unique保证索引不重复-唯一索引;可以为空;可以用not null约束;default 设置默认值

非空约束

not null

默认值约束

default

外键约束

foreign key另一个表的主键;可以重复;可以为空

create table user(
	id int primary key auto_increment comment '主键索引',
	nickname varchar(50) unique not null comment '昵称'
	age tinyint unsigned not null default 18,
	sex enum('male', 'female')
);
image-20240406214640176
image-20240406214640176

现代的后端开发中 外键、存储函数、存储过程、触发器... 这些限制逻辑或者代码逻辑是由MySQL本身控制的,现在性能瓶颈在于存储层逻辑,所以主要核心CRUD由MySQL做,其他的,减少MySQL的压力