Home  >  Article  >  Database  >  What foreign key constraint levels does mysql have?

What foreign key constraint levels does mysql have?

WBOY
WBOYforward
2023-05-30 16:25:19873browse

1. Cascade method.

When update/delete records are on the master table, synchronize update/delete to delete matching records from the slave table.

2. Set null method.

When the update/delete record is on the main table, the column of the matching record on the slave table is set to null, but it should be noted that the foreign key column of the child table cannot be notnull.

3. No action method.

If there are matching records in the child table, update/delete operations on candidate keys corresponding to the parent table are not allowed.

4. Restrict method, the same as no action.

Check foreign key constraints immediately.

5. In Set default mode, blank spaces may be displayed in the visualization tool SQLyog.

When the parent table changes, the child table sets the foreign key column to the default value, but Innodb cannot recognize it.

Example

-- 部门表
create table dept(
id int primary key,
dept_name varchar(50),
dept_location varchar(50)
);
-- 员工表
CREATE TABLE emp(
eid int primary key,
name varchar(50) not null,
sex varchar(10),
    dept_id int
);
-- 给员工表表的dept_id添加外键指向部门表的主键
alter table emp add foreign key(dept_id) references dept(id)

The above is the detailed content of What foreign key constraint levels does mysql have?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete