当前位置:网站首页>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约束的关键字
边栏推荐
- Pytorch Chinese document
- Thread pool batch processing data
- XSS basic content learning (continuous update)
- H5 jump applet
- Matplotlib double Y axis + adjust legend position
- 小程序跳装到公众号
- Basic use of form
- Youth training camp -- database operation project
- 1036 Boys vs Girls
- Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
猜你喜欢
Grbl software: basic knowledge of simple explanation
Balsamiq wireframes free installation
[personal test] copy and paste code between VirtualBox virtual machine and local
Pytorch Basics
青训营--数据库实操项目
“簡單”的無限魔方
Storage of data
Win10 copy files, save files... All need administrator permission, solution
6. Network - Foundation
[technical notes-08]
随机推荐
中小型项目手撸过滤器实现认证与授权
OLED12864 液晶屏
Applet jumps to official account
KMP idea and template code
Brew install * failed, solution
Cube magique infini "simple"
Centos8 installation mysql8.0.22 tutorial
Fabric. JS iText sets the color and background color of the specified text
Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
[technical notes-08]
Thread pool overview
指针使用详解
Alibaba: open source and self-developed liquid cooling data center technology
I want to understand the swift code before I learn it. I understand it
Gee: find the spatial distribution and corresponding time of the "greenest" in the Yellow River Basin in 2020 [pixel by pixel analysis]
[personal test] copy and paste code between VirtualBox virtual machine and local
RGB infinite cube (advanced version)
MySQL foundation --- query (learn MySQL foundation in 1 day)
Disable access to external entities in XML parsing