当前位置:网站首页>数据库表外键的设计
数据库表外键的设计
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。
边栏推荐
- Bodong medical sprint Hong Kong stocks: a 9-month loss of 200million Hillhouse and Philips are shareholders
- On covariance of array and wildcard of generic type
- Struct in linked list
- Alibaba test engineer with an annual salary of 500000 shares notes: a complete set of written tests of software testing
- STM32 key light
- (Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
- Wechat official account and synchronization assistant
- Global and Chinese market of glossometer 2022-2028: Research Report on technology, participants, trends, market size and share
- Att & CK actual combat series - red team actual combat - V
- [CSDN Q & A] experience and suggestions
猜你喜欢

China standard gas market prospect investment and development feasibility study report 2022-2028

Shell script three swordsman sed

NLP pre training technology development

Qtcharts notes (V) scatter diagram qscatterseries
![[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!](/img/28/fc05f2e8d53cf81fd061c799090022.jpg)
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!

Similarities and differences of text similarity between Jaccard and cosine

A Kuan food rushed to the Shenzhen Stock Exchange: with annual sales of 1.1 billion, Hillhouse and Maotai CCB are shareholders

Kubedl hostnetwork: accelerating the efficiency of distributed training communication

Kubedl hostnetwork: accelerating the efficiency of distributed training communication

Idea a method for starting multiple instances of a service
随机推荐
Software testers, how can you quickly improve your testing skills? Ten minutes to teach you
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
What is the potential of pocket network, which is favored by well-known investors?
Pair
SPI based on firmware library
What are the application fields of digital twins in industry?
Selenium library 4.5.0 keyword explanation (III)
P1656 bombing Railway
China standard gas market prospect investment and development feasibility study report 2022-2028
I would like to ask how the top ten securities firms open accounts? Is it safe to open an account online?
Pytest unit test framework: simple and easy to use parameterization and multiple operation modes
[Mongodb] 2. Use mongodb --------- use compass
Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
What is the future of software testing industry? Listen to the test veterans' answers
It is forbidden to splice SQL in code
Test the influence of influent swacth on the electromagnetic coil of quartz meter
[C language] break and continue in switch statement
手机异步发送短信验证码解决方案-Celery+redis
Regular expressions and text processors for shell programming
1214 print diamond