当前位置:网站首页>数据库表外键的设计
数据库表外键的设计
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。
边栏推荐
- Development and application of fcitx functional plug-ins
- On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
- Wechat official account and synchronization assistant
- URL (data:image/png; Base64, ivborw0k... Use case
- For loop
- Idea a method for starting multiple instances of a service
- system. Exit (0) and system exit(1)
- ITK learning notes (VII) the position of ITK rotation direction remains unchanged
- Self study software testing. To what extent can you go out and find a job?
- What is the potential of pocket network, which is favored by well-known investors?
猜你喜欢
What does redis do? Redis often practices grammar every day
What is the GPM scheduler for go?
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
Generic
What is the Valentine's Day gift given by the operator to the product?
Correlation analysis summary
What is the potential of pocket network, which is favored by well-known investors?
Detailed explanation of the relationship between Zhongtai, wechat and DDD
2022 system integration project management engineer examination knowledge points: software development model
功能:编写函数fun求s=1^k+2^k +3^k + ......+N^k的值, (1的K次方到N的K次方的累加和)。
随机推荐
Wechat official account and synchronization assistant
Selenium library 4.5.0 keyword explanation (II)
What is the Valentine's Day gift given by the operator to the product?
2020.2.14
Is the account opening of Guoyuan securities really safe and reliable
不得不会的Oracle数据库知识点(四)
Interview script of Software Test Engineer
Social network analysis -social network analysis
How to be a professional software testing engineer? Listen to the byte five year old test
P3371 [template] single source shortest path (weakened version)
Shell script three swordsman sed
AI Challenger 2018 text mining competition related solutions and code summary
Detailed explanation of the relationship between Zhongtai, wechat and DDD
Anomalies seen during the interview
Global and Chinese markets for blood and liquid heating devices 2022-2028: Research Report on technology, participants, trends, market size and share
On covariance of array and wildcard of generic type
2022 system integration project management engineer examination knowledge points: software development model
(Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
Introducing Software Testing
Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival