MySQL constraints
Concept: Limit the data in the table to ensure the correctness, validity and completeness of the data .
classification:
- Non-empty constraint: not null
- Unique constraint: unique
- Primary key constraint: primary key
- Foreign key constraints: foreign key
- Default constraint: Default
- Check constraints: Check
Non-empty constraint: not null
The value of a column cannot be empty
-
Add constraints when creating a table
CREATE TABLE stu( id INT, NAME VARCHAR(20) NOT NULL -- name为非空 );
-
After creating the table, add a non-empty constraint
ALTER TABLE stu MODIFY NAME VARCHAR(20) NOT NULL;
3. Delete the name of non-empty constraint
ALTER TABLE stu MODIFY NAME VARCHAR(20);
The only constraint: unique
The value of a column cannot be repeated
note:
- The unique constraint can have a NULL value, but only one record can be null
-
When creating a table, add a unique constraint
CREATE TABLE stu( id INT, phone_number VARCHAR(20) UNIQUE -- 手机号 );
-
After the table is created, add a unique constraint
ALTER TABLE stu MODIFY phone_number VARCHAR(20) UNIQUE;
-
Delete unique constraints (different from non-empty constraints)
ALTER TABLE stu DROP INDEX phone_number;
Primary key constraint: primary key
Non-empty and unique
Note:
2. A table can only have one field as the primary key
3. The primary key is the unique identifier of the records in the table
-
When creating a table, add a primary key constraint
create table stu( id int primary key,-- 给id添加主键约束 name varchar(20) );
-
After creating the table, add the primary key
ALTER TABLE stu MODIFY id INT PRIMARY KEY;
-
Delete primary key
ALTER TABLE stu DROP PRIMARY KEY;
-
Automatic growth:
**Note: **Auto growth is based on the value of the previous data. The data set for automatic growth can also be assigned actively.
-
Concept: If a column is numeric type , the use
auto_increment
can be done automatically worth growth (and typically along with primary key) -
When creating a table, add a primary key constraint, and complete the primary key self-growth
create table stu( id int primary key auto_increment,-- 给id添加主键约束 name varchar(20) );
-
Add automatic growth
ALTER TABLE stu MODIFY id INT AUTO_INCREMENT;
-
Delete automatic growth
ALTER TABLE stu MODIFY id INT;
Foreign key constraint: foreign key
Let the table have a relationship with the table to ensure the correctness of the data.
- When creating a table, you can add foreign keys
create table 表名(
....
外键列
constraint 约束(外键)名称 foreign key (外键列名称) references 主表名称(主表列名称)
);
- Delete foreign key
ALTER TABLE 表名 DROP FOREIGN KEY 外键名称;
- After creating the table, add the foreign key
ALTER TABLE 表名 ADD CONSTRAINT 约束(外键)名称 FOREIGN KEY (外键字段名称) REFERENCES 主表名称(主表列名称);
- Cascade operation
- Add cascade operation
ALTER TABLE 表名 ADD CONSTRAINT 外键名称
FOREIGN KEY (外键字段名称) REFERENCES 主表名称(主表列名称)
ON UPDATE CASCADE ON DELETE CASCADE ;
- Classification:
1. Cascade update:ON UPDATE CASCADE
2. Cascade delete:ON DELETE CASCADE
(use with caution )
Default constraint: Default
Set the default value of the attribute, when it is not defined, the default value is null
-
When creating a table, you can add default constraints
CREATE TABLE Persons ( Id int NOT NULL, Name varchar(255) DEFAULT '张三' );
-
After creating the table, add default constraints
ALTER TABLE Persons ALTER Name SET DEFAULT '张三';
-
Remove default constraints
ALTER TABLE Persons ALTER Name DROP DEFAULT
Check constraint: Check
Used to limit the range of values in the column
-
When creating a table, add check constraints
Set constraint name
CREATE TABLE Persons ( Id int NOT NULL, Name varchar(255) NOT NULL, Address varchar(255), City varchar(255), CONSTRAINT chk_Person CHECK (Id>0 AND City='北京') );
Do not set constraint name
CREATE TABLE Persons ( Id int NOT NULL, Name varchar(255) NOT NULL, Address varchar(255), City varchar(255), CHECK (Id>0) );
-
After creating the table, add check constraints
Set constraint name
ALTER TABLE Persons ADD CONSTRAINT chk_Person CHECK (Id>0 AND City='北京');
Do not set constraint name
ALTER TABLE Persons ADD CHECK (Id>0);
-
Delete check constraint
ALTER TABLE Persons DROP CHECK chk_Person;