当前位置:网站首页>(三)MySQL约束
(三)MySQL约束
2022-04-22 19:10:00 【小何┌】
目录
什么是约束?
约束实际上就是表中数据的限制条件
1、主键约束 primary key
一张表只能有一个主键约束。
给某个字段添加了主键约束之后,它不能为空,且唯一,
但与not null + unique的区别是 :主键约束除了可以做到非空、唯一之外,还可以添加索引 : index
create table student(
id int primary key comment '学生id'
);
由于主键唯一,所以不能重复添加主键约束。会报错:Multiple primary key defined
2、非空约束 not null
限制该字段不能为null
若添加null值 :报错Column 'name' cannot be null
create table student(
name varchar(10) not null comment '学生姓名'
);
3、唯一约束 unique
唯一约束保证该字段的值只能出现一次
重复添加会报错
create table student(
// 非空且唯一
phonenum varchar(20) unique not null comment '手机号'
);
4、默认约束 default
保存数据时,如果未指定该字段的值,则采用默认值
create table student(
gender char(1) default '男' comment '性别'
);
5、检查约束 check
保证字段值满足某一个条件
create table student(
age int check(age >= 0 and age < 120) comment '学生年龄'
);
约束的增加、修改和删除
可以看出,除了主键和唯一约束,其他约束的修改方式都是用modify修改字段时的操作。
同时,主键和唯一约束也可以用这种方式修改
主键
增加主键约束:
alter table student add primary key(id);
删除主键约束:
// 由于主键唯一,所以删除时不必指定字段
alter table studnet drop primary key;
非空
增加非空约束:
alter table student modify id int not null;
删除非空约束:
alter table student modify id int;
默认
增加默认约束:
alter table student modify gender char(1) default '男';
删除默认约束:
alter table student modify gender char(1);
唯一
增加唯一约束
## 如果要增加的字段中,已经有重复的信息,就不能再加唯一约束,否则报错
alter table student add unique(phonenum)
删除唯一约束
## 正常的修改操作,修改唯一约束的字段不唯一就可以了
alter table student modify phonenum varchar(11);
检查:
增加检查约束:
alter table student modify age int check(age >= 0 and age < 120);
删除检查约束:
alter table student modify age int ;
6、外键约束 foreign key
外键添加
用来与其他表建立连接,保证数据的一致性和完整性
字段类型不同不能添加。
不使用特殊手段时:
父表外键约束的字段中的信息不能删除、修改
子表可以修改,不能删除
(可以将外键约束清除后再删)
添加外键约束:
alter table 表名1
add constraint fk_表名1_字段1
foreign key(字段1) references 表名2(字段2);
// fk_表名1_字段1 实际上是给外键起的别名,也可以改成其他的
eg:
alter table student
add constraint fk_student_classid
foreign key (classid) references class(id);
外键删除
alter table student drop foreign key 外键名;
alter table student drop foreign key fk_student_classid;
外键信息的删除/更新行为

1、cascade :删除或更新父表元素的同时删除/更新子表元素
可指定是否同时删除、更新子表元素。如:on update cascade 、on delete cascade
alter table student
add constraint fk_student_classid foreign key(classid)
references class(id) on update cascade on delete cascade;


更新父表外键信息,子表对应的信息也会被更新
update class set id = 4 where id = 3;
// 将普通班的id:3改成4
对应数据:

删除父表外键信息,子表对应的字段也会被删除
delete from class where id = 1;
// 将博雅班删除掉

2、set null :删除父表数据时,将子表的数据设置为null;(前提是子表没有非空约束)
父表
create table class (
id int primary key,
name varchar(10) not null
);
insert into class values (1, '博雅班'),
(2, '阳光班'),
(3, '普通班');
子表
create table student (
id int primary key ,
name varchar(10) not null,
classid int ,
constraint fk_student_classid foreign key(classid) references class(id)
on update set null on delete set null
);
insert into student values (1, '李四', 1),
(2, '王五', 2),
(3, '赵六', 3);
(需要注意的是,添加外键约束时,父表对应的字段应有主键约束)
执行以下操作时,
update class set id = 4 where id = 3; // 修改
delete from class where id = 1; // 删除

版权声明
本文为[小何┌]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_62939743/article/details/124342649
边栏推荐
- Project training - Design and development of 2D multiplayer scuffle game based on unity (III. unity plasticscm multiplayer collaboration)
- Huawei cloud media Zha Yong: Huawei cloud's technical practice in the field of video AI transcoding
- yes. Net future
- 14-Sprak设置自动分区
- 欢迎页展示
- transfer方法详解
- Small LED screen / digital alarm clock display screen / LED billboard / temperature digital display and other LED nixie tube display driver ic-vk1640 / 1640B sop28 / ssop24 package
- The research group of Shenzhen University issued the evaluation report on sustainable development of Shenzhen (2016-2021)
- Pychar configures CONDA and uses the correct image source address in China
- [self redemption -- top 101 4-day question brushing plan of niuke.com] warm up exercise on the first day
猜你喜欢

项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(小地图修改完善)

Project training - Design and development of 2D multiplayer fighting game based on unity (pre work knowledge of small map: camera)
![[TCP] TCP three handshakes and four waves](/img/d1/20252b9d83730ca6c6cfa06673eacb.png)
[TCP] TCP three handshakes and four waves

项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(小地图工作前期知识:摄像机)

Database index

Introduction to Alibaba micro service component Sentinel

LeetCode_343 整数拆分

C#与 Halcon 联合编程
![[self redemption -- top 101 4-day question brushing plan of niuke.com] warm up exercise on the first day](/img/31/8120f310af28e4cf49f67616748b7a.png)
[self redemption -- top 101 4-day question brushing plan of niuke.com] warm up exercise on the first day

leetcode:642. Design search automatic completion system
随机推荐
MySQL query with serial number
Introduction to feign, a microservice invocation component
What happens when you run the NPM install command?
青训营-刷题打卡-控制并发执行goroutine的数量
Mysql索引
剪切的文件在哪里?文件剪切丢失如何恢复?仅需3步
Simple implementation of linear regression
2019-12-07 wav audio cutting and merging
关于字符串常量池,intern方法的理解
14-Sprak设置自动分区
2020-10-26 go language grpc learning
Error c4996 'fopen': this function or variable may be unsafe Consider using fopen_ s instead. To disabl
SQL命令 DISTINCT
Will map () in JS change the original array
2019-12-07 wav音频剪切与合并
2020-11-04 go test compiled into executable file
final的作用以及String为什么不可变
Better than SQL, another domestic database language was born
ArrayList学习笔记
Where is the cut file? How to recover the lost file cut? Just 3 steps