当前位置:网站首页>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.
边栏推荐
- Att & CK actual combat series - red team actual combat - V
- Swagger2 quick start and use
- Is the account opening of Guoyuan securities really safe and reliable
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- 12. Go implementation of integer to Roman numeral and leetcode
- The first training of wechat applet
- On covariance of array and wildcard of generic type
- [leetcode] interview question 17.08 Circus tower
- Axure resources and prototype tool Axure RP 9 download
- OS interrupt mechanism and interrupt handler
猜你喜欢

Test the influence of influent swacth on the electromagnetic coil of quartz meter

MySQL winter vacation self-study 2022 12 (1)

Regular expressions and text processors for shell programming

It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction

Entropy and full connection layer
![[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!](/img/3f/75b3125f8779e6cf9467a30fd7eeb4.jpg)
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!

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

A method to solve Bert long text matching
![[PHP basics] session basic knowledge, application case code and attack and defense](/img/f5/713d7044a693ef23e3fbb1ff9bc9f4.jpg)
[PHP basics] session basic knowledge, application case code and attack and defense

NLP Chinese corpus project: large scale Chinese natural language processing corpus
随机推荐
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
P1339 [USACO09OCT]Heat Wave G
Regular expressions and text processors for shell programming
不得不会的Oracle数据库知识点(四)
BBS forum recommendation
Regular expression of shell script value
打印菱形图案
Subgraph isomorphism -subgraph isomorphism
[dynamic programming] leetcode 53: maximum subarray sum
Understanding of Radix
CSP window
Cloud dial test helps Weidong cloud education to comprehensively improve the global user experience
功能:求出菲波那契数列的前一项与后一项之比的极限的 近似值。例如:当误差为0.0001时,函数值为0.618056。
【leetcode】300. Longest increasing subsequence (dynamic programming, dichotomy)
Sequence list and linked list
How to use AHAS to ensure the stability of Web services?
Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival
swagger中响应参数为Boolean或是integer如何设置响应描述信息
12. Go implementation of integer to Roman numeral and leetcode
Generic