当前位置:网站首页>Why do you need foreign keys?
Why do you need foreign keys?
2022-07-27 12:39:00 【Rippling rippling】
introduce
Think about it , When you are shopping in JD , Is there always such a time : The receiving address or consignee is always different , You have a shipping address of your own home , There is also a shipping address of the school , There may even be other friends' shipping addresses . So how do we make jd.com fuliuqingfeng The user information and its multiple mailing addresses are saved in the database ?
The initial product
We may write the following code to realize this function at the beginning .
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',' Zhang San ','18920120206',' Anyang, Henan ');
insert into user_info (id,user_name,password,real_name,mobile,address)
values ('cc95772b-75a2-4702-bd99-4c3b0322d606','fuliuqingfeng','123456',' Li Si ','18617297545',' Beijing haidian ');
insert into user_info (id,user_name,password,real_name,mobile,address)
values ('c63028fd-cf8d-4dac-a278-b5cc8fd61e3c','fuliuqingfeng','123456',' Wang Wu ','17694976949',' Datong, Shanxi ');
But we found that , If you write like this , Too much code , data redundancy . Time consuming . that , How can we improve ?
Initial improvement
We think of : We can split them into two tables , Put the same data on one sheet , Put different data on one sheet . Write the code as follows :
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',' Zhang San ','18920120206',' Anyang, Henan ');
insert into address (id,user_info_id,real_name,mobile,address)
values ('5227c6b9-45a2-44aa-8ac0-1f63a38d3b65','51b28fe1-4ebf-41ac-a17b-d5e276861fd0',' Li Si ','18617297545',' Beijing haidian ');
insert into address (id,user_info_id,real_name,mobile,address)
values ('30b8584b-aa6a-4516-a623-03f487058586','51b28fe1-4ebf-41ac-a17b-d5e276861fd0',' Wang Wu ','17694976949',' Datong, Shanxi ');
But in this case , We found out , If we were user_info perhaps address Add inside / Modifying data , Another table cannot synchronize its information .
for example :
take user_info in id by 51b28fe1-4ebf-41ac-a17b-d5e276861fd0 Data deletion , But at this time, the data in the address table will no longer be complete
Can't find which user these addresses belong to ;
Another example is :
Also you can ask the address Add a user_info_id Nonexistent address information
( Such as :insert into address (id,user_info_id,real_name,mobile,address) values (‘7da42cc6-36a6-4ad5-9998-71dbc30c8e17’,‘ddc376dd-f8b3-42a6-b42a-db22abed1941’,‘xiaowang’,‘18338970095’,‘ Dongcheng District, Beijing ’);)
—— Again , This data is not complete , Still can't find which user these addresses belong to .
therefore , We still need to improve again .
Second improvement
We thought of foreign keys , The solution is user_info_id Added foreign keys , Point to user_info Primary Key , This constraint protects data integrity :
If you delete the user information id Already in address Used in table , Then the data cannot be deleted ; Unable to contact address Add users to the table id Nonexistent address information .
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',' Zhang San ','18920120206',' Anyang, Henan ');
insert into address (id,user_info_id,real_name,mobile,address)
values ('5227c6b9-45a2-44aa-8ac0-1f63a38d3b65','51b28fe1-4ebf-41ac-a17b-d5e276861fd0',' Li Si ','18617297545',' Beijing haidian ');
insert into address (id,user_info_id,real_name,mobile,address)
values ('30b8584b-aa6a-4516-a623-03f487058586','51b28fe1-4ebf-41ac-a17b-d5e276861fd0',' Wang Wu ','17694976949',' Datong, Shanxi ');
If you want to change user_info The data in it , Need to put address Only when the data in the table is cleared , And when executing code , Must be user_info First create .
such , We need foreign keys .
边栏推荐
- 2022 global Vocational Education Industry Development Report
- Watermelon book chapter 3 (first & second)
- Common usage of curl command
- Julia beginner tutorial 2022
- Implicit indicators for evaluating the advantages and disadvantages of automated testing
- About the problem that the onapplicationevent method of the custom listener is executed multiple times
- Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
- POJ1611_ The Suspects
- Self built personalized automatic quotation system to cope with changeable quotation mode
- 2021-3-23-meituan-regular sequence
猜你喜欢

Basic architecture of data Lake

(07) flask is OK if you have a hand -- flask Sqlalchemy

Error: slf4j: class path contains multiple slf4j bindings

Good looking (dynamic) Jay fan self-made dynamic album card (front and back are different) and lyrics page

Openpyxl drawing area map

Chapter 8 multithreading

12 pictures, take you to thoroughly understand ZGC garbage collector!

JS true / false array conversion

SparkSubmit.main()方法提交外部参数,远程提交standalone集群任务

Interviewer: how can you close an order without using a scheduled task?
随机推荐
POJ1988_ Cube Stacking
Why does MySQL index use b+ tree instead of jump table?
浪潮之巅——读书笔记+摘录+感悟
Shutter project scrollcontroller attached to multiple scroll views, failed assertion: line 109 POS 12 error handling
Interviewer: how to deal with the data loss of redis master-slave cluster switching?
BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)
Openpyxl drawing radar map
Good looking (dynamic) Jay fan self-made dynamic album card (front and back are different) and lyrics page
Go Introduction (2)
Play CSDN editor
详述try-catch-finally
Dominoes staged: the beginning and end of the three arrow capital crash
Common usage of curl command
Photoshop web design tutorial
Detailed explanation of flask framework
CMD Chinese garbled code solution
CEPH distributed storage performance tuning (6)
Complete data summary of lapsus$apt organization that stole Microsoft's source code in March 2022
Soft core microprocessor
Application parameters of Southern biotech HRP labeled avidin avidin avidin