当前位置:网站首页>数据库表外键的设计
数据库表外键的设计
2022-07-04 00:34:00 【Mr_WoLong】
数据库表外键的设计
一对多:
一对多,两张表,多的表加外键!!!!!!!!!!!!
t_class 班级表
classno(pk) classname
------------------------------------------------------
100 北京市大兴区亦庄镇第二中学高三1班
101 北京市大兴区亦庄镇第二中学高三1班
t_student 学生表
no(pk) name cno(FK引用t_class这张表的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
多对多:
多对多,三张表,关系表两个外键!!!!!!!!!!!!!!!
为了让以上的表满足第二范式,需要这样设计:
使用3张表来表示多对多的关系!!!!
学生表
学生编号(pk) 学生名字
------------------------------------
1001 张三
1002 李四
1003 王五
教师表
教师编号(pk) 教师姓名
--------------------------------------
001 王老师
002 赵老师
学生教师关系表
id(pk) 学生编号(fk) 教师编号(fk)
------------------------------------------------------
1 1001 001
2 1002 002
3 1003 001
4 1001 002
一对一:
一对一放到一张表中不就行了吗?为啥还要拆分表?
在实际的开发中,可能存在一张表字段太多,太庞大。这个时候要拆分表。
一对一怎么设计?
没有拆分表之前:一张表
t_user
id login_name login_pwd real_name email address........
---------------------------------------------------------------------------
1 zhangsan 123 张三 [email protected]
2 lisi 123 李四 [email protected]
这种庞大的表建议拆分为两张:
t_login 登录信息表
id(pk) login_name login_pwd
---------------------------------
1 zhangsan 123
2 lisi 123
t_user 用户详细信息表
id(pk) real_name email address........ login_id(fk+unique)
-----------------------------------------------------------------------------------------
100 张三 [email protected] 1
200 李四 [email protected] 2
口诀:一对一,外键唯一!!!!!!!!!!
外键约束(foreign key,简称FK)
外键约束涉及到的相关术语:
外键约束:一种约束(foreign key)
外键字段:该字段上添加了外键约束
外键值:外键字段当中的每一个值。
业务背景:
请设计数据库表,来描述“班级和学生”的信息?
第一种方案:班级和学生存储在一张表中???
t_student
no(pk) name classno classname
----------------------------------------------------------------------------------
1 jack 100 北京市大兴区亦庄镇第二中学高三1班
2 lucy 100 北京市大兴区亦庄镇第二中学高三1班
3 lilei 100 北京市大兴区亦庄镇第二中学高三1班
4 hanmeimei 100 北京市大兴区亦庄镇第二中学高三1班
5 zhangsan 101 北京市大兴区亦庄镇第二中学高三2班
6 lisi 101 北京市大兴区亦庄镇第二中学高三2班
7 wangwu 101 北京市大兴区亦庄镇第二中学高三2班
8 zhaoliu 101 北京市大兴区亦庄镇第二中学高三2班
分析以上方案的缺点:
数据冗余,空间浪费!!
这个设计是比较失败的!
第二种方案:班级一张表、学生一张表
t_class 班级表
classno(pk) classname
------------------------------------------------------
100 北京市大兴区亦庄镇第二中学高三1班
101 北京市大兴区亦庄镇第二中学高三1班
t_student 学生表
no(pk) name cno(FK引用t_class这张表的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
当cno字段没有任何约束的时候,可能会导致数据无效。可能出现一个102,但是102班级不存在。
所以为了保证cno字段中的值都是100和101,需要给cno字段添加外键约束。
那么:cno字段就是外键字段。cno字段中的每一个值都是外键值。
注意:
t_class是父表
t_student是子表
删除表的顺序?
先删子,再删父。
创建表的顺序?
先创建父,再创建子。
删除数据的顺序?
先删子,再删父。
插入数据的顺序?
先插入父,再插入子。
注意:
- 子表中的外键引用的父表中的某个字段,被引用的这个字段必须是主键吗? 不一定是主键,但至少具有unique约束。
- 外键值可以为NULL。
边栏推荐
- Report on prospects and future investment recommendations of China's assisted reproductive industry, 2022-2028 Edition
- Five high-frequency questions were selected from the 200 questions raised by 3000 test engineers
- P1629 postman delivering letter
- Iclr2022: how does AI recognize "things I haven't seen"?
- The FISCO bcos console calls the contract and reports an error does not exist
- I would like to ask how the top ten securities firms open accounts? Is it safe to open an account online?
- What insurance products should be bought for the elderly?
- Selenium library 4.5.0 keyword explanation (I)
- Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
- Selenium library 4.5.0 keyword explanation (4)
猜你喜欢

MySQL is installed as a Windows Service

What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

Unity elementary case notes of angry birds Siki college 1-6

Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
![[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
![[CSDN Q & A] experience and suggestions](/img/db/dff3173dda24ca5740729b54a81153.jpg)
[CSDN Q & A] experience and suggestions

Beijing invites reporters and media

打印菱形图案

On covariance of array and wildcard of generic type
随机推荐
Idea set class header comments
What are the application fields of digital twins in industry?
Distributed transaction -- middleware of TCC -- selection / comparison
[leetcode] interview question 17.08 Circus tower
Interview script of Software Test Engineer
Understanding of Radix
What is the difference between NFT, SFT and dnft? How to build NFT platform applications?
Is the low commission link on the internet safe? How to open an account for China Merchants Securities?
Anomalies seen during the interview
不得不会的Oracle数据库知识点(一)
P1339 [USACO09OCT]Heat Wave G
MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
[PHP basics] session basic knowledge, application case code and attack and defense
Zipper table in data warehouse (compressed storage)
Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
The culprit of unrestrained consumption -- Summary
老姜的特点
[NLP] text classification still stays at Bert? Duality is too strong than learning framework
STM32 GPIO CSDN creative punch in
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!