73人参与 • 2026-05-12 • Mysql
今天这篇博客,我把 mysql 里表的约束从头到尾梳理一遍,全部是面试、开发高频考点,配案例、配语法、配易错点,看完直接能用在项目里。
数据类型只能做最基础的限制,但业务里要求更严格:比如邮箱不能重复、姓名不能为空、学号必须唯一、成绩不能为负……
约束 = 给字段加 “规则”,从业务层面保证数据合法、正确、不混乱。
mysql 常用约束一共 8 个:null / not null、default、comment、zerofill、primary key、auto_increment、unique key、foreign key。
作用
控制字段是否允许为空。
null:允许为空(默认)not null:不允许为空关键点
1 + null = nullnot 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
作用
插入数据时不指定该字段,自动使用预设值。
示例
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');
作用
给字段加注释说明,方便团队维护。
示例
create table tt12 (
name varchar(20) not null comment '姓名',
age tinyint unsigned default 0 comment '年龄'
);
查看注释
show create table tt12\g
作用
数字不足指定位数时,前面补 0,只影响显示,不影响存储。
示例
alter table tt3 change a a int(5) unsigned zerofill;
插入 1 → 显示 00001,实际存储还是 1。
作用
唯一标识一行数据,约束最强:
示例
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;
作用
字段不赋值时自动 + 1,通常配合主键使用。
特点
示例
create table tt21(
id int unsigned primary key auto_increment,
name varchar(10) not null default ''
);
获取刚插入的自增 id
select last_insert_id();
作用
保证字段业务不重复,但允许为 null。
主键 vs 唯一键
示例
create table student (
id char(10) unique comment '学号',
name varchar(10)
);
作用
约束表与表之间的关系,保证数据逻辑一致。
规则:
示例
先建主表(班级):
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 不改变存储(一眼不混,直接背)
| 分类 | 作用 | 关键字 / 语句 | 示例 |
|---|---|---|---|
| 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; |
最核心记忆口诀(背会永不混)
mysql 约束看似多,其实逻辑非常统一:都是为了管住数据,不让脏数据进库。开发时先想清楚字段规则,再建表,后面少填无数坑。
以上就是一文系统梳理mysql数据库中表的约束的详细内容,更多关于mysql表约束的资料请关注代码网其它相关文章!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论