Table of contents
Graphical interface constraints
Foreign key delete/update behavior
Overview
- Concept: Constraints are rules that act on fields in a table to limit the data stored in the table.
- Purpose: To ensure the accuracy, validity and integrity of the data in the database.
- Classification:
constraint | describe | Keywords |
---|---|---|
non-null constraint | The data in this field cannot 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.16) | Save field values that meet a certain condition | CHECK |
foreign key constraints | Used to establish a connection between the data of the two tables to ensure the consistency and integrity of the data. | FOREIGN KEY |
Constraint demonstration
Table creation
Complete the creation of the form as required:
Field name | Field meaning | Field Type | Restrictions | constraint keyword |
---|---|---|---|---|
id | ID unique identification | int | Primary key, and automatically grows | PRIMARY KEY,AUTO_INCREMENT |
name | Name | varchar(10) | Not empty and unique | NOT NULL,UNIQUE |
age | age | int | Greater than 0 and less than or equal to 120 | CHECK |
status | state | char(1) | If this value is not specified, it defaults to 1 | DEFAULT |
gender | gender | char(1) | none |
Automatic growth: the id increases automatically with the number of rows in the table, the keyword is auto_increment
create table user(
id int primary key auto_increment comment '主键',
name varchar(10) not null unique comment '姓名',
age int check(age > 0 and age <= 120) comment '年龄',
status char(1) default '1' comment '状态',
gender char(1) comment '性别'
) comment '用户表';
verify
1. Primary key and automatic growth
When we insert data, we do not enter the ID
create table user(
id int primary key auto_increment comment '主键',
name varchar(10) not null unique comment '姓名',
age int check(age > 0 and age <= 120) comment '年龄',
status char(1) default '1' comment '状态',
gender char(1) comment '性别'
) comment '用户表';
At this time, we found that id is used as the primary key and has automatic growth constraints. It will automatically increase as the number of rows increases, and we do not need to enter it.
2. The name is not empty and unique
When our name input is empty, an error will be reported; and when the name is repeated, after applying for the primary key for the inserted value, it will not be actually inserted, so the id value will jump by 1. (For example: 1, 2,3,5)
3. Age must be greater than 0 and less than or equal to 120
4. When the status is not specified, the default is 1
insert into user (name,age,gender)
values ('Jack6',18,'男');
Graphical interface constraints
foreign key constraints
concept
Foreign keys are used to establish a connection between data in two tables to ensure data consistency and integrity.
Note: Currently, the above two tables have not established foreign key relationships at the database level, so the consistency and integrity of the data cannot be guaranteed.
In other words, if the R&D department in the department table is deleted, the employees of the R&D department in the employee table will not be deleted, causing data integrity problems.
grammar
Add foreign key
CREATE TABLE 表名(
字段名 数据类型,
...
[CONSTRAINT] [外键名称] FOREIGN KEY (外键字段名) REFERENCES 主表 (主列表名)
);
-- 或
ALTER TABLE 表名
ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名) REFERENCE 主表 (主列表名);
Delete foreign key
ALTER TABLE 表名 DROP FOREIGN KEY 外键名称;
Example
alter table emp
add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);
alter table emp drop foreign key fk_emp_dept_id;
Foreign key delete/update behavior
Behavior | illustrate |
---|---|
NO ACTION | When deleting/updating the corresponding record in the parent table, first check whether the record has a corresponding foreign key, and if so, deletion/update is not allowed. (Same as RESTRICT) |
RESTRICT | When deleting/updating the corresponding record in the parent table, first check whether the record has a corresponding foreign key, and if so, deletion/update is not allowed. (Consistent with NO ACTION) |
CASCADE | When deleting/updating the corresponding record in the parent table, first check whether the record has a corresponding foreign key, and if so, delete/update the record with the foreign key in the child table. |
SET NULL | When deleting the corresponding record in the parent table, first check whether the record has a corresponding foreign key. If so, set the foreign key value in the child table to null (requires that the foreign key allows null) |
SET DEFAULT | When the parent table changes, the child table sets the foreign key column to a default value (Innodb does not support this) |
ALTER TABLE 表名
ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名) REFERENCE 主表 (主列表名)
ON UPDATE CASCADE ON DELETE CASCADE;
Demo
alter table emp
add constraint fk_emp_dept_id foreign key (dept_id) references dept(id)
on update cascade on delete cascade ;
At this time, change the id of the main table R&D department to 6:
The subtable is also updated:
Summarize
- Non-null constraint: NOT NULL
- Unique constraint: UNIQUE
- Primary key constraint: PRIMARY KEY (increment: AUTO_INCREMENT)
- Default constraint: DEFAULT
- Check constraints: CHECK
- Foreign key: FOREIGN KEY
end
Learn from: Dark Horse Programmer - MySQL Database Course