当前位置:网站首页>mysql的约束总结
mysql的约束总结
2022-07-02 05:40:00 【斑马!】
目录
什么是约束?
为了保证数据的一致性和完整性,SQL规范以约束的方式对表数据进行额外的条件限制。
约束是表级的强制规定。
可以在创建表时候规定约束(create table语句),或者在表创建之后也可以(通过alter table 语句)。
约束分类
1)一般分类
not null 非空约束,规定某个字段不能为空;
unique 唯一约束,规定某个字段在整个表中是唯一的;
primary key 主键(非空且唯一)
foreign key 外键
check 检查约束(mysql不支持check约束,但可以使用check约束,而没有任何效果)
default 默认值。
2)根据约束列的限制,约束可以分为
单列约束
多列约束
3)根据约束范围
列级约束:只能作用于在一个列上,跟在列的定义后面;
表级约束:可以作用于在多个列上,不与列一起,而是单独定义。
约束使用
not null约束
非空约束用于确保当前列的值不为空,非空约束只能出现在表对象的列上;
null类型的特点:所有类型的值都可以是null,包括int,float等数据类型
空字符串“”和0都不等于null。
创建not null约束:

增加not null约束

取消null约束

unique约束
唯一约束,允许出现多个空值。
同一个表可以有多个唯一约束,多个列组合的约束。
在创建唯一约束的时候,如果不给唯一约束名称,就默认和列名相同;
mysql会给唯一约束的列上默认创建一个唯一索引
创建唯一约束

添加唯一约束

删除约束
primary key约束
主键约束相当于唯一约束+非空约束的组合,主键约束不允许重复,也不允许出现空值。
如果是多列组合的主键约束,那么这些列都不允许为空值,并且组合的值不允许重复;
每个表最多只允许一个主键,建立主键约束可以在列级别创建,也可以在表级别上创建;
mysql的主键名总是primary,当创建主键约束时候,系统默认会在所在的列和列组合上建立对应的唯一索引。
创建约束

删除主键约束
alter table dep drop primary key;
添加主键约束
alter table dep add primary key(name, pwd);
修改主键约束
alter table dep modify id int primary key;
foreign key
外键约束是保证一个或两个表之间的参照完整性,外键是构建于一个表的两个字段或者是两个表的两个字段之间的参照关系。
从表的外键值必须在主表中能找到或者为空。当主表的记录被从表参照时候,主表的记录将不允许删除,如果要删除数据,需要先删除从表中依赖该记录的数据,然后才可以删除主表的数据。
外键约束的参照列,在主表中引用的只能是主键或者唯一键约束的列。
同一个表可以有多个外键约束。
创建外键约束

创建多列外键组合,必须使用表级约束

删除外键约束

增加外键约束

foreign key约束的关键字

边栏推荐
- Using QA band and bit mask in Google Earth engine
- Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
- Online English teaching app open source platform (customized)
- Fabric. JS round brush
- ERP management system development and design existing source code
- Foreign trade marketing website system development function case making
- 1036 Boys vs Girls
- Fabric. JS free draw rectangle
- 操作符详解
- How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure
猜你喜欢

Alibaba: open source and self-developed liquid cooling data center technology

Grbl software: basic knowledge of simple explanation

Ls1046nfs mount file system
![Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]](/img/25/a726643b96b6f7dbfee3782c4905d9.jpg)
Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]

Pytorch Basics

Fabric. JS iText sets the color and background color of the specified text

JVM class loading mechanism

idea开发工具常用的插件合集汇总
![Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]](/img/78/a17034d4b77d5c0dbe741f84a8ecd7.jpg)
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]

青训营--数据库实操项目
随机推荐
all3dp. All Arduino projects in com website (2022.7.1)
Balsamiq wireframes free installation
Zzuli:1064 encrypted characters
Disable access to external entities in XML parsing
Importation de studio visuel
all3dp.com网站中全部Arduino项目(2022.7.1)
1036 Boys vs Girls
Technologists talk about open source: This is not just using love to generate electricity
Youth training camp -- database operation project
“簡單”的無限魔方
How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure
Applet jumps to official account
idea开发工具常用的插件合集汇总
Generate QR code
idea開發工具常用的插件合集匯總
Fabric. JS iText set italics manually
Brew install * failed, solution
Ls1046nfs mount file system
Get the details of the next largest number
Fabric. JS centered element