今天这篇博客,我把 mysql 里表的约束从头到尾梳理一遍,全部是面试、开发高频考点,配案例、配语法、配易错点,看完直接能用在项目里。
一、什么是表约束?为什么要用?
数据类型只能做最基础的限制,但业务里要求更严格:比如邮箱不能重复、姓名不能为空、学号必须唯一、成绩不能为负……
约束 = 给字段加 “规则”,从业务层面保证数据合法、正确、不混乱。
mysql 常用约束一共 8 个:null / not null、default、comment、zerofill、primary key、auto_increment、unique key、foreign key。
二、空属性:null / not null
作用
控制字段是否允许为空。
null:允许为空(默认)not null:不允许为空
关键点
- 空值无法参与运算:
1 + null = null - 实际开发尽量用
not null,数据更可靠
示例
create table myclass(
class_name varchar(20) not null,
class_room varchar(10) not null
);
插入时缺了 class_room 直接报错:
error 1364 : field 'class_room' doesn't have a default value
三、默认值:default
作用
插入数据时不指定该字段,自动使用预设值。
示例
create table tt10 (
name varchar(20) not null,
age tinyint unsigned default 0,
sex char(2) default '男'
);
只插 name,age=0、sex = 男 自动填充:
insert into tt10(name) values('zhangsan');
四、列描述:comment
作用
给字段加注释说明,方便团队维护。
示例
create table tt12 (
name varchar(20) not null comment '姓名',
age tinyint unsigned default 0 comment '年龄'
);
查看注释
show create table tt12\g
五、零填充:zerofill
作用
数字不足指定位数时,前面补 0,只影响显示,不影响存储。
示例
alter table tt3 change a a int(5) unsigned zerofill;
插入 1 → 显示 00001,实际存储还是 1。
六、主键:primary key
作用
唯一标识一行数据,约束最强:
- 不能重复
- 不能为空
- 一张表最多一个主键
示例
create table tt13 (
id int unsigned primary key comment '学号',
name varchar(20) not null
);
复合主键
多个字段一起做主键,组合唯一:
create table tt14(
id int unsigned,
course char(10),
primary key(id, course)
);
追加 / 删除主键
alter table 表 add primary key(字段); alter table 表 drop primary key;
七、自增长:auto_increment
作用
字段不赋值时自动 + 1,通常配合主键使用。
特点
- 必须是索引列
- 必须是整数
- 一张表最多一个自增
示例
create table tt21(
id int unsigned primary key auto_increment,
name varchar(10) not null default ''
);
获取刚插入的自增 id
select last_insert_id();
八、唯一键:unique key
作用
保证字段业务不重复,但允许为 null。
主键 vs 唯一键
- 主键:不能为空,一张表一个
- 唯一键:可以为空,一张表多个
示例
create table student (
id char(10) unique comment '学号',
name varchar(10)
);
九、外键:foreign key
作用
约束表与表之间的关系,保证数据逻辑一致。
规则:
- 从表外键的值 必须在主表主键里存在
- 或为 null
示例
先建主表(班级):
create table myclass (
id int primary key,
name varchar(30) not null
);
再建从表(学生):
create table stu (
id int primary key,
name varchar(30) not null,
class_id int,
foreign key (class_id) references myclass(id)
);
插入不存在的班级 id 直接报错,保证数据不乱。
十、综合实战:商城三表设计(高频面试题)
商品表、客户表、订单表,一次写完主外键 + 所有约束:
create database if not exists mall default charset utf8;
use mall;
-- 商品表
create table goods(
goods_id int primary key auto_increment comment '商品id',
goods_name varchar(32) not null comment '商品名',
unitprice int not null default 0 comment '单价(分)',
category varchar(12) comment '类别',
provider varchar(64) not null comment '供应商'
);
-- 客户表
create table customer(
customer_id int primary key auto_increment comment '客户id',
name varchar(32) not null comment '姓名',
address varchar(256) comment '地址',
email varchar(64) unique comment '邮箱',
sex enum('男','女') not null comment '性别',
card_id char(18) unique comment '身份证'
);
-- 订单表
create table purchase(
order_id int primary key auto_increment comment '订单id',
customer_id int comment '客户id',
goods_id int comment '商品id',
nums int default 0 comment '数量',
foreign key (customer_id) references customer(customer_id),
foreign key (goods_id) references goods(goods_id)
);
十一、高频考点速记(面试必背)
not null不能为空,default提供默认值comment是注释,zerofill只补 0 不改变存储- 主键唯一且非空,一张表一个
- 自增长必须是整数、索引列
- 唯一键允许 null,可建多个
- 外键保证表关系合法,主表必须有主键 / 唯一键
十二、mysql 约束|表结构修改|数据修改 终极对比表
(一眼不混,直接背)
| 分类 | 作用 | 关键字 / 语句 | 示例 |
|---|---|---|---|
| 1. 字段约束(建表用) | 给字段定规则(不能为空、唯一、默认值等) | not null default comment zero fill primary key auto_increment unique key foreign key | name varchar(20) not nullage int default 0id int primary key auto_increment |
| 2. 修改表结构(alter) | 改表:加字段、删字段、改约束、加主键 | alter table 表add(添加)drop(删除)modify(改类型 / 约束)change(改名 + 改类型) | alter table t add primary key(id);alter table t modify name not null; |
| 3. 修改表中数据 | 改表里的内容:增 / 删 / 改记录 | insert(插入)update(修改)delete(删除) | insert into t values (1,' 张三 ');update t set name=' 李四 ' where id=1;delete from t where id=1; |
最核心记忆口诀(背会永不混)
- 约束:管字段规则(not null / primary key 等)
- alter:管表结构(加字段、改约束、加主键)
- insert / update / delete:管表里的数据
十三、结束语
mysql 约束看似多,其实逻辑非常统一:都是为了管住数据,不让脏数据进库。开发时先想清楚字段规则,再建表,后面少填无数坑。
以上就是一文系统梳理mysql数据库中表的约束的详细内容,更多关于mysql表约束的资料请关注代码网其它相关文章!
发表评论