当前位置:网站首页>Why do we not use foreign keys now (2)?
Why do we not use foreign keys now (2)?
2022-06-22 23:49:00 【Kunlunbase Kunlun database】
Preface
MySQL Foreign keys (FOREIGNKEY) Is a special field of the table , Used to establish the relationship between the master table and the slave table , Establish a connection between the data of two tables , Constrains the consistency and integrity of data in two tables .
Previous first article related articles (「 Technical discussion 」 Why do you seldom use foreign keys ?), Summarizes some scenarios that do not use foreign keys and the advantages of using foreign keys .
This article will illustrate, based on examples, that foreign key constraints can guarantee the data relationship between tables “ Always complete and consistent ”, But in practice , Every time I do DELETE perhaps UPDATE Must consider foreign key constraints , It can lead to painful development .
Keep data consistent and complete , It is mainly reflected in the following two aspects :
Prevent execution
Insert new row from table , If the foreign key value is not the primary key value of the primary table, the insertion is prevented ;
Modify foreign key values from the table , If the new value is not the primary key value of the primary table, the modification is prevented ;
Main table delete row , Its primary key value cannot be deleted if it exists in the secondary table ( To delete , You must first delete the related rows from the table );
Modify the primary key value in the main table , Old values that exist from the table prevent modification ( To modify , You must first delete the related rows from the table ).
Cascade execution
Main table delete row , Delete the related rows of the table together ;
Modify the primary key value in the main table , Modify it together with the foreign key values of the related rows of the table .
Let's take an example to illustrate :
1. Create two table,“ The coach ” and “ student ”,table“ student ” With foreign key constraints .
CREATE TABLE jiaolian(
`jiaolian_id` INT AUTO_INCREMENT,
`jiaolian_name` VARCHAR(30),
PRIMARY KEY (`jiaolian_id`));
CREATE TABLE xueyuan(
`xueyuan_id` INT AUTO_INCREMENT,
`jiaolian_id` INT,
`xueyuan_name` VARCHAR(30),
FOREIGN KEY (`jiaolian_id`) REFERENCES `jiaolian` (`jiaolian_id`),
PRIMARY KEY (`xueyuan_id`));2. Add two “ The coach ”, Two “ student ”.
insert into jiaolian(jiaolian_name) values(" Driver Lao Li ");
insert into jiaolian(jiaolian_name) values(" Driver Lao Lin ");
insert into xueyuan(jiaolian_id,xueyuan_name) values(1," Disciple Zhang San ");
insert into xueyuan(jiaolian_id,xueyuan_name) values(2," Disciple Li Si ");3. Add another “ student ”, There is no the jiaolian_id, Execution failed .
insert into xueyuan(jiaolian_id,xueyuan_name) values(3," Disciple Wang Wu "); #error4. Delete one “ The coach ”,“ The coach ” Related “ student ”, Execution failure .
delete from jiaolian where jiaolian_name=" Driver Lao Li "; #error5. Delete first “ The coach ” The associated “ student ”, And then delete “ The coach ”, Successful implementation
delete from xueyuan where xueyuan_name=" Disciple Zhang San ";
delete from jiaolian where jiaolian_name=" Driver Lao Li ";Conclusion
Through this example , We can see , Foreign key constraints guarantee the relationship of data between tables “ Always complete and consistent ”.
Although the data consistency and integrity judgment is entrusted to the database , Reduces the amount of code in the program . But in practice , Every time I do DELETE perhaps UPDATE Must consider foreign key constraints , It can lead to painful development , The test data is extremely inconvenient .
In the case of using foreign keys , Every time you modify the data, you need to check the data in another table , Need to acquire additional locks .
In high concurrency and high traffic transaction scenarios , Using foreign keys can easily cause deadlock , And the bottleneck of database resources , Therefore, it is not recommended to use in general Internet industry with high frequency and concurrency .
Generally speaking , When implemented in business code , As long as the application layer follows the inherent correlation logic at the beginning of the design , To process the data , There is no need for foreign key constraints at the database level .
KunlunDB The project is open source
【GitHub:】
https://github.com/zettadb
【Gitee:】
https://gitee.com/zettadb
END
边栏推荐
- [go] go modules GETTING STARTED
- After passing the hcip exam, I still failed to change my career. What do professional network workers value most
- 包管理工具--NPM、--CNPM、 --Yarn、 --CYarn
- Synchronization circuit and cross clock domain circuit design 2 -- cross clock domain transmission (FIFO) of multi bit signals
- JSBridge
- 'dare not doubt the code, but have to doubt the code 'a network request timeout analysis
- 冒泡排序 指针
- Is it safe to make an appointment to pay new debts? Is it reliable?
- KunlunDB 查询优化(一)
- Unity:利用 射线Ray 检测物体
猜你喜欢

DCC888 :SSA (static single assignment form)

2022天梯赛-全国总决赛复盘赛

After passing the hcip exam, I still failed to change my career. What do professional network workers value most

Oracle ASM uses the CP command in asmcmd to perform remote replication

华为云如何实现实时音视频全球低时延网络架构【上】

异步FIFO
![[kubernetes series] overview of kubernetes](/img/c2/b42f7a0db236558cc466a508d3d579.png)
[kubernetes series] overview of kubernetes
![[go] go mod mode, package 12import/add is not in goroot](/img/b5/4cf5d3f04d0e5cc6f5a957959022ec.png)
[go] go mod mode, package 12import/add is not in goroot

Synchronization circuit and cross clock domain circuit design 2 -- cross clock domain transmission (FIFO) of multi bit signals

OLAP - Druid introduction
随机推荐
Oracle ASM uses the CP command in asmcmd to perform remote replication
OJ每日一练——删除单词后缀
Redis cache
[arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board
Is it difficult to turn weak current into professional network worker? Huawei pre-sales engineers share their own experience
【GO】go语言interface
Kunlun distributed database sequence function and its implementation mechanism
[go] go language interface
RedisTemplate使用遇到\x00的問題
Use the find command
Safe and reliable! Tianyi cloud data security management platform passed the evaluation
口令安全是什么意思?等保2.0政策中口令安全标准条款有哪些?
[kubernetes series] overview of kubernetes
昆仑分布式数据库技术优势
OJ每日一练——单词的长度
os. When the command line parameter in args[1:] is empty, the internal statement will not be executed
[go] go mod mode, package 12import/add is not in goroot
c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试
Programmers' choice of taking private jobs and part-time jobs
13. Roman numeral to integer