当前位置:网站首页>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
边栏推荐
- PHP read file (read the specified line containing a string in the file)
- Zzuli: maximum Convention and minimum common multiple
- Visual studio import
- PHP obtains some values in the string according to the specified characters, and reorganizes the remaining strings into a new array
- "Simple" infinite magic cube
- 数据库学习总结5
- 如何写出好代码 — 防御式编程指南
- Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
- Basic use of form
- vite如何兼容低版本浏览器
猜你喜欢
Win10 copy files, save files... All need administrator permission, solution
Visual Studio导入
OLED12864 液晶屏
500. 键盘行
Alibaba: open source and self-developed liquid cooling data center technology
【論文翻譯】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
Vscode paste image plugin saves image path settings
GRBL 软件:简单解释的基础知识
Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
Centos8 installation mysql8.0.22 tutorial
随机推荐
Technologists talk about open source: This is not just using love to generate electricity
php内类名称与类内方法名相同
PHP inner class name is the same as the inner class method name
DRM display framework as I understand it
Online music player app
[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
我所理解的DRM显示框架
Zzuli:1069 learn from classmate Z
Huawei Hongmeng OS, is it OK?
RGB 无限立方体(高级版)
Lantern Festival gift - plant vs zombie game (realized by Matlab)
深度学习分类网络--Network in Network
2022-2-14 learning xiangniuke project - Section 6 displays login information
Zzuli:1068 binary number
TypeScript的泛型和泛型约束
XSS basic content learning (continuous update)
Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash
460. LFU cache bidirectional linked list
PHP array to XML
mysql的约束总结