当前位置:网站首页>MySQL foundation - constraints

MySQL foundation - constraints

2022-06-22 20:23:00 White chocolate x

Catalog

constraint

Case study

Foreign key constraints

Add foreign keys

Delete foreign key

Behavior


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 nullNOT 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
idID 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 123456DEFAULT
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 .

原网站

版权声明
本文为[White chocolate x]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221853583942.html