一、字段约束条件
1.无负号
unsigned
create table t(id int unsigned); # 不能添加负数
2.零填充
zerofill
create table t(id int zerofill); #填入得数据 展示会自动被零填充至 展示得长度
3.非空
not null
create table t(name varchar(32) not null); #不能不填入数据
4.默认值
default
create table t(gender varchar(32) default 'male') #数据默认为male
5.唯一值
unique
create table t(id int unique); #单列唯一 出现重复报错
create table t2(host varchar,port int,unique) #创建 联合唯一
#当 host 和 port 合并 出现重复时 报错
6.自增
auto_increment
1.不能单独使用 必须跟在键后面 (主要配合主键一起使用)一张表只能出现一次
2.可以让我们录入数据时省点事,而且这个东西绑定在主键后面,如果有人动过某段数据,再新增数据时可以根据主键的形式看出来
3.要注意,设置自增后,只能往前走,如果被删除或者打断不会停下计数的脚步,想恢复正常只能指定更大的数覆盖或者全表重置(使用truncate)
# 代码实操
# 创一个表,随便设两个字段,其中一个作为主键,跟上自增
create table mybiao(id int primary key auto_increment,name varchar(16));
# 表创建好了去塞入数据,这时候id 就可以不用自己去掐指头输入了
insert into mybiao(name) values('jack'),('nike'),('rose');
# 查看下数据
select * from mybiao;
>>>
+----+------+
| id | name |
+----+------+
| 1 | jack |
| 2 | nike |
| 3 | rose |
+----+------+
二、约束条件——主键
- 前提:innoDB引擎下,所有表都得需要主键,如果自己没设置,在没有字段是非空且唯一的情况下,mysql会贴心的给你的表创一个隐藏字段作为主键,如果有字段是非空且唯一,从上至下第一个非空且唯一的字段加冕为主键!
创建表的时候应该有一个id字段 并且该字段 应该作为主键,主键可以加快数据查询
主键 = not null + unique
语法
:约束条件写 >>> primary key
# 补充说明
# 单列主键:
create table t(id int primary key ,name varchar(32))
# 联合主键:
create table t(sid int,nid int,name varchar(32),primary key(sid,nid));
三、约束条件——外键
1.简介
用来记录表与表之间数据得关系 而数据得关系 有四种
- 一对多关系
- 多对多关系
- 一对一关系
- 没有关系
可以自己换位思考站在某一方表下,判断数据关系
如果 一个可以 一个不可以那么表关系就是 《一对多》
如果两边都可以,那么表关系就是《多对多》
2.关系形式——一对多
只有一边能多,外键建在多对一的表这边。
!!!要注意:先创建被关联表,因为一对多,多表是根据被关联表建立外键字段的
# 先创建班级表
create table class_info(id int primary key auto_increment, class_name varchar(16), miaoshu varchar(16));
# 再创建学生表,学生表做外键关联班级表
create table student_info(id int primary key auto_increment,
-> student_name varchar(16),
-> class_num int,
-> foreign key (class_num) references class_info(id)
-> );
# 添数据进被关联表,一定要先往被关联表里
insert into class_info(class_name,miaoshu) values('1班','尖子班'),('2班','天龙班'),('3班','普通班');
# 每次插入了啥数据都检查下
select * from class_info;
>>>
'''
+----+------------+-----------+
| id | class_name | miaoshu |
+----+------------+-----------+
| 1 | 1班 | 尖子班 |
| 2 | 2班 | 天龙班 |
| 3 | 3班 | 普通班 |
+----+------------+-----------+
'''
# 往学生表添加数据
insert into student_info(student_name,class_num) values('李华',1),('小王',2),('小李',2),('小张',3);
# 因为已经关联上,
>>>
'''
+----+--------------+-----------+
| id | student_name | class_num |
+----+--------------+-----------+
| 1 | 李华 | 1 |
| 2 | 小王 | 2 |
| 3 | 小李 | 2 |
| 4 | 小张 | 3 |
+----+--------------+-----------+
'''
3.级联更新、级联删除
被关联的数据一旦变动 关联的数据同步变动
语法:
跟在外键后面,外键不用加逗号哦!
on update cascade #级联更新
on delete cascade #级联删除
4.关系形式——多对多
不能直接创建,需要建立第三张表
# 代码实操
create table book(
id int primary key auto_increment,
title varchar(32),
author_id int,
foreign key(author_id) references author(id)
on update cascade #级联更新
on delete cascade #级联删除
);
#需要单独开设第三张关系表 存储数据关系
create table book(
id int primary key auto_increment,
title varchar(32)
);
create table author(
id int primary key auto_increment,
name varchar(32)
);
create table book2author(
id int primary key auto_increment,
book_id int,
foreign key(book_id) references book(id)
on update cascade #级联更新
on delete cascade, #级联删除
author_id int,
foreign key(author_id) references author(id)
on update cascade #级联更新
on delete cascade #级联删除
);
5.关系形式——一对一
两边有关系,但是又不是那么有关系,只能表1的一个数据对一个表2的数据
针对一对一的表关系 外键字段建在任何一张表都可以 但是建议你建在查询频率较高的表中便于后续查询
# 代码实操
create table user(
id int primary key auto_increment,
name varchar(32)
detail_id int unique,
foreign key(detail_id) references userDetail(id)
on update cascade #级联更新
on delete cascade #级联删除
);
create table userDetail(
id int primary key auto_increment,
phone bigint
);