当前位置:网站首页>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.
边栏推荐
- Sequence list and linked list
- STM32 key light
- 国元证券开户是真的安全可靠吗
- Understanding of Radix
- Bodong medical sprint Hong Kong stocks: a 9-month loss of 200million Hillhouse and Philips are shareholders
- 2-Redis架构设计到使用场景-四种部署运行模式(下)
- OS interrupt mechanism and interrupt handler
- Global and Chinese market of glossometer 2022-2028: Research Report on technology, participants, trends, market size and share
- [complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
- 关于 uintptr_t和intptr_t 类型
猜你喜欢
What is the future of software testing industry? Listen to the test veterans' answers
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
swagger中响应参数为Boolean或是integer如何设置响应描述信息
Reading notes on how programs run
How to be a professional software testing engineer? Listen to the byte five year old test
A method to solve Bert long text matching
The FISCO bcos console calls the contract and reports an error does not exist
It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
Qtcharts notes (V) scatter diagram qscatterseries
MySQL is installed as a Windows Service
随机推荐
Sorry, Tencent I also refused
Advanced C language - pointer 2 - knowledge points sorting
It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
STM32 GPIO CSDN creative punch in
P3371 [template] single source shortest path (weakened version)
Why use get/set instead of exposing properties
Generic
swagger中响应参数为Boolean或是integer如何设置响应描述信息
What does redis do? Redis often practices grammar every day
不得不会的Oracle数据库知识点(一)
Global and Chinese markets for instant saliva testing devices 2022-2028: Research Report on technology, participants, trends, market size and share
关于 uintptr_t和intptr_t 类型
[CSDN Q & A] experience and suggestions
Subgraph isomorphism -subgraph isomorphism
Global and Chinese market of breast cancer imaging 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL is installed as a Windows Service
Understanding of Radix
Bodong medical sprint Hong Kong stocks: a 9-month loss of 200million Hillhouse and Philips are shareholders
OS interrupt mechanism and interrupt handler
Anomalies seen during the interview