MySQL约束条件
# 约束条件
# 约束条件 not null 与 default
必须写的是字段名和字段类型
zerofill
填充 0
not null
值不能为空
不写,默认为空
default
可以设置默认值
# 约束条件 unique key
单列唯一
# 正常来说,部门名不能有几个一样的
create table department (
id int unique,
name char(10) unique
);
# unique 可以限制name字段必须唯一
# 方式2
create table department (
id int,
name char(10),
unique(id),
unique(name)
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
联合唯一
create table services (
id int,
ip char(15),
port int,
unique(id),
unique(ip, port) # 合到一起必须唯一
);
# 可以有某一个重复,但是ip和port联合到一起必须唯一
insert into services values
(1, '192.168.1.1', 80),
(2, '192.168.1.1', 8080),
(3, '192.168.1.12', 80)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# primary key 主键约束
约束条件:
- not null
- unique
存储引擎现在默认为 innodb
:对于此引擎来说,一张表必须有一个主键
单列主键
create table t1(
id int primary key,
name char(16)
);
# 可以正常插入
insert into t1 values
(1, 'wujie'),
(2, 'alic')
# 2重复了,会插入失败
insert into t1 values
(3, 'xxxx'),
(2, 'wxxx')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
联合主键,复合主键
create table services (
id int,
ip char(15),
port int,
primary key (ip, port)
);
1
2
3
4
5
6
2
3
4
5
6
# auto_increment 自增约束
create table t1 (
id int primary key auto_increment,
name char(6)
);
1
2
3
4
2
3
4
auto_increment 自增的字段必须是 key
# 此时不需要插入id,此时自增长
insert into t1 values
('dqwdqwd'),
('dwqdqwdqw'),
('dwqdwqdwqdqwdq')
1
2
3
4
5
2
3
4
5
此时 id 如果非要传,也是可以传的,但是你得复合它的约束条件
了解内容
show variables like 'auto_inc';
1
- auto_increment_increment 步长 默认为 1
- auto_increment_offset 起始偏移量 默认为 1
设置步长
# 会话级别 意味着只在本次连接中有效
set session auto_increment_increment=5;
# 全局设置 设置完了必须重新登录
set global auto_increment_increment=5;
1
2
3
4
5
2
3
4
5
设置起始偏移量
起始偏移量必须小于等于步长,否则失效
set global auto_increment_offset=3;
1
设置完成之后,必须重新连接加载
# 清空表
delete from t1; # 没有加条件,代表全部数据都删
1
注意,如果 id 自增长,此时 id 的增长的值没有重置为 1
# 截断表
truncate t1;
1
2
2
不要使用 delete,delete 是配合 where 删除固定的或固定范围的数据
# 约束条件 foreign key
建立表
员工表和部分表外键约束关系
# 先进被关联的表
create table dep (
id int primary key,
name char(16),
comment char(50)
);
create table emp (
id int primary key,
name char(10),
sex enum('male', 'female'),
dep_id int,
foreign key(dep_id) references dep (id)
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
被关联的表的字段必须是唯一
插入记录
先往被关联的表插入数据,再往关联表插入数据
insert into dep values
(1, 'IT部门', '技术'),
(2, '销售部门', '用钱的'),
(3, '财务部门', '花钱的')
insert into emp values
(1, 'dwqdqwd', 'male', 2),
(2, 'dwqdqwdwdq', 'male', 3),
(3, 'dwqdqdwqdqwd', 'male', 3),
delete from dep where id = 1;
delete from emp where dep_id = 1;
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
删除,也必须先删被关联的,再删关联的内容
简化删除操作
建表的时候,还有一个参数
on delete cascade on update cascade
可以进行删除和修改的时候同时删除对应的关联信息
# 先进被关联的表
create table dep (
id int primary key,
name char(16),
comment char(50)
);
create table emp (
id int primary key,
name char(10),
sex enum('male', 'female'),
dep_id int,
foreign key(dep_id) references dep (id)
on delete cascade on update cascade
);
# 对应的员工也会删除掉
delete from dep where id = 1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
建议:在真正项目里,还是不建议这么建立起外键约束关系,使用我们自己语言程序来控制。如果那样做了,会导致强耦合。考虑长期的扩展性,也不要建立。
# 两张表之间的三种关系
- 多对多
- 多对一
- 一对一
多对一
左边多条员工记录对应右边的部门表
简而言之:一个部门可以有多个员工
先从左边的记录,看看能不能多条记录对应右边一个记录
案例:
出版社 一本书
多本书可以由一个出版社出版。右边表新增一个字段为出版社的主键 id
多对多
作者 书
一个作者可以出很多书
一个书可以有多个作者一起写
一个书的表,一个作者的表
使用一个中间表来记录两者的多对多关系
一对一
学生表关联客户
一个学生就是一个客户
外键约束:foreign + unique
create table customer (
id int primary key auto_increment,
name varchar(20) not null,
qq varchar(10) not null,
phone char(11) not null
);
create table student (
id int primary key auto_increment,
class_name varchar(20) not null,
customer_id int unique,
foreign key (customer_id) references customer(id)
on delete cascade on update cascade
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
编辑 (opens new window)
上次更新: 2022/01/13, 23:04:18