当前位置:网站首页>Summary of MySQL constraints
Summary of MySQL constraints
2022-07-02 05:51:00 【Zebra!】
Catalog
What are constraints ?
In order to ensure the consistency and integrity of data ,SQL The specification imposes additional constraints on table data in a constrained manner .
Constraints are mandatory at the table level .
Constraints can be specified when creating tables (create table sentence ), Or after the table is created ( adopt alter table sentence ).
Constraint classification
1) General classification
not null Non empty constraint , Specifies that a field cannot be empty ;
unique Unique constraint , Specify that a field is unique in the whole table ;
primary key Primary key ( Non empty and unique )
foreign key Foreign keys
check Check constraint (mysql I won't support it check constraint , But you can use check constraint , Without any effect )
default The default value is .
2) According to the restriction of the constraint column , Constraints can be divided into
Single column constraints
Multi column constraint
3) According to the constraint range
Column level constraints : It can only act on one column , Follow the definition of the column ;
Table level constraints : Can act on multiple columns , Not with columns , It is defined separately .
Restrict the use of
not null constraint
The non empty constraint is used to ensure that the value of the current column is not empty , Non empty constraints can only appear on columns of table objects ;
null Characteristics of the type : All types of values can be null, Include int,float And so on
An empty string “” and 0 None of them is equal to null.
establish not null constraint :

increase not null constraint

Cancel null constraint

unique constraint
Unique constraint , Multiple null values are allowed .
The same table can have multiple unique constraints , Constraints of multiple column combinations .
When creating unique constraints , If you don't give a unique constraint name , The default is the same as the column name ;
mysql A unique index will be created by default for columns with unique constraints
Create a unique constraint

Add unique constraints

Delete constraints
primary key constraint
A primary key constraint is equivalent to a unique constraint + A combination of nonnull constraints , Primary key constraint cannot be duplicated , Null values are not allowed .
If it is the primary key constraint of multi column combination , Then none of these columns are allowed to be null , And the combined value is not allowed to repeat ;
Only one primary key is allowed per table , You can create a primary key constraint at the column level , You can also create... At the table level ;
mysql The primary key name of is always primary, When creating a primary key constraint , By default, the system will establish a unique index on the column and column combination .
Create constraints

Delete primary key constraint
alter table dep drop primary key;
Add primary key constraint
alter table dep add primary key(name, pwd);
Modify the primary key constraint
alter table dep modify id int primary key;
foreign key
A foreign key constraint guarantees referential integrity between one or two tables , A foreign key is a reference relationship between two fields of a table or two fields of two tables .
The foreign key value of the slave table must be found in the master table or empty . When the records of the master table are referenced by the slave table , Records in the main table will not be allowed to be deleted , If you want to delete data , You need to delete the data that depends on this record from the table first , Then you can delete the data of the main table .
Reference column of foreign key constraint , Only primary key or unique key constrained columns can be referenced in the main table .
The same table can have multiple foreign key constraints .
Create foreign key constraints

Create a multi column foreign key combination , Table level constraints must be used

Delete foreign key constraint

Add foreign key constraints

foreign key Keywords for constraints

边栏推荐
- Can't the dist packaged by vite be opened directly in the browser
- Technologists talk about open source: This is not just using love to generate electricity
- How to change the IP address of computer mobile phone simulator
- Centos8 installation mysql8.0.22 tutorial
- Zzuli:1061 sequential output of digits
- 2022-2-15 learning xiangniuke project - Section 8 check login status
- A collection of commonly used plug-ins for idea development tools
- 数据挖掘方向研究生常用网站
- Balsamiq wireframes free installation
- With an amount of $50billion, amd completed the acquisition of Xilinx
猜你喜欢

Vite打包后的dist不能直接在浏览器打开吗

Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan

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

数理统计与机器学习

A collection of commonly used plug-ins for idea development tools

Reading notes of cgnf: conditional graph neural fields

Record sentry's path of stepping on the pit

Practice C language advanced address book design

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

Fundamentals of software testing
随机推荐
Installation du tutoriel MySQL 8.0.22 par centos8
文件包含漏洞(二)
3D printer G code command: complete list and tutorial
【論文翻譯】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
测试 - 用例篇
RGB 无限立方体(高级版)
Win10 copy files, save files... All need administrator permission, solution
Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea
Several keywords in C language
Reading notes of cgnf: conditional graph neural fields
Applet jumps to official account
[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP
软件测试答疑篇
15 C language advanced dynamic memory management
Get the details of the next largest number
我所理解的DRM显示框架
php内类名称与类内方法名相同
Zzuli:1064 encrypted characters
深度学习分类网络--Network in Network