MySQL约束条件一览

文章目录

  • [1 前言](#1 前言)
  • [2 空属性 null/not null](#2 空属性 null/not null)
  • [3 默认值 default](#3 默认值 default)
  • [4 列描述 comment](#4 列描述 comment)
  • [5 零填充 zerofill](#5 零填充 zerofill)
  • [6 主键 primary key](#6 主键 primary key)
  • [7 自增长 auto_increment](#7 自增长 auto_increment)
  • [8 唯一键 unique](#8 唯一键 unique)
  • [9 外键 foreign key](#9 外键 foreign key)

1 前言

约束:数据库通过技术手段限制用户插入的数据,保证数据合法、正确、完整、可预期。

在插入/修改数据时,如果按照约束条件插入/修改,那么,数据库中的数据就比较符合预期,符合人类逻辑。

常见的约束有:

空属性、默认值、列描述、ZEROFILL、主键、自增长、唯一键、外键

  • 站在程序猿角度:必须按照约束插入数据。
  • 站在MySQL角度:成功插入的数据,一定符合约束。

2 空属性 null/not null

NULL 表示什么都没有,不等于空字符串,不等于0。

NULL 的比较不能用等于,要用 is null / is not null。

一般情况下,插入的值不指定默认值,默认值就是 NULL。

eg:

原语句:

sql 复制代码
create table if not exists t1 (
  id itn unsigned not null,
  name char(20)
);

优化后,name 字段增加了默认值 NULL。

sql 复制代码
Create Table: CREATE TABLE `t1` (
  `id` int unsigned NOT NULL,
  `name` char(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

3 默认值 default

default的使用就像 C++ 中的缺省值。

使用方法:

sql 复制代码
create table if not exists t2 (
  name char(20),
  age tinyint unsigned not null default 18,
  gender char(1) default '男'
);

需要注意的是:

not null 和 default 并不冲突,只是两者同时存在的情况比较少。

当指定插入字段为 null 时不会触发dafault ,但会触发 not null。

4 列描述 comment

comment 就像注释一样,不是给mysql看的,是给程序猿看的。

之所以说 comment 是一种约束,是因为它是一种程序猿之间的约束。

5 零填充 zerofill

zerofill 不影响底层存储的数据,只是以更规整的格式显示。

如果数据位数大约显示位数,则按照数据显示,否则补充0到显示位数。

sql 复制代码
create table if not exists t3 (
  id int(5) zerofill
);

6 主键 primary key

主键用于标识一条记录的唯一性。即,不能重复、非空。

主键可以是一个字段构成,也可以是多个字段构成,即,符合主键。

数据库会在主键字段建立索引,加快查询效率。

主键通常是 int 类型。

注意:主键字段尽量不要是业务,当业务变化时,主键字段就非常尴尬。

eg:

sql 复制代码
create table if not exists t4(
  id int primary key comment "这是主键",
  name varchar(20) not null,
  age tinyint unsigned not null default 18
);

主键的功能约等于 unique + not null ,但意义不同。

一张表中只能有一个 primary key。

7 自增长 auto_increment

自增长主要是解决主键冲突问题的。常与主键、唯一键、索引搭配使用。

自增长是表级约束,可以指定默认值开始增长,不指定则为1。

自定从10开始增长,eg:

sql 复制代码
create table if not exists t5(
  id int primary key auto_increment,
  name varchar(20) not null
) auto_increment=10;

如果指定了一个较大的 id ,下次从较大值下一个数字开始增长,且以后不能指定更小的值。

获取最后一次插入自增值:

sql 复制代码
select last_insert_id();

早期QQ号通过 auto_increment 实现的。

8 唯一键 unique

唯一键保证某一字段不重复,加上 not null 可以模拟 primary key 的效果,但意义不同。

一个表可以有多个 unique 字段,mysql 会在 unique 字段自动建立索引。

unique 也是用于保证数据完整性的,例如,电话号码、身份证等。

sql 复制代码
create table if not exists t6 (
  id int primary key,
  name char(20) not null, 
  telphone char(11) not null unique
);

虽然电话号码在现实一定不会重复,但是加上 unique 约束,对数据的要求更加严格,避免了程序猿的粗心操作。

9 外键 foreign key

foreign key 也是表级约束,强调的是表与表之间的关联、表与表之间的约束。

从表的 foreign key 字段一般依赖主表的 primary key 或者 unique 字段。

如下两表,存在依赖关系。

eg:

sql 复制代码
create table if not exists classroom(
  id int primary key,
  NumOfTeacher tinyint not null
);

create table if not exists student(
  id int primary key,
  name char(20) not null,
  class_id int not null,
  foreign key (class_id) references classroom(id)
);
text 复制代码
+--------------+---------+------+-----+---------+-------+
| Field        | Type    | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| id           | int     | NO   | PRI | NULL    |       |
| NumOfTeacher | tinyint | NO   |     | NULL    |       |
+--------------+---------+------+-----+---------+-------+

mysql> desc student;
+----------+----------+------+-----+---------+-------+
| Field    | Type     | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| id       | int      | NO   | PRI | NULL    |       |
| name     | char(20) | NO   |     | NULL    |       |
| class_id | int      | NO   | MUL | NULL    |       |
+----------+----------+------+-----+---------+-------+
相关推荐
八月瓜科技19 分钟前
专精特新企业如何搭建专利资产中台?三步落地法
大数据·数据库·人工智能·科技·深度学习·机器学习·oracle
hackhase4 小时前
Django 多表数据整合:7 张异构表如何收敛成 4 类页面
后端·mysql
AllData公司负责人5 小时前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris
船神丿男人7 小时前
DevDocQA 的 Redis 答案缓存实战:版本切换、异步写回与故障排查
数据库·redis·缓存
CoderYanger9 小时前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis
云上工程笔记9 小时前
企业大模型调用数据安全与合规怎么保障:2026 年等保、数据留存、密钥与 IP 白名单清单
数据库·网络协议·tcp/ip
真上帝的左手10 小时前
4. 关系型数据库-MySQL-实战-索引治理
数据库·mysql·index
Joy T11 小时前
MySQL Outbox、Redis Streams 与 LangGraph——如何构建可靠的异步 AI Agent
数据库·redis·mysql·任务队列·redis streams·mysql outbox
FungLeo11 小时前
FengCMS · PHP+MySQL 升级实录· 回归测试从 69 项到 478 项:HTTP/数据库回归套件怎么搭
数据库·mysql·php·回归测试·fengcms
captain37611 小时前
Redis简单使用
数据库·redis·缓存