当前位置:网站首页>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.
边栏推荐
- 机器学习基础:用 Lasso 做特征选择
- Detailed explanation of the relationship between Zhongtai, wechat and DDD
- GUI 应用:socket 网络聊天室
- 不得不会的Oracle数据库知识点(四)
- A dichotomy of Valentine's Day
- Beijing invites reporters and media
- Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
- MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
- The FISCO bcos console calls the contract and reports an error does not exist
- [error record] configure NDK header file path in Visual Studio (three header file paths of NDK | ASM header file path selection related to CPU architecture)
猜你喜欢

Interview script of Software Test Engineer
![[NLP] text classification still stays at Bert? Duality is too strong than learning framework](/img/49/1ff6025bdb0445e5638c1451e0b267.jpg)
[NLP] text classification still stays at Bert? Duality is too strong than learning framework

Att & CK actual combat series - red team actual combat - V
![[GNN] hard core! This paper combs the classical graph network model](/img/a7/2140744ebad9f1dc0a609254cc618e.jpg)
[GNN] hard core! This paper combs the classical graph network model

Eight year test old bird, some suggestions for 1-3 year programmers
![[C language] break and continue in switch statement](/img/ae/5967fefcf3262c9d3096e5c7d644fd.jpg)
[C language] break and continue in switch statement

Windos10 reinstallation system tutorial

URL (data:image/png; Base64, ivborw0k... Use case

ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt

How to use AHAS to ensure the stability of Web services?
随机推荐
7.1 学习内容
Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
Advanced C language - pointer 2 - knowledge points sorting
Understanding of Radix
查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
P1656 bombing Railway
P1629 postman delivering letter
Recommendation of knowledge base management system
The upload experience version of uniapp wechat applet enters the blank page for the first time, and the page data can be seen only after it is refreshed again
1214 print diamond
Joint examination of six provinces 2017
Gauss elimination method and template code
It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
功能:编写函数fun求s=1^k+2^k +3^k + ......+N^k的值, (1的K次方到N的K次方的累加和)。
手机异步发送短信验证码解决方案-Celery+redis
[common error] custom IP instantiation error
[error record] configure NDK header file path in Visual Studio (three header file paths of NDK | ASM header file path selection related to CPU architecture)
Anomalies seen during the interview
Test the influence of influent swacth on the electromagnetic coil of quartz meter
Network layer - routing