当前位置:网站首页>数据库表外键的设计
数据库表外键的设计
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。
边栏推荐
- 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
- CSP window
- 不得不会的Oracle数据库知识点(一)
- Several ways to set up a blog locally [attach relevant software download links]
- Global and Chinese markets for coronary artery disease treatment devices 2022-2028: Research Report on technology, participants, trends, market size and share
- P1656 bombing Railway
- It is forbidden to splice SQL in code
- Zipper table in data warehouse (compressed storage)
- What is regression testing? Talk about regression testing in the eyes of Ali Test Engineers
- 不得不会的Oracle数据库知识点(二)
猜你喜欢
Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival
[C language] break and continue in switch statement
How to be a professional software testing engineer? Listen to the byte five year old test
It is forbidden to splice SQL in code
2022 system integration project management engineer examination knowledge points: software development model
What is the Valentine's Day gift given by the operator to the product?
Att & CK actual combat series - red team actual combat - V
Eight year test old bird, some suggestions for 1-3 year programmers
手机异步发送短信验证码解决方案-Celery+redis
Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing
随机推荐
What is the potential of pocket network, which is favored by well-known investors?
On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
Idea a method for starting multiple instances of a service
不得不会的Oracle数据库知识点(二)
It is worthy of "Alibaba internal software test interview notes" from beginning to end, all of which are essence
How will the complete NFT platform work in 2022? How about its core functions and online time?
ITK learning notes (VII) the position of ITK rotation direction remains unchanged
MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
Global and Chinese markets for blood and liquid heating devices 2022-2028: Research Report on technology, participants, trends, market size and share
Report on prospects and future investment recommendations of China's assisted reproductive industry, 2022-2028 Edition
[NLP] text classification still stays at Bert? Duality is too strong than learning framework
P1339 [USACO09OCT]Heat Wave G
What is the Valentine's Day gift given by the operator to the product?
Shell script three swordsman sed
Subgraph isomorphism -subgraph isomorphism
网上的低佣金链接安全吗?招商证券怎么开户?
【leetcode】374. Guess the size of the number
Unity elementary case notes of angry birds Siki college 1-6
Entropy and full connection layer
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