Learn the constraints of MySQL tables from 0 (continuous update...)

Table of contents

1. Common constraints

2. Non-empty constraints

1. Add a non-null constraint

 2. Delete the not-null constraint

3. Unique constraints

1. Add a unique constraint

1.1. Direct add

2. delete

4. Primary key constraints

1. Add constraints

2. Delete the primary key 

5. Default value constraints

 6. Self-growth constraints

7. Check constraints



1. Common constraints

not-null constraint

unique constraint

primary key constraint

foreign key constraints

2. Non-empty constraints

The not null constraint defines whether a specific column of a data row in a table can be null

1. Add a non-null constraint

When creating the table add

create table table name (field name field type...) not null;

Add when modifying the data type of the field in the table

alter table table name modify field name new data type not null;

 2. Delete the not-null constraint

Delete when modifying the data type of the field in the table

 alter table table name change column field name new data type;

 alter table table name modify field name new data type;

3. Unique constraints

The unique constraint ensures that the combined values ​​of one or more columns in the table are unique and prevents duplicate values ​​from being entered. It is mainly used to ensure the integrity of non-primary key columns.

1. Add a unique constraint

1.1. Direct add

1.1.1 Table-level constraints

 alter table table name add unique(field name);

1.1.2 Joint uniqueness constraints

 alter table table name add unique(field name 1, field name 2); 

The combination of field name 1 and field name 2 is unique

For example, in the code: the same id cannot exist in the same class; 

2. delete

alter table table name drop index field name;

4. Primary key constraints

There can only be one primary key in a table. A primary key can consist of one or more columns.

A primary key constraint is equivalent to a combination of uniqueness and not-null constraints

 Add both a unique constraint and a non-null constraint to a field

1. Add constraints

Add primary key constraints on creation 

create table table name (field name field type primary key,...)

 Add when alter table

alter table table name add primary key (field name);

2. Delete the primary key 

alter table table name drop primary key;

5. Default value constraints

add on create

create table table name (field name field type default default value);

when modified

alter table table name modify field name field type default default value;

delete 

 6. Self-growth constraints

The auto-increment column must be a primary key column

If the auto-increment column specifies 0 and null, it will be auto-incremented based on the current maximum value

If the auto-increment column manually specifies a specific value, directly assign the value to the specific value

 when creating the table 

create table table name (field name field type primary key auto_increment);

 When modifying the data type of a field in a table

alter table table name modify field name field type auto_increment;

7. Check constraints

Check if a field meets the requirements  

alter table table name add check (field name between minimum and maximum); 

 

Guess you like

Origin blog.csdn.net/GANTENJ/article/details/127370658