当前位置:网站首页>Simple explanation of database table connection
Simple explanation of database table connection
2022-07-27 01:05:00 【hayhead】
The hardest part about relational databases , It's modeling (model).
Intricate data , You need to model , To be stored in a database . So-called " Model " There are two things : Entity (entity)+ Relationship (relationship).
Entities refer to the actual objects , With its own properties , Containers that can be understood as a set of related properties . Relationship is the connection between entities , Usually it can be divided into " one-on-one "、" One to many " and " Many to many " Other types .

In a relational database , Each entity has its own table (table), All properties are fields of this table (field), Table to table according to the associated fields " Connect "(join) together . therefore , Table join is the core problem of relational database .
There are several types of table connections .
- Internal connection (inner join)
- External connection (outer join)
- Left connection (left join)
- The right connection (right join)
- Full connection (full join)
before , Many articles use Venntu ( The set operation of two circles ), Explain the differences between the different connections .




Last week, , I read An article , Think there's a better way to explain it than Venn diagram . I found that it was , Explain it in a different way , Easier to understand .
So-called " Connect ", Two tables are based on the associated fields , Combine into a data set . The problem is , The values of the associated fields in the two tables are often inconsistent , If the associated fields do not match , How to deal with ? such as , surface A Including Zhang San and Li Si , surface B Including Li Si and Wang Wu , The only match is the record of Li Si .
It's easy to see , There are four ways to deal with it .
- Only records matching two tables are returned , This is called internal connection (inner join).
- Return matching records , And the watch A Redundant records , This is called left connection (left join).
- Return matching records , And the watch B Redundant records , This is called right connection (right join).
- Return matching records , And the watch A And table B Their own redundant records , This is called full connection (full join).
The figure below shows the four connections . I think , This picture is easier to understand than Venn's , It's more accurate .

Above picture , surface A The record of is 123, surface B The record of is ABC, Color means match . Return the result , If the other table does not have a matching record , Then use null fill .
These four connections , It can be divided into two categories : Internal connection (inner join) Indicates that only matching records are included , External connection (outer join) Indicates that there are also mismatched records . therefore , Left connection 、 The right connection 、 All connections belong to external connections .
These four connected SQL The statement is as follows .
SELECT * FROM A INNER JOIN B ON A.book_id=B.book_id;
SELECT * FROM A
LEFT JOIN B ON A.book_id=B.book_id;
SELECT * FROM A
RIGHT JOIN B ON A.book_id=B.book_id;
SELECT * FROM A
FULL JOIN B ON A.book_id=B.book_id;
above SQL You can also add where Conditional clause , Screening records , For example, just return to the table A It doesn't match the table B The record of .
SELECT * FROM A LEFT JOIN B ON A.book_id=B.book_id WHERE B.id IS null;
Another example , Go back to the table A Or table B All mismatched records .
SELECT * FROM A FULL JOIN B ON A.book_id=B.book_id WHERE A.id IS null OR B.id IS null;
Besides , There is also a special connection , be called " Cross connect "(cross join), It means the watch A And table B There are no associated fields , At this time, the table A( share n Bar record ) And watch B ( share m Bar record ) After connection , Will produce a sheet containing n x m New table of records ( See the picture below ).

边栏推荐
- MySQL uses and implements ranking functions rank and deny_ Rank and row_ NUMBER
- 腾讯云直播插件MLVB如何借助这些优势成为主播直播推拉流的神助攻?
- 深入理解Golang - 闭包
- Flink checkpoint源码理解
- 不止直播:腾讯云直播MLVB 插件除了推流/拉流还有哪些亮眼功能
- [ciscn2019 North China division Day1 web2]ikun
- Flink面试常见的25个问题(无答案)
- [CISCN2019 华北赛区 Day1 Web2]ikun
- Dataframe of sparksql
- [BJDCTF2020]EzPHP
猜你喜欢
![[By Pass] WAF 的绕过方式](/img/dd/7204b2401a9f18c02c8b9897258905.png)
[By Pass] WAF 的绕过方式

Golang切片make与new的区别

Flink 1.15 implements SQL script to recover data from savepointh

2022.7.13
![[ciscn2019 finals Day2 web1]easyweb](/img/36/1ca4b6cae4e0dda0916b511d4bcd9f.png)
[ciscn2019 finals Day2 web1]easyweb
![[CTF 真题] 2018-网鼎杯-Web-Unfinish](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[CTF 真题] 2018-网鼎杯-Web-Unfinish

深入理解Golang - 闭包
![[RootersCTF2019]I_< 3_ Flask](/img/69/1c77e45e939cf86bb75be8a6c42574.png)
[RootersCTF2019]I_< 3_ Flask

One of the Flink requirements - processfunction (requirement: alarm if the temperature rises continuously within 30 seconds)
![[hongminggu CTF 2021] write_ shell](/img/f5/c3a771ab7b40311e37a056defcbd78.png)
[hongminggu CTF 2021] write_ shell
随机推荐
MySQL index optimization: scenarios where the index fails and is not suitable for indexing
Learn json.stringify again
通过FlinkCDC将MySQL中变更的数据写入到kafka(DataStream方式)
(Spark调优~)算子的合理选择
网站日志采集和分析流程
Spark数据倾斜解决办法
Solve the problem that there is no ado.net entity data model in vs
Scala pattern matching
Flink中的状态管理
[SQL注入] 联合查询
移动直播选择 RTMP 还是RTC协议
当事务遇上分布式锁
10 Web APIs
Spark data skew solution
logback自定义MessageConverter
Redisson working principle - source code analysis
Neo4j基础指南(安装,节点和关系数据导入,数据查询)
JSCORE day_ 04(7.5)
不止直播:腾讯云直播MLVB 插件除了推流/拉流还有哪些亮眼功能
2022.7.13