当前位置:网站首页>MySQL foundation - constraints
MySQL foundation - constraints
2022-06-22 20:23:00 【White chocolate x】
Catalog
In the last article, we learned MySQL Basics —— function , This article is for us to learn MySQL Basics —— constraint .
constraint
Constraints are rules that act on fields in a table , Used to limit the data stored in a table , Its purpose is to ensure that the data in the database is correct 、 Effectiveness and integrity .
Common constraints are as follows :
| constraint | describe | keyword |
|---|---|---|
| Non empty constraint | Limit the data of this field to null | NOT NULL |
| Unique constraint | Ensure that all data in this field is unique 、 Not repeated | UNIQUE |
| Primary key constraint | A primary key is the unique identifier of a row of data , It is required to be non empty and unique | PRIMAPY KEY |
| Default constraint | When saving data , When the value of this field is not specified , The default value is used | DEFAULT |
| Detection constraints (8.0 After the version ) | Ensure that the field value meets a certain condition | CHECK |
| Foreign key constraints | Make a connection between the data of two tables , Ensure data consistency and integrity | FOREIGN KEY |
Be careful : Constraints act on fields in a table , You can create a table / Add constraints when modifying tables .
Case study
Next, we use a case to demonstrate how to set constraints when creating a data table .
Case requirements are as follows :
| Field name | Field meaning | Field type | constraint condition | Constraint keywords |
|---|---|---|---|---|
| id | ID Unique identification | int | Primary key , And grow automatically | PRIMARY KEY,AUTO_INCREMENT |
| name | full name | varchar(10) | Not empty and unique | NOT NULL,UNIQUE |
| passwrod | password | varchar(10) | The default value is 123456 | DEFAULT |
| gender | Gender | char(1) | nothing |
The sample code is as follows :
CREATE TABLE students(
id int PRIMARY KEY AUTO_INCREMENT COMMENT ' user id',
name varchar(10) NOT NULL UNIQUE COMMENT ' full name ',
password varchar(10) DEFAULT '123456' COMMENT ' password ',
gender char(1) COMMENT ' Gender '
)COMMENT ' Student list ';As shown in the figure below :

This will successfully create constraints for fields when creating tables .
Foreign key constraints
Foreign keys are used to connect the data of two tables , So as to ensure the consistency and integrity of data .
Add foreign keys
Add foreign key syntax format as follows :
# Add foreign keys when creating tables
CREATE TABLE Table name (
Field name data type ,
...
[CONSTRAINT] [ Foreign key name ] FOREIGN KEY ( Foreign key field name ) REFERENCES Main table ( Name of main table )
);
# Add a foreign key after creating a table
ALTER TABLE Table name ADD CONSTRAINT Foreign key name FOREIGN KEY ( Foreign key field name ) REFERENCES Main table ( Name of main table );The sample code is as follows :
# Word list
CREATE TABLE classinfo(
id int PRIMARY KEY AUTO_INCREMENT COMMENT ' class id',
name varchar(50) NOT NULL COMMENT ' Class name '
)COMMENT ' Class table ';
# Main table
CREATE TABLE students(
id int PRIMARY KEY AUTO_INCREMENT COMMENT 'id',
name varchar(50) NOT NULL COMMENT ' Student name ',
password varchar(10) DEFAULT '123456' COMMENT ' password ',
gender char(1) COMMENT ' Gender ',
classid int NOT NULL COMMENT ' class id',
CONSTRAINT fk_students_classinfo_id FOREIGN KEY (classid) REFERENCES classinfo(id)
)COMMENT ' Class table ';As shown in the figure below :


Delete foreign key
The syntax format for deleting foreign keys is as follows :
ALTER TABLE Table name DROP FOREIGN KEY Foreign key name ;The sample code is as follows :
ALTER TABLE students DROP FOREIGN KEY fk_students_classinfo_id;As shown in the figure below :

Next, we add foreign keys in another way , The code is as follows :
ALTER TABLE students ADD CONSTRAINT fk_student_classinfo FOREIGN KEY (classid) REFERENCES classinfo(id);As shown in the figure below :

Behavior
After setting the foreign key , You can't easily delete the data in the data table , This is because there is a foreign key constraint behavior —— Delete / Update behavior .
Foreign key deletion / Update behaviors are :
| Behavior | explain |
|---|---|
| NO ACTION | When deleting in the parent table / When updating the corresponding record , First, check whether the record has a corresponding foreign key , If there is, it is not allowed to delete / to update . |
| RESTRICT | When deleting in the parent table / When updating the corresponding record , First, check whether the record has a corresponding foreign key , If there is, it is not allowed to delete / to update . |
| CASCADE | When deleting in the parent table / When updating the corresponding record , First, check whether the record has a corresponding foreign key , If there is , Then delete / Update the record of the foreign key in the sub table . |
| SET NULL | When deleting the corresponding record in the parent table , First, check whether the record has a corresponding foreign key , If yes, set the foreign key value in the sub table to null( Foreign keys are required and allowed to be null) |
| SET DEFAULT | When the parent table changes , The sub table sets the foreign key column to a default value (Innodb I won't support it ) |
Set delete for foreign keys / The update behavior syntax format is as follows :
ALTER TABLE Table name ADD CONSTRAINT Foreign key name FOREIGN KEY ( Foreign key field ) REFERENCES Main table name ( Main table fields ) ON UPDATE Behavior ON DELETE Behavior ;The sample code is as follows :
ALTER TABLE students ADD CONSTRAINT fk_student_classinfo FOREIGN KEY (classid) REFERENCES classinfo(id) ON UPDATE CASCADE ON DELETE CASCADE;As shown in the figure below :

Be careful : Delete / The update behavior should be consistent .
Okay ,MySQL Basics —— That's where constraints come in , Next article study MySQL Basics —— Multi-table query .
边栏推荐
- Interpolation lookup and half (bisection) lookup
- [deeply understand tcapulusdb technology] create a game zone
- [petty bourgeoisie database] break down the concept: data, database, database system, database management system, database technology
- 【深入理解TcaplusDB技术】TcaplusDB 表管理——新建表
- Teach you how to create SSM project structure in idea
- Storage structure of graph (adjacency matrix)
- Random talk on redis source code 119
- 带超时的recv函数
- [in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance
- Latest download address of libcef - compiled as MD dynamic link under vs2015
猜你喜欢
随机推荐
Classic interview question: a page from entering URL to rendering process
Introduction of Neural Network (BP) in Intelligent Computing
Comment le sac à dos complet considère - t - il la disposition?
Three dimensional world helps the laboratory to consolidate the complete quality system management
天,靠八股文逆袭了啊
[deeply understand tcapulusdb technology] create a game area for document acceptance
Topological sorting
socket的connect函数用法
Using span method to realize row merging of multi-layer table data
一张图解码 OpenCloudOS 社区开放日
Bubble sort, select sort, direct insert sort
Huffman tree (C language)
Shell编程基础(第七篇:分支语句-if)
【深入理解TcaplusDB技术】入门MySQL Driver
Precautions for Apollo use
Possible security vulnerabilities in NFT
完全背包如何考虑排列问题
Recv function with timeout
[deeply understand tcapulusdb technology] realize tcapulusdb transaction management in the operation and maintenance platform
【深入理解TcaplusDB技术】TcaplusDB运维









