Home > Database > Mysql Tutorial > body text

What foreign key constraint levels does mysql have?

WBOY
Release: 2023-05-30 16:25:19
forward
854 people have browsed it

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)
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!