当前位置:网站首页>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约束的关键字

边栏推荐
- Gee dataset: chirps pentad high resolution global grid rainfall dataset
- Centos8 installation mysql8.0.22 tutorial
- Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]
- I want to understand the swift code before I learn it. I understand it
- RGB infinite cube (advanced version)
- 7. Eleven state sets of TCP
- Visual studio import
- 1035 Password
- Youth training camp -- database operation project
- Go language web development is very simple: use templates to separate views from logic
猜你喜欢

6.网络-基础

RGB 无限立方体(高级版)

【技术随记-08】

MySQL foundation --- query (learn MySQL foundation in 1 day)

"Simple" infinite magic cube

Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?

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

Gee: create a new feature and set corresponding attributes

Gee dataset: chirps pentad high resolution global grid rainfall dataset

Gee series: Unit 1 Introduction to Google Earth engine
随机推荐
Lantern Festival gift - plant vs zombie game (realized by Matlab)
[technical notes-08]
Taskbar explicit / implicit toggle function
Online English teaching app open source platform (customized)
ERP management system development and design existing source code
Matplotlib double Y axis + adjust legend position
Common protocols and download paths of NR
brew install * 失败,解决方法
Storage of data
Straighten elements (with transition animation)
Gee data set: export the distribution and installed capacity of hydropower stations in the country to CSV table
The Hong Kong Stock Exchange learned from US stocks and pushed spac: the follow-up of many PE companies could not hide the embarrassment of the world's worst stock market
RNN recurrent neural network
Database batch insert data
centos8安裝mysql8.0.22教程
2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions
Ls1046nfs mount file system
Alibaba: open source and self-developed liquid cooling data center technology
JVM class loading mechanism
1037 Magic Coupon