当前位置:网站首页>Design of database table foreign key
Design of database table foreign key
2022-07-04 00:40:00 【Mr_ WoLong】
Database table foreign key design
One to many :
One to many , Two tables , More tables with foreign keys !!!!!!!!!!!!
t_class Class table
classno(pk) classname
------------------------------------------------------
100 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 1 class
101 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 1 class
t_student Student list
no(pk) name cno(FK quote t_class This watch is classno)
----------------------------------------------------------------
1 jack 100
2 lucy 100
3 lilei 100
4 hanmeimei 100
5 zhangsan 101
6 lisi 101
7 wangwu 101
8 zhaoliu 101
Many to many :
Many to many , Three tables , Relationship table two foreign keys !!!!!!!!!!!!!!!
In order to make the above table meet the second paradigm , Need this design :
Use 3 A watch To express Many to many The relationship between !!!!
Student list
Student number (pk) Student name
------------------------------------
1001 Zhang San
1002 Li Si
1003 Wang Wu
Teachers list
Teacher number (pk) Teacher's name
--------------------------------------
001 Teacher wang
002 Miss Zhao
Student teacher relationship form
id(pk) Student number (fk) Teacher number (fk)
------------------------------------------------------
1 1001 001
2 1002 002
3 1003 001
4 1001 002
one-on-one :
Don't you just put one-on-one in a table ? Why split the table ?
In actual development , There may be too many fields in a table , Too big . Split the table at this time .
How to design one-on-one ?
Before splitting the table : A watch
t_user
id login_name login_pwd real_name email address........
---------------------------------------------------------------------------
1 zhangsan 123 Zhang San [email protected]
2 lisi 123 Li Si [email protected]
This huge table is recommended to be split into two :
t_login Login information form
id(pk) login_name login_pwd
---------------------------------
1 zhangsan 123
2 lisi 123
t_user User details table
id(pk) real_name email address........ login_id(fk+unique)
-----------------------------------------------------------------------------------------
100 Zhang San [email protected] 1
200 Li Si [email protected] 2
formula : one-on-one , The foreign key is unique !!!!!!!!!!
Foreign key constraints (foreign key, abbreviation FK)
Terms related to foreign key constraints :
Foreign key constraints : A constraint (foreign key)
Foreign key field : A foreign key constraint is added to this field
Foreign key value : Every value in the foreign key field .
Business background :
Please design the database table , To describe “ Class and students ” Information about ?
First option : Classes and students are stored in a table ???
t_student
no(pk) name classno classname
----------------------------------------------------------------------------------
1 jack 100 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 1 class
2 lucy 100 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 1 class
3 lilei 100 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 1 class
4 hanmeimei 100 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 1 class
5 zhangsan 101 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 2 class
6 lisi 101 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 2 class
7 wangwu 101 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 2 class
8 zhaoliu 101 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 2 class
Analyze the disadvantages of the above scheme :
data redundancy , Space waste !!
This design is a failure !
Second option : Class a table 、 Students a table
t_class Class table
classno(pk) classname
------------------------------------------------------
100 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 1 class
101 Senior 3, No. 2 middle school, Yizhuang Town, Daxing District, Beijing 1 class
t_student Student list
no(pk) name cno(FK quote t_class This watch is classno)
----------------------------------------------------------------
1 jack 100
2 lucy 100
3 lilei 100
4 hanmeimei 100
5 zhangsan 101
6 lisi 101
7 wangwu 101
8 zhaoliu 101
When cno When the field has no constraints , May cause invalid data . There could be a 102, however 102 Class does not exist .
So to make sure cno The values in the field are 100 and 101, Need to give cno Field add foreign key constraint .
that :cno Fields are foreign key fields .cno Each value in the field is a foreign key value .
Be careful :
t_class It's the father's watch
t_student It's a subtable
The order in which tables are deleted ?
Delete the sub first , Then delete the father .
The order in which tables are created ?
Create the parent first , Create Jianzi again .
The order in which data is deleted ?
Delete the sub first , Then delete the father .
The order in which the data is inserted ?
Insert parent first , Then insert the sub .
Be careful :
- The foreign key in the child table refers to a field in the parent table , Must the referenced field be a primary key ? Not necessarily the primary key , But at least it has unique constraint .
- The foreign key value can be NULL.
边栏推荐
- Swagger2 quick start and use
- Global and Chinese market of process beer equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- 功能:编写函数fun求s=1^k+2^k +3^k + ......+N^k的值, (1的K次方到N的K次方的累加和)。
- From functools import reduce -- see the use of reduce function from typical examples
- 手机异步发送短信验证码解决方案-Celery+redis
- 1-Redis架构设计到使用场景-四种部署运行模式(上)
- P1629 postman delivering letter
- A method to solve Bert long text matching
- 【leetcode】374. Guess the size of the number
- Joint examination of six provinces 2017
猜你喜欢

The super fully automated test learning materials sorted out after a long talk with a Tencent eight year old test all night! (full of dry goods

GUI 应用:socket 网络聊天室
![[dynamic programming] leetcode 53: maximum subarray sum](/img/f0/80357f9ffc556f3ed4d3aa0901bb1d.jpg)
[dynamic programming] leetcode 53: maximum subarray sum
![[prefix and notes] prefix and introduction and use](/img/a6/a75e287ac481559d8f733e6ca3e59c.jpg)
[prefix and notes] prefix and introduction and use

Sorry, Tencent I also refused
![[CSDN Q & A] experience and suggestions](/img/db/dff3173dda24ca5740729b54a81153.jpg)
[CSDN Q & A] experience and suggestions

swagger中响应参数为Boolean或是integer如何设置响应描述信息

Self study software testing. To what extent can you go out and find a job?

Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing

Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival
随机推荐
How to use AHAS to ensure the stability of Web services?
Generic
Regular expressions and text processors for shell programming
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
Five high-frequency questions were selected from the 200 questions raised by 3000 test engineers
Interview script of Software Test Engineer
Is the low commission link on the internet safe? How to open an account for China Merchants Securities?
【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Suggestions for improving code quality
NLP pre training technology development
Struct in linked list
12. Go implementation of integer to Roman numeral and leetcode
Cloud dial test helps Weidong cloud education to comprehensively improve the global user experience
网上的低佣金链接安全吗?招商证券怎么开户?
On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
[leetcode] interview question 17.08 Circus tower
A dichotomy of Valentine's Day