当前位置:网站首页>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.
边栏推荐
- Stock price forecast
- 我管你什么okr还是kpi,PPT轻松交给你
- ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
- P1656 bombing Railway
- 1214 print diamond
- Global and Chinese market of melting furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
- 12. Go implementation of integer to Roman numeral and leetcode
- [cloud native topic -48]:kubesphere cloud Governance - operation - overview of multi tenant concept
- Development and application of fcitx functional plug-ins
- Is the low commission link on the internet safe? How to open an account for China Merchants Securities?
猜你喜欢

Network layer - routing

Analysis and solution of lazyinitializationexception
![Several ways to set up a blog locally [attach relevant software download links]](/img/2f/51a09d9ef71065319ed90306517854.jpg)
Several ways to set up a blog locally [attach relevant software download links]
![[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

It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
![[prefix and notes] prefix and introduction and use](/img/a6/a75e287ac481559d8f733e6ca3e59c.jpg)
[prefix and notes] prefix and introduction and use

Pytest unit test framework: simple and easy to use parameterization and multiple operation modes

A method to solve Bert long text matching

The difference between objects and objects

Regular expressions and text processors for shell programming
随机推荐
Is the low commission link on the internet safe? How to open an account for China Merchants Securities?
Stock price forecast
(Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
swagger中响应参数为Boolean或是integer如何设置响应描述信息
Arc 135 supplementary report
不得不会的Oracle数据库知识点(四)
The FISCO bcos console calls the contract and reports an error does not exist
Unity elementary case notes of angry birds Siki college 1-6
Solution to the impact of Remote Code Execution Vulnerability of log4j2 component on December 9, 2021
Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share
Self study software testing. To what extent can you go out and find a job?
Pair
MySQL winter vacation self-study 2022 12 (2)
不得不会的Oracle数据库知识点(二)
打印菱形图案
功能:求出菲波那契数列的前一项与后一项之比的极限的 近似值。例如:当误差为0.0001时,函数值为0.618056。
ITK learning notes (VII) the position of ITK rotation direction remains unchanged
Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
【leetcode】300. Longest increasing subsequence (dynamic programming, dichotomy)
2022 Software Test Engineer skill list, please check