当前位置:网站首页>为什么需要外键?
为什么需要外键?
2022-07-27 12:32:00 【涟涟涟涟】
引入
想一下,你在京东买东西的时候,是不是总有这样的时候:收货地址或者收货人总是不一样,你有一个自己家的收货地址,还有一个学校的收货地址,甚至还有可能有其他朋友的收货地址。那么我们如何将京东fuliuqingfeng的用户信息及其多个邮寄商品地址保存到数据库当中呢?
初始产品
我们一开始可能会写出下面的代码来实现此功能。
create table user_info(
id char(36) primary key,
user_name varchar(30) not null,
password varchar(30) not null,
real_name varchar(8),
mobile char(11),
address varchar(150)
);
insert into user_info (id,user_name,password,real_name,mobile,address)
values ('51b28fe1-4ebf-41ac-a17b-d5e276861fd0','fuliuqingfeng','123456','张三','18920120206','河南安阳');
insert into user_info (id,user_name,password,real_name,mobile,address)
values ('cc95772b-75a2-4702-bd99-4c3b0322d606','fuliuqingfeng','123456','李四','18617297545','北京海淀');
insert into user_info (id,user_name,password,real_name,mobile,address)
values ('c63028fd-cf8d-4dac-a278-b5cc8fd61e3c','fuliuqingfeng','123456','王五','17694976949','山西大同');
可是我们发现,这样子写的话,代码量太大,数据冗余。耗费时间。那么,我们如何改进呢?
初次改进
我们想到:我们可以把他们拆成两张表,一张放相同的数据,一张放不同的数据。写出代码如下:
create table user_info(
id char(36) primary key,
user_name varchar(30) not null,
password varchar(30) not null
)
insert into user_info (id,user_name,password) values ('51b28fe1-4ebf-41ac-a17b-d5e276861fd0','fuliuqingfeng','123456');
create table address(
id char(36) primary key,
user_info_id char(36),
real_name varchar(8) not null,
mobile char(11) not null,
address varchar(150) not null
)
insert into address (id,user_info_id,real_name,mobile,address)
values ('bfb9472a-7911-4e6f-a479-3b719454ebab','51b28fe1-4ebf-41ac-a17b-d5e276861fd0','张三','18920120206','河南安阳');
insert into address (id,user_info_id,real_name,mobile,address)
values ('5227c6b9-45a2-44aa-8ac0-1f63a38d3b65','51b28fe1-4ebf-41ac-a17b-d5e276861fd0','李四','18617297545','北京海淀');
insert into address (id,user_info_id,real_name,mobile,address)
values ('30b8584b-aa6a-4516-a623-03f487058586','51b28fe1-4ebf-41ac-a17b-d5e276861fd0','王五','17694976949','山西大同');
可是这个样子的话,我们又发现,如果我们在user_info或者address里面添加/修改数据,另一个表不能同步他的信息。
例如:
将user_info中id为51b28fe1-4ebf-41ac-a17b-d5e276861fd0的数据删除,但此时地址表中数据将不再完整
找不到这些地址属于哪个用户;
再例如:
也可以向address表中添加一条user_info_id不存在的地址信息
(如:insert into address (id,user_info_id,real_name,mobile,address) values (‘7da42cc6-36a6-4ad5-9998-71dbc30c8e17’,‘ddc376dd-f8b3-42a6-b42a-db22abed1941’,‘xiaowang’,‘18338970095’,‘北京东城区’);)
——同样,该条数据并不完整,依然找不到这些地址属于哪个用户。
所以,我们仍需要再次改进。
二次改进
我们想到了外键,这种方案为user_info_id添加了外键,指向user_info表的主键,该约束起到了保护数据完整性的作用:
如果删除的用户信息id已经在address表中使用,则该条数据无法删除;无法向address表中添加用户id不存在的地址信息。
create table user_info(
id char(36) primary key,
user_name varchar(30) not null,
password varchar(30) not null
)
insert into user_info (id,user_name,password) values ('51b28fe1-4ebf-41ac-a17b-d5e276861fd0','fuliuqingfeng','123456');
create table address(
id char(36) primary key,
user_info_id char(36),
real_name varchar(8) not null,
mobile char(11) not null,
address varchar(150) not null,
constraint address_user_info_id_fk foreign key(user_info_id) references user_info(id)
)
insert into address (id,user_info_id,real_name,mobile,address)
values ('bfb9472a-7911-4e6f-a479-3b719454ebab','51b28fe1-4ebf-41ac-a17b-d5e276861fd0','张三','18920120206','河南安阳');
insert into address (id,user_info_id,real_name,mobile,address)
values ('5227c6b9-45a2-44aa-8ac0-1f63a38d3b65','51b28fe1-4ebf-41ac-a17b-d5e276861fd0','李四','18617297545','北京海淀');
insert into address (id,user_info_id,real_name,mobile,address)
values ('30b8584b-aa6a-4516-a623-03f487058586','51b28fe1-4ebf-41ac-a17b-d5e276861fd0','王五','17694976949','山西大同');
如果要更改user_info里面的数据,需要将address表中的数据清除才可以,并且执行代码时,一定要是user_info先创建。
如此,我们需要外键。
边栏推荐
- No matching distribution found for flask_ A solution to compat
- Newticker uses
- Why does MySQL index use b+ tree instead of jump table?
- Several rounds of SQL queries in a database
- Binary search decision tree (average search length of binary search tree)
- The song of the virtual idol was originally generated in this way!
- Watermelon book chapter 3 (first & second)
- 银行人脸识别系统被攻破:一储户被偷走 43 万元
- Wechat applet session holding
- 严控室外作业时间!佛山住建局发文:加强高温期间建筑施工安全管理
猜你喜欢

II. Analysis of the execution process of make menuconfig

Photoshop web design tutorial

STS download tutorial (the solution cannot be downloaded on the include official website)
One article to understand the index of like in MySQL

Chapter 8 multithreading

Guangdong: fire safety supervision is no longer "absent" in new industries and new formats such as script killing

Chapter 12 generics

Solution: the idea project does not display a tree view
![[product] about wechat product analysis](/img/b2/e1b32e86e2c991ae5c8f8a71f692fe.png)
[product] about wechat product analysis

J9 number theory: how long is the mainstreaming of decentralized identity?
随机推荐
Openpyxl drawing area map
About the problem that the onapplicationevent method of the custom listener is executed multiple times
Keil mdk5.37 and above can add AC5 (armcc) compiler by themselves
Good looking (dynamic) Jay fan self-made dynamic album card (front and back are different) and lyrics page
[网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
About offline caching application cache / using manifest file caching
[excerpt] [medical image] common DICOM thumbnail interpretation and viewer converter conversion tool
No matching distribution found for flask_ A solution to compat
Chapter 7 exception handling
MySQL common commands
STS download tutorial (the solution cannot be downloaded on the include official website)
Shutter project scrollcontroller attached to multiple scroll views, failed assertion: line 109 POS 12 error handling
HDU1698_ Just a Hook
Detailed explanation of flask framework
Go Beginner (4)
USB network card drive data stream
Implicit indicators for evaluating the advantages and disadvantages of automated testing
系统临时文件的写和读:createTempFile和tempFileContent[通俗易懂]
Redis data type
20210512 recursive formula