SQL OUTER JOIN概述和示例

本文详述了SQL外连接,包括全外连接、左外连接和右外连接的概念和示例。通过实例展示了如何从多个表中获取数据,解释了外连接在数据库中的作用,特别是对于规范化数据表的重要意义。同时,通过Venn图直观地解释了不同类型的外连接如何处理匹配和不匹配的行。最后,讨论了左外连接和右外连接的联合,相当于全外连接的效果。
摘要由CSDN通过智能技术生成

This article will provide a full overview, with examples of the SQL Outer join, including the full, right and left outer join as well as cover the union between SQL left and right outer joins.

本文将提供完整的概述,并提供SQL外连接示例,包括完整,右侧和左侧外部连接,以及SQL左侧和右侧外部连接之间的联合。

It is essential to understand the process to get the data from the multiple tables. A beginner might not have the idea of Joins in SQL Server. In this tip, we will take an overview of the SQL joins, learn SQL OUTER JOIN along with its syntax, examples, and use cases.

必须了解从多个表中获取数据的过程。 初学者可能没有在SQL Server中加入连接的想法。 在本文中,我们将概述SQL连接,学习SQL OUTER JOIN及其语法,示例和用例。

In a relational database system, it is best practice to follow the principles of Normalization, in which, basically, we split large tables into the smaller tables. In a select statement, we can retrieve the data from these tables using joins. We can join the tables and get the required fields from these tables in the result set. These tables should have some common field to relate with each other. You might find data split across multiple databases and sometimes it is a very complex structure as well. With Joins, we can join the data together from the multiple tables, databases into a user-friendly way and represent this data in the application.

在关系数据库系统中,最佳实践是遵循规范化原则,在该原则中,我们基本上将大表拆分为较小的表。 在select语句中,我们可以使用联接从这些表中检索数据。 我们可以联接表并从结果集中的这些表中获取必填字段。 这些表应具有一些相互关联的公共字段。 您可能会发现数据分散在多个数据库中,有时它也是一个非常复杂的结构。 使用Joins,我们可以将来自多个表,数据库的数据连接在一起,以一种用户友好的方式进行,并在应用程序中表示该数据。

We can represent a SQL JOIN using the following image

我们可以使用下图表示一个SQL JOIN

SQL JOIN

We can many SQL Join types in SQL Server. In the following image, you can see SQL Joins categories

我们可以在SQL Server中使用许多SQL Join类型。 在下图中,您可以看到SQL Joins类别

SQL Join types

Let’s explore SQL Outer Join in details in the upcoming section.

让我们在接下来的部分中详细探讨SQL外连接。

SQL OUTER JOIN概述 (Overview of the SQL OUTER JOIN)

We use the SQL OUTER JOIN to match rows between tables. We might want to get match rows along with unmatched rows as well from one or both of the tables. We have the following three types of SQL OUTER JOINS.

我们使用SQL OUTER JOIN来匹配表之间的行。 我们可能想从一个或两个表中获取匹配行以及不匹配行。 我们有以下三种类型SQL OUTER JOINS。

  1. SQL Full Outer Join

    SQL Full外连接
  2. SQL Left Outer Join

    SQL左外部联接
  3. SQL Right Outer Join

    SQL右外部联接

Let’s explore each of SQL Outer Join with examples.

让我们通过示例探索每个SQL Outer Join。

SQL Full外连接 (SQL Full Outer Join)

In SQL Full Outer Join, all rows from both the tables are included. If there are any unmatched rows, it shows NULL values for them.

在SQL Full Outer Join中,两个表中的所有行都包括在内。 如果有任何不匹配的行,则显示它们的NULL值。

We can understand efficiently using examples. Let’s create a sample table and insert data into it.

我们可以使用示例来有效地理解。 让我们创建一个示例表并将数据插入其中。

CREATE TABLE [dbo].[Employee](
  [EmpID] [int] IDENTITY(1,1) PRIMARY KEY CLUSTERED,
  [EmpName] [varchar](50) NULL,
  [City] [varchar](30) NULL,
  [Designation] [varchar](30) NULL]
)
 
CREATE TABLE Departments
(EmpID          INT  PRIMARY KEY CLUSTERED , 
 DepartmentID  INT, 
 DepartmentName VARCHAR(50)
);

You can refer the following data model of both the tables.

您可以参考以下两个表的数据模型。

Table models

Insert data into the Employee table with the following script.

使用以下脚本将数据插入Employee表。

Sample data
USE [SQLShackDemo];
GO
SET IDENTITY_INSERT [dbo].[Employee] ON; 
GO
INSERT INTO [dbo].[Employee]
([EmpID], 
 [EmpName], 
 [City], 
 [Designat
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值