[Disable foreign keys] Why do Internet giants disable foreign key constraints? Talk about the advantages, disadvantages and usage scenarios of foreign keys in detail

navigation:

[Java Notes + Stepping on the Pit Summary] Java Basics + Advanced + JavaWeb + SSM + SpringBoot + St. Regis Takeaway + SpringCloud + Dark Horse Tourism + Guli Mall + Xuecheng Online + MySQL Advanced Chapter + Design Mode + Common Interview Questions + Source Code

Table of contents

1. Foreign key introduction

1.1 Overview 

1.2 Exercises

1.2.1 Data preparation

1.2.2 When verifying that there is a foreign key, delete the record to maintain the foreign key

1.2.3 Add foreign key again

1.2.4 Query all foreign keys

Second, the advantages and disadvantages of foreign keys 

2.1 Foreign key disadvantages

2.2 Advantages of foreign keys

3. Applicable scenarios

3.1 Under what circumstances should foreign keys not be used? Why do Internet giants disable foreign key constraints?

3.2 Under what circumstances can foreign keys be used?


1. Foreign key introduction

1.1 Overview 

-- 创建表时添加外键约束
CREATE TABLE 表名(
   列名 数据类型,
   …
   [CONSTRAINT] [外键取名名称] FOREIGN KEY(外键列名) REFERENCES 主表(主表列名) 
); 

-- 创建表时添加外键约束,constraint译作限制,束缚;references译作关联,参考,提及
create table 表名(
   列名 数据类型,
   …
   [constraint] [外键取名名称] foreign key(外键列名) references 主表(主表列名) 
); 
-- 建完表后添加外键约束
ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名称) REFERENCES 主表名称(主表列名称);

-- 建完表后添加外键约束
alter table 表名 add constraint 外键名称 foreign key (外键字段名称) references 主表名称(主表列名称);
  • Remove foreign key constraints

ALTER TABLE 表名 DROP FOREIGN KEY 外键名称;

1.2 Exercises

1.2.1 Data preparation

-- 删除表
DROP TABLE IF EXISTS emp;
DROP TABLE IF EXISTS dept;

-- 部门表
CREATE TABLE dept(
	id int primary key auto_increment,
	dep_name varchar(20),
	addr varchar(20)
);
-- 员工表 
CREATE TABLE emp(
	id int primary key auto_increment,
	name varchar(20),
	age int,
	dep_id int,

	-- 添加外键 dep_id,关联 dept 表的id主键
	CONSTRAINT fk_emp_dept FOREIGN KEY(dep_id) REFERENCES dept(id)	
);

adding data

-- 添加 2 个部门
insert into dept(dep_name,addr) values
('研发部','广州'),('销售部', '深圳');
​
-- 添加员工,dep_id 表示员工所在的部门
INSERT INTO emp (name, age, dep_id) VALUES 
('张三', 20, 1),
('李四', 20, 1),
('王五', 20, 1),
('赵六', 20, 2),
('孙七', 22, 2),
('周八', 18, 2);

 

 

1.2.2 When verifying that there is a foreign key, delete the record to maintain the foreign key

If you delete 研发部this piece of data at this time, you will find that it cannot be deleted.

DELETE FROM dept WHERE id=1

 

1.2.3 When verifying that there is no foreign key, deleting records does not need to maintain foreign keys  

delete foreign key

alter table emp drop FOREIGN key fk_emp_dept;

Delete 研发部this piece of data at this time, and you will find that the deletion is successful:

DELETE FROM dept WHERE id=1

 

 

 

 

1.2.3 Add foreign key again

alter table emp add CONSTRAINT fk_emp_dept FOREIGN key(dep_id) REFERENCES dept(id);

1.2.4 Query all foreign keys

SELECT 
  COLUMN_NAME, 
  CONSTRAINT_NAME, 
  REFERENCED_TABLE_NAME, 
  REFERENCED_COLUMN_NAME 
FROM 
  INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE 
  TABLE_NAME = 'emp' 
  AND REFERENCED_TABLE_NAME IS NOT NULL;

 

 

Second, the advantages and disadvantages of foreign keys 

2.1 Foreign key disadvantages

  • Foreign keys must be considered when modifying or deleting: every time you do DELETE or UPDATE, you must consider foreign key constraints, which is inconvenient.
  • Table-level locks lead to poor concurrency: Concurrency issues foreign key constraints will enable row-level locks The main table will enter blocking when writing
  • Cascading deletion problem: When deleting a record in the main table, the record in the secondary table associated with the foreign key of the record will also be deleted, resulting in uncontrollable data. For example, if you delete an order in the "Order Table", a record in the associated "Order Details Table" will also be deleted.
  • High coupling and troublesome migration: The main table and the slave table are coupled with each other. When the data volume of the main table is too large and you want to split the table and migrate the data, you must first delete the foreign key. Otherwise, you just deleted a record in the main table and associate it with the slave table Records are also cascaded deleted, resulting in data loss.

2.2 Advantages of foreign keys

  • Data consistency: The database can guarantee the integrity and consistency of data through foreign keys. Because when the records in the master table are updated, the corresponding records in the slave table will also be changed, and table-level locks will be added when the changes are made to ensure data consistency at the expense of concurrency. For example, if you delete an order in the "Order Table", a record in the associated "Order Details Table" will also be deleted.
  • Increase the readability of ER diagrams: Through foreign keys, you can see the relationship between tables more intuitively.

3. Applicable scenarios

3.1 Under what circumstances should foreign keys not be used? Why do Internet giants disable foreign key constraints?

Comprehensively comparing the advantages and disadvantages of foreign keys, it can be concluded that:

Distributed projects with high concurrency and a large amount of table data are suitable for using foreign keys. Prevent table-level locks from being added every time data is updated, and maintain the consistency of foreign keys.

Because the products of major Internet companies have high concurrency and are generally distributed projects , in order to improve the concurrency performance of the database, foreign keys are generally disabled to achieve data consistency at the business level. For example, before deleting a record in the "order table", first associate Query the corresponding records in the "Order Details Table", and then delete them one by one.

3.2 Under what circumstances can foreign keys be used?

Comprehensively comparing the advantages and disadvantages of foreign keys, it can be concluded that:

Monolithic projects with low concurrency, small table data volume, and high data consistency requirements are suitable for using foreign keys.

Guess you like

Origin blog.csdn.net/qq_40991313/article/details/131688530