当前位置:网站首页>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
边栏推荐
- Learning Android from scratch -- Introduction
- Logrus set log format and output function name
- 【数据库】MySQL多表查询(一)
- Independent station operation | Facebook marketing artifact - chat robot manychat
- [WinUI3]編寫一個仿Explorer文件管理器
- Unity C e-learning (IV)
- Informatics Aosai yibentong 1212: letters | openjudge 2.5 156: Letters
- MySQL - index
- C. Tree Infection(模拟+贪心)
- Backup MySQL database with Navicat
猜你喜欢
How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
[2022 ICLR] Pyramid: low complexity pyramid attention for long range spatiotemporal sequence modeling and prediction
MySQL - index
What are the redis data types
Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
#define 定义常量和宏,指针和结构体
持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
JS engine loop mechanism: synchronous, asynchronous, event loop
[winui3] write an imitation Explorer file manager
2022/4/22
随机推荐
MySQL uses or to query SQL, and SQL execution is very slow
机器学习---线性回归
Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
On distributed lock
Uglifyjs compress JS
Details related to fingerprint payment
Informatics Olympiad 1955: [11noip popularization group] Swiss round | openjudge 4.1 4363: Swiss round | Luogu p1309 [noip2011 popularization group] Swiss round
【数据库】表的查看、修改和删除
Progress of innovation training (IV)
Progress of innovation training (III)
The 8 diagrams let you see the execution sequence of async / await and promise step by step
Live delivery form template - automatically display pictures - automatically associate series products
Kubectl command automatic replenishment
leetcode——启发式搜索
Logrus set log format and output function name
The applet calls the function of scanning QR code and jumps to the path specified by QR code
Independent station operation | Facebook marketing artifact - chat robot manychat
Day.js 常用方法
[2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
Leetcode 1547: minimum cost of cutting sticks