文章目录
- [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 | |
+----------+----------+------+-----+---------+-------+