Detailed explanation of database constraints

1. Overview of constraints

1. Concept: Constraints are rules that act on fields in a table to limit the data stored in the table.

2. Purpose: To ensure the accuracy, validity and integrity of the data in the database.

3. Classification:

constraint describe Keywords
non-null constraint Restrict the data of this field to not be null NOT NULL
unique constraint Ensure that all data in this field are unique and non-duplicate UNIQUE
primary key constraints The primary key is the unique identifier of a row of data and must be non-empty and unique. PRIMARY KEY
Default constraints When saving data, if the value of this field is not specified, the default value is used DEFAULT
Check constraints (after version 8.0.1) Ensure that the field value meets a certain condition CHECK
foreign key constraints Used to establish a connection between the data of the two graphs to ensure the consistency and integrity of the data. FOREIGN KEY

Constraints are applied to the fields in the table. Constraints can be added when creating/modifying the table. 

Common constraints:

Restrictions Keywords
primary key PRIMARY KEY
auto-grow AUTO_INCREMENT
not null NOT NULL
only UNIQUE
logical condition CHECK
default value DEFAULT

2. Cases of restraint 

The table creation statement is as follows:

create table user(
     id int primary key auto_increment comment  '主键',

     name  varchar(10) not null unique comment  '姓名',

     age int  check ( age > 0 && age <= 120 ) comment  '年龄',

     status char(1) default '1'comment 'status',

     gender char(1) comment 'Gender'
) comment 'User table;
 

3. Foreign key constraints

Concept:
        Foreign keys are used to establish a connection between the data of two tables, thereby ensuring the consistency and integrity of the data.


 Case:

The dept_id (department id) of the employee table emp corresponds to the id of the department table dept, where dept is the main table

alter table emp add constraint fk_emp_dept_id foreign key(dept_id) references dept(id);

  

4. Summary

MySQL database provides multiple types of constraints to ensure data integrity and consistency. The following are some common MySQL database constraints:

  1. PRIMARY KEY: A primary key is a column or column combination used to uniquely identify each row in a table. It must be unique and null values ​​are not allowed.

  2. Unique constraint (UNIQUE): A unique constraint is used to ensure that the value of a certain column or column combination is unique in the table, that is, duplicate values ​​are not allowed. Unlike primary keys, unique constraints can allow null values.

  3. Foreign key constraints (FOREIGN KEY): Foreign keys are used to establish relationships between tables. It specifies one or more columns as primary keys that reference another table to ensure referential integrity. Foreign key constraints can prevent invalid data from being inserted and can also perform cascading operations on related tables.

  4. Non-null constraint (NOT NULL): The non-null constraint is used to restrict a column from storing null values. It ensures that the column must always contain valid data.

  5. Default constraint (DEFAULT): The default constraint is used to define the default value that a column will automatically use when no value is explicitly specified.

  6. Check constraint (CHECK): A check constraint is used to define a logical expression to determine the range of values ​​allowed when inserting or updating data. If the expression evaluates to false, the insert or update operation is rejected.

These constraints can be defined when creating the table or modified on an existing table. They can help maintain data integrity and provide stronger data verification and protection mechanisms. By using appropriate constraints, you can ensure that the data in your database behaves as expected and reduce the risk of errors and data inconsistencies.

Guess you like

Origin blog.csdn.net/weixin_55772633/article/details/132120659