当前位置:网站首页>MySQL foreign key constraint
MySQL foreign key constraint
2022-04-23 05:07:00 【Write poetry for you@】
First, let's build two test tables
First create the main table
CREATE TABLE my_class(
id INT PRIMARY KEY , -- Primary key
`name` VARCHAR(32) NOT NULL DEFAULT ''
);
Creating slave tables
CREATE TABLE my_stu (
id INT PRIMARY KEY , -- Student number
`name` VARCHAR(32) NOT NULL DEFAULT '',
class_id INT, -- If ( Foreign keys ) If it's not empty, you can't
-- Next, specify the foreign key relationship
FOREIGN KEY (class_id) REFERENCES my_class(id)
)
First record the definition of foreign keys :
1. Foreign keys are used for Define the relationship between master table and slave table : Foreign key constraints should be defined on the slave table ,
From the table
Found a field from the table (class_id), This field is used to establish the foreign key relationship ‘ The key ’,
Actually, there's a... On the table class_id This field The value of the inside Point to a field in the main table (id Field ) Value ,
That is, in the foreign key table The class number of each student class_id( The value ) Namely ( Main table ) The class code in the class table id Value
First record the necessary conditions for establishing a foreign key relationship :
The value of the foreign key field , Must have appeared in the primary key field ,( Like in the picture class_id The field value of is 300 In the main table id There has been... In this field )
Or for null [ If the foreign key field is allowed to be null]
Once the foreign key relationship is established between the slave table and the master table , Want to delete a piece of information in the main table , First delete the record of the table where the foreign key is located ( From the table ),
Then delete the record in the main table
ps: Here, a record in the main table is deleted directly , Because of... In the main table id This field is the same as that from the table class_id The foreign key relationship established by this field cannot be deleted
The correct approach :
First delete the record in the table where the foreign key is located ( From the table id) Record ,
class_id=300 The record of is gone
Then delete the main table ( Records in the class table ) Find out
reason : From table The foreign key Record of the value of the field There is no association in the main table
That is to say , From the values of foreign keys class_id Field =??? And In the main table id=??? This field value has no relationship
版权声明
本文为[Write poetry for you@]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220549552354.html
边栏推荐
- 洛谷P2731骑马修栅栏
- Chapter III project schedule management of information system project manager summary
- Sword finger offer: push in and pop-up sequence of stack
- Unity C# 网络学习(四)
- 【数据库】MySQL单表查询
- redis数据类型有哪些
- L2-011 play binary tree (build tree + BFS)
- 【数据库】MySQL基本操作(基操~)
- Set Chrome browser background to eye protection (eye escort / darkreader plug-in)
- Day. JS common methods
猜你喜欢
[winui3] Écrivez une copie du gestionnaire de fichiers Explorer
The applet calls the function of scanning QR code and jumps to the path specified by QR code
Repair of self calibration SPC failure of Tektronix oscilloscope dpo3054
2022/4/22
[2022 ICLR] Pyramid: low complexity pyramid attention for long range spatiotemporal sequence modeling and prediction
MySQL - index
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
AQS源码阅读
使用zerotier让异地设备组局域网
【数据库】MySQL单表查询
随机推荐
Analysis of POM files
C. Tree infection (simulation + greed)
Discussion on flow restriction
Chapter II project scope management of information system project manager summary
Various ways of writing timed tasks of small programs
L2-011 play binary tree (build tree + BFS)
C. Tree Infection(模拟+贪心)
QPushbutton 槽函数被多次触发
深度学习笔记 —— 微调
js 判斷數字字符串中是否含有字符
什么是指令周期,机器周期,和时钟周期?
Machine learning - linear regression
Other problems encountered in debugging fingerprints
MySQL views the SQL statement details executed by the optimizer
Unity C e-learning (IV)
Field injection is not recommended using @ Autowired
Grpc long connection keepalive
In aggregated query without group by, expression 1 of select list contains nonaggregated column
直播带货表格模板-自动显示图片-自动关联系列商品
Mac enters MySQL terminal command