当前位置:网站首页>MySQL基础——约束
MySQL基础——约束
2022-06-22 18:54:00 【白巧克力x】
目录
上篇文章我们学习了MySQL基础——函数,这篇文章我们学习MySQL基础——约束。
约束
约束是作用于表中字段上的规则,用于限制存储在表中的数据,其目的是保证数据库中数据的正确、有效性和完整性。
常用的约束如下表:
| 约束 | 描述 | 关键字 |
|---|---|---|
| 非空约束 | 限制该字段的数据不能为null | NOT NULL |
| 唯一约束 | 保证该字段的所有数据都是唯一、不重复的 | UNIQUE |
| 主键约束 | 主键是一行数据的唯一标识,要求非空且唯一 | PRIMAPY KEY |
| 默认约束 | 保存数据时,当没指定该字段的值,则使用默认值 | DEFAULT |
| 检测约束(8.0版本之后) | 保证字段值满足某个条件 | CHECK |
| 外键约束 | 使两张表的数据之间建立连接,保证数据的一致性和完整性 | FOREIGN KEY |
注意:约束是作用于表中字段上的,可以在创建表/修改表的时候添加约束。
案例
接下来我们通过案例来演示在创建数据表时设置约束。
案例需求如下:
| 字段名 | 字段含义 | 字段类型 | 约束条件 | 约束关键字 |
|---|---|---|---|---|
| id | ID唯一标识 | int | 主键,并且自动增长 | PRIMARY KEY,AUTO_INCREMENT |
| name | 姓名 | varchar(10) | 不为空且唯一 | NOT NULL,UNIQUE |
| passwrod | 密码 | varchar(10) | 默认值为123456 | DEFAULT |
| gender | 性别 | char(1) | 无 |
示例代码如下:
CREATE TABLE students(
id int PRIMARY KEY AUTO_INCREMENT COMMENT '用户id',
name varchar(10) NOT NULL UNIQUE COMMENT '姓名',
password varchar(10) DEFAULT '123456' COMMENT '密码',
gender char(1) COMMENT '性别'
)COMMENT '学生表';如下图所示:

这样就成功在创建表的时候为字段创建约束。
外键约束
外键是用来让两张表的数据之间建立连接,从而保证数据的一致性和完整性。
添加外键
添加外键语法格式如下:
#在创表时添加外键
CREATE TABLE 表名(
字段名 数据类型,
...
[CONSTRAINT] [外键名] FOREIGN KEY (外键字段名) REFERENCES 主表(主表列名)
);
#创表后添加外键
ALTER TABLE 表名 ADD CONSTRAINT 外键名 FOREIGN KEY (外键字段名) REFERENCES 主表(主表列名);示例代码如下:
# 字表
CREATE TABLE classinfo(
id int PRIMARY KEY AUTO_INCREMENT COMMENT '班级id',
name varchar(50) NOT NULL COMMENT '班级名'
)COMMENT '班级表';
# 主表
CREATE TABLE students(
id int PRIMARY KEY AUTO_INCREMENT COMMENT 'id',
name varchar(50) NOT NULL COMMENT '学生名',
password varchar(10) DEFAULT '123456' COMMENT '密码',
gender char(1) COMMENT '性别',
classid int NOT NULL COMMENT '班级id',
CONSTRAINT fk_students_classinfo_id FOREIGN KEY (classid) REFERENCES classinfo(id)
)COMMENT '班级表';如下图所示:


删除外键
删除外键语法格式如下:
ALTER TABLE 表名 DROP FOREIGN KEY 外键名;示例代码如下:
ALTER TABLE students DROP FOREIGN KEY fk_students_classinfo_id;如下图所示:

接下来我们通过另一种方法来添加外键,代码如下:
ALTER TABLE students ADD CONSTRAINT fk_student_classinfo FOREIGN KEY (classid) REFERENCES classinfo(id);如下图所示:

行为
在设置外键之后,就无法轻易地删除数据表中的数据,这是因为存在外键约束行为——删除/更新行为。
外键的删除/更新行为有:
| 行为 | 说明 |
|---|---|
| NO ACTION | 当在父表中删除/更新对应记录时,首先检查该记录是否存在对应外键,如果有则不允许删除/更新。 |
| RESTRICT | 当在父表中删除/更新对应记录时,首先检查该记录是否存在对应外键,如果有则不允许删除/更新。 |
| CASCADE | 当在父表中删除/更新对应记录时,首先检查该记录是否存在对应外键,如果有,则也删除/更新外键在子表中的记录。 |
| SET NULL | 当在父表中删除对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null(需要外键允许为null) |
| SET DEFAULT | 父表有变更时,子表将外键列设置成一个默认的值(Innodb不支持) |
为外键设置删除/更新行为语法格式如下:
ALTER TABLE 表名 ADD CONSTRAINT 外键名 FOREIGN KEY (外键字段) REFERENCES 主表名(主表字段) ON UPDATE 行为 ON DELETE 行为;示例代码如下:
ALTER TABLE students ADD CONSTRAINT fk_student_classinfo FOREIGN KEY (classid) REFERENCES classinfo(id) ON UPDATE CASCADE ON DELETE CASCADE;如下图所示:

注意:删除/更新行为要一致。
好了,MySQL基础——约束就学到这里了,下篇文章学习MySQL基础——多表查询。
边栏推荐
- 一个支持IPFS的电子邮件——SKIFF
- 数字经济加速落地,能为中小企业带来什么?
- University of Calgary | recommendation system based on Reinforcement Learning
- 完全背包如何考虑排列问题
- Heap sort (principle plus code)
- [compréhension approfondie de la technologie tcaplusdb] exploitation et entretien de tcaplusdb - inspection quotidienne des patrouilles
- 2019 年总结:31岁,不过是另一个开始
- 漫话Redis源码之一百二十一
- 自己写了一个telnet命令
- A text to show you the memory leak
猜你喜欢

三维天地助力实验室夯实完整质量体系管理

Possible security vulnerabilities in NFT

数字化转型的失败原因及成功之道

An error is reported when idea writes JSP code, but it is solved by normal operation

Tree, forest and transformation of binary tree

critical path
![[in depth understanding of tcapulusdb technology] introduction tcapulusdb problem summary](/img/2b/3ab5e247ac103728b4d3579c3c5468.png)
[in depth understanding of tcapulusdb technology] introduction tcapulusdb problem summary

【深入理解TcaplusDB技术】入门Tcaplus-JDBC开发
![[in depth understanding of tcaplus DB technology] getting started tcaplus SQL driver](/img/2b/3ab5e247ac103728b4d3579c3c5468.png)
[in depth understanding of tcaplus DB technology] getting started tcaplus SQL driver

510000 prize pool invites you to join the war! The second Alibaba cloud ECS cloudbuild developer competition is coming
随机推荐
Cloud computing in the metauniverse to enhance your digital experience
Three dimensional world helps the laboratory to consolidate the complete quality system management
Traversal of trees and forests
Security policy and NAT (easy IP) of firewall Foundation
[in depth understanding of tcaplus DB technology] Introduction to tcaplus JDBC development
Peking University - robust task representation for off-line meta reinforcement learning through contrastive learning
市场开始降温,对NFT 是坏事么?
完全背包如何考虑排列问题
iVX无代码挑战五秒游戏制作
Fibonacci search (golden section)
播放增长900w,B站用户叫好叫座的恰饭总结
[in depth understanding of tcapulusdb technology] how to take tcapulusdb off the shelf
软件上线前为什么要做性能测试?软件性能测试机构怎么找
[deeply understand tcapulusdb technology] tcapulusdb process
带超时的recv函数
JWT简介
Shell Sort
【深入理解TcaplusDB技术】入门MySQL Driver
MySQL高级(二)
【已解决】--go_out: protoc-gen-go: Plugin failed with status code 1.