MySQL constraint non-empty constraint, unique constraint, primary key constraint, foreign key constraint, default constraint, check constraint

MySQL constraints

Concept: Limit the data in the table to ensure the correctness, validity and completeness of the data .

classification:

  1. Non-empty constraint: not null
  2. Unique constraint: unique
  3. Primary key constraint: primary key
  4. Foreign key constraints: foreign key
  5. Default constraint: Default
  6. Check constraints: Check

Non-empty constraint: not null

The value of a column cannot be empty

  1. Add constraints when creating a table

    CREATE TABLE stu(
    	id INT,
    	NAME VARCHAR(20) NOT NULL -- name为非空
    );
    
  2. 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
  1. When creating a table, add a unique constraint

    CREATE TABLE stu(
    	id INT,
    	phone_number VARCHAR(20) UNIQUE -- 手机号
    );
    
  2. After the table is created, add a unique constraint

    ALTER TABLE stu MODIFY phone_number VARCHAR(20) UNIQUE;
    
  3. 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

  1. When creating a table, add a primary key constraint

    create table stu(
    	id int primary key,-- 给id添加主键约束
     name varchar(20)
    );
    
  2. After creating the table, add the primary key

    ALTER TABLE stu MODIFY id INT PRIMARY KEY;
    
  3. Delete primary key

    ALTER TABLE stu DROP PRIMARY KEY;
    
  4. 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_incrementcan 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.

  1. When creating a table, you can add foreign keys
create table 表名(
	....
	外键列
	constraint 约束(外键)名称 foreign key (外键列名称) references 主表名称(主表列名称)
);
  1. Delete foreign key
ALTER TABLE 表名 DROP FOREIGN KEY 外键名称;
  1. After creating the table, add the foreign key
ALTER TABLE 表名 ADD CONSTRAINT 约束(外键)名称 FOREIGN KEY (外键字段名称) REFERENCES 主表名称(主表列名称);
  1. Cascade operation
  2. Add cascade operation
  ALTER TABLE 表名 ADD CONSTRAINT 外键名称 
  					FOREIGN KEY (外键字段名称) REFERENCES 主表名称(主表列名称) 
  									ON UPDATE CASCADE ON DELETE CASCADE  ;
  1. 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

  1. When creating a table, you can add default constraints

    CREATE TABLE Persons
    (
    Id int NOT NULL,
    Name varchar(255)  DEFAULT '张三'
    );
    
  2. After creating the table, add default constraints

    ALTER TABLE Persons
    ALTER Name SET DEFAULT '张三';
    
  3. Remove default constraints

    ALTER TABLE Persons
    ALTER Name DROP DEFAULT
    

Check constraint: Check

Used to limit the range of values ​​in the column

  1. 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)
    );
    
  2. 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);
    
  3. Delete check constraint

    ALTER TABLE Persons DROP CHECK chk_Person;
    

Guess you like

Origin blog.csdn.net/ren9436/article/details/107718524