当前位置:网站首页>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

边栏推荐
- “简单”的无限魔方
- token过期自动续费方案和实现
- RGB infinite cube (advanced version)
- 数理统计与机器学习
- “簡單”的無限魔方
- 2022-2-14 learning xiangniuke project - Section 7 account setting
- LCD之MIPI协议的一些说明
- [PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
- Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
- 文件包含漏洞(二)
猜你喜欢
随机推荐
c语言中的几个关键字
[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
RGB 无限立方体(高级版)
Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
GRBL 软件:简单解释的基础知识
RGB infinite cube (advanced version)
如何写出好代码 — 防御式编程指南
Fundamentals of software testing
Huawei Hongmeng OS, is it OK?
Spark概述
来啦~ 使用 EasyExcel 导出时进行数据转换系列新篇章!
brew install * 失败,解决方法
Practice C language advanced address book design
RNN recurrent neural network
Centos8 installation mysql8.0.22 tutorial
软件测试 - 概念篇
460. LFU cache bidirectional linked list
Reading notes of cgnf: conditional graph neural fields
3D printer G code command: complete list and tutorial
php内的addChild()、addAttribute()函数








