当前位置:网站首页>数据库表外键的设计
数据库表外键的设计
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。
边栏推荐
- Detailed explanation of the relationship between Zhongtai, wechat and DDD
- Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
- Att & CK actual combat series - red team actual combat - V
- HR disgusted interview behavior
- Several ways to set up a blog locally [attach relevant software download links]
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
- [complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
- Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
- Advanced C language - pointer 2 - knowledge points sorting
- Analysis: misunderstanding of choosing WMS warehouse management system
猜你喜欢

2022 system integration project management engineer examination knowledge points: software development model

What is the GPM scheduler for go?

Beijing invites reporters and media

SPI based on firmware library

Report on the construction and development mode and investment mode of sponge cities in China 2022-2028

Recommendation of knowledge base management system

Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method

It is forbidden to splice SQL in code
![Several ways to set up a blog locally [attach relevant software download links]](/img/2f/51a09d9ef71065319ed90306517854.jpg)
Several ways to set up a blog locally [attach relevant software download links]

打印菱形图案
随机推荐
For loop
MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
Selenium library 4.5.0 keyword explanation (II)
[leetcode] interview question 17.08 Circus tower
What is the difference between NFT, SFT and dnft? How to build NFT platform applications?
功能:编写函数fun求s=1^k+2^k +3^k + ......+N^k的值, (1的K次方到N的K次方的累加和)。
Development and application of fcitx functional plug-ins
Why use get/set instead of exposing properties
On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
STM32 key light
12. Go implementation of integer to Roman numeral and leetcode
Global and Chinese markets for coronary artery disease treatment devices 2022-2028: Research Report on technology, participants, trends, market size and share
Software testers, how can you quickly improve your testing skills? Ten minutes to teach you
system. Exit (0) and system exit(1)
Understanding of Radix
Solution to the impact of Remote Code Execution Vulnerability of log4j2 component on December 9, 2021
P1629 postman delivering letter
(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
The FISCO bcos console calls the contract and reports an error does not exist