数据库 嵌套查询

加盟业务 (Join operations)

Join operations and nested queries both works to combine the data, which is allocated in different tables to make a single result. Both the join and nested query operations are similar but there are some differences which help a user to choose between them on specific situations.

连接操作和嵌套查询都可以合并数据,这些数据分配在不同的表中以产生单个结果。 连接和嵌套查询操作都相似,但是存在一些差异,可以帮助用户在特定情况下在它们之间进行选择。

Let's look at how join works and what are its types?

让我们看看联接的工作原理以及它的类型是什么?

A join operation is used to combine related tuples or rows from two or more different tables or relations, if and only if, the condition which is given is satisfied.

当且仅当满足给定条件时,联接操作才用于组合来自两个或多个不同表或关系的相关元组或行。

Join operations are of two types,

连接操作有两种类型,

  1. Inner join

    内部联接

    1. Theta join
    2. Equi join
    3. Natural join
  2. Outer join

    外加入

    1. Left outer join
    2. Right outer join
    3. Full outer join

1)内部加入 (1) Inner join)

An Inner Join is used when a user has to combine related tuples with similar attributes and the tuples which are left are discarded in resulting table or relation.

当用户必须将具有相似属性的相关元组合并,而剩下的元组在结果表或关系中被丢弃时,将使用内部联接

a) Theta join: Theta join is used when a user has to combine tuples from different tables or relations, If and only if they satisfy the theta condition. It is denoted by θ. Theta Join can use all types of comparative operators.

a)Theta联接 :当用户必须组合来自不同表或关系的元组时,且仅当它们满足theta条件时,才使用Theta联接。 用θ表示。 Theta Join可以使用所有类型的比较运算符。

Example: Suppose T1(A1, A2, ..., AN) and T2(B1, B2, ..., BN) are two tables given whose not a single value of attributes is same then the combined result will be T1 union T2= θ.

示例 :假设给定T1(A1,A2,...,AN)和T2(B1,B2,...,BN)是两个表,它们的单个属性值都不相同,那么合并后的结果将为T1并集T2 =θ 。

b) Equi Join: Equi Join is used when a user has to use equality operator in Theta join.

b)等值连接 :当用户必须在Theta连接中使用等值运算符时,将使用等值连接。

Example: Two tables are given Employee and Project.

示例:给两个表Employee和Project。

Employee:

雇员:

Emp.I.d. Name Section
202 Anubhav verma 2B
203 Smita singh 3B
Emp.Id 名称 部分
202 阿努巴夫维玛 2B
203 史密塔·辛格(Smita Singh) 3B

Project:

项目:

Standard Projects
2B C++
2B Python
3B Java
3B Adobe
标准 专案
2B C ++
2B Python
3B Java
3B 土坯

After combining them through Equi Join, we get

通过Equi Join合并它们后,我们得到

Employee Details:

员工详细信息:

Emp.I.d. Name Section Standard Projects
202 Anubhav verma 2B 2B C++
202 Anubhav verma 2B 2B Python
203 Smita singh 3B 3B Java
203 Smita singh 3B 3B Adobe
Emp.Id 名称 部分 标准 专案
202 阿努巴夫维玛 2B 2B C ++
202 阿努巴夫维玛 2B 2B Python
203 史密塔·辛格(Smita Singh) 3B 3B Java
203 史密塔·辛格(Smita Singh) 3B 3B 土坯

C) Natural Join: Natural Join is used when there has to be at least one common tuples attribute between two tables or relations. The attributes must have the same name and domain. Natural Join does not use any comparative operator. Natural Join works between those tuples whose attributes values are the same in both the tables and relations. It is denoted by .

C)自然联接 :当两个表或关系之间必须至少有一个公共元组属性时,使用自然联接。 这些属性必须具有相同的名称和域。 自然联接不使用任何比较运算符。 自然联接在表和关系中属性值相同的那些元组之间工作。 用表示。

Example: Two tables are given Student and Subject.

示例:给了两个表Student和Subject。

Student:

学生:

Roll no. Name Class
4 Aman 10
7 Raghav 11
8 Sameer 12
卷号 名称
4 阿曼 10
7 拉加夫 11
8 沙美尔 12

Subject:

学科:

Class Subjects
10 Maths
11 Science
12 Biology
科目
10 数学
11 科学
12 生物学

The Resulting table is:

结果表为:

Student ⋈ Subjects

学生⋈科目

Roll no. Name Class Subjects
4 Aman 10 Maths
7 Raghav 11 Science
8 Sameer 12 Biology
卷号 名称 科目
4 阿曼 10 数学
7 拉加夫 11 科学
8 沙美尔 12 生物学

2)外加入 (2) Outer Join)

An Outer Join is used when a user has to combine all the tuples with attributes without any discarding in resulting table or relation.

当用户必须将所有元组与属性组合而不会在结果表或关系中进行任何丢弃时,将使用外部联接。

a) Left outer join: This operation is used when the user has to include tuples from the Left table in the resulting table. If the tuples in the Right side of the table are not similar to the Left side of the table then the tuples of the right side are made NULL.

a)左外部联接 :当用户必须在结果表中包括“左”表中的元组时,将使用此操作。 如果表右侧的元组与表左侧的元组不同,则将右侧的元组设为NULL。

Example,

例,

Left Side Table:

左侧桌:

A B
203 Taj Palace
205 Royal plaza
207 Hotel Villas
一个
203 泰姬陵宫
205 皇家广场
207 别墅酒店

Right Side Table:

右侧表:

A B
203 Karan
204 Raghubir
205 Satyam
一个
203 卡兰
204 拉格比尔
205 萨蒂扬

Resultant Table:

结果表:

A B C D
203 Taj Palace 203 Karan
205 Royal plaza 205 Satyam
207 Hotel Villas --- ---
一个 C d
203 泰姬陵宫 203 卡兰
205 皇家广场 205 萨蒂扬
207 别墅酒店 --- ---

b) Right outer join: This operation is used when the user has to include tuples from the right table in the resulting table. If the tuples in the left side of the table are not similar to the right side of the table then the tuples of the right side are made NULL.

b)右外部联接:当用户必须在结果表中包括来自右表的元组时,使用此操作。 如果表左侧的元组与表的右侧不相似,则将右侧的元组设为NULL。

Example,

例,

Left Side Table:

左侧桌:

A B
203 Taj Palace
205 Royal plaza
207 Hotel Villas
一个
203 泰姬陵宫
205 皇家广场
207 别墅酒店

Right Side Table:

右侧表:

A B
203 Karan
204 Raghubir
205 Satyam
一个
203 卡兰
204 拉格比尔
205 萨蒂扬

Resultant Table:

结果表:

A B C D
203 Taj Palace 203 Karan
--- --- 204 Raghubir
205 Taj Palace 205 Satyam
一个 C d
203 泰姬陵宫 203 卡兰
--- --- 204 拉格比尔
205 泰姬陵宫 205 萨蒂扬

c) Full outer join: This operation is used when the user has to include tuples from both sides in the resulting table. If the tuples are not the same in both the tables, then the not similar attributes are made NULL.

c)完全外部联接:当用户必须在结果表的两侧都包含元组时,将使用此操作。 如果两个表中的元组都不相同,则将不相似的属性设置为NULL。

Example,

例,

Left Side Table:

左侧桌:

A B
203 Taj Palace
205 Royal plaza
207 Hotel Villas
一个
203 泰姬陵宫
205 皇家广场
207 别墅酒店

Right Side Table:

右侧表:

A B
203 Karan
204 Raghubir
205 Satyam
一个
203 卡兰
204 拉格比尔
205 萨蒂扬

Resultant Table:

结果表:

A B C D
203 Taj Palace 203 Karan
--- --- 204 Raghubir
205 Taj Palace 205 Satyam
207 Hotel Villas --- ---
一个 C d
203 泰姬陵宫 203 卡兰
--- --- 204 拉格比尔
205 泰姬陵宫 205 萨蒂扬
207 别墅酒店 --- ---

Let's look at how nested query works?

让我们看看嵌套查询是如何工作的?

嵌套查询 (Nested query)

A Nested query also is known as Subquery, sub-selector, an inner query is a SELECT query within another SQL query, which is embedded within the WHERE or HAVING Clause.

嵌套查询也称为Subquerysub-selector ,内部查询是另一个SQL查询中的SELECT查询,该查询嵌入在WHERE或HAVING子句中。

The data which is used in Nested query is returned by the nested query and used in the form of the condition in the main query, which further restricts so that the retrieval of the data should not be followed.

嵌套查询返回的嵌套查询中使用的数据以主查询中条件的形式使用,这进一步受到限制,因此不应遵循对数据的检索。

The rules which are followed by nested queries are,

嵌套查询遵循的规则是:

  1. A nested query must always be enclosed within parentheses.

    嵌套查询必须始终用括号括起来。

  2. An ORDER BY command can be used by the main query but not by the nested query. For the ORDER BY command, in nested query GROUP BY command can be used to perform the same function.

    主查询可以使用ORDER BY命令,但嵌套查询不能使用。 对于ORDER BY命令,可以在嵌套查询GROUP BY命令中执行相同的功能。

  3. For the Column purpose, A nested query must return a single column within the Select* Clause.

    出于列目的,嵌套查询必须在Select *子句中返回单个列。

  4. For the Row purpose, a nested query may use to return multiple columns.

    出于行目的,嵌套查询可用于返回多个列。

  5. The nested queries that return more than one row can only be used with multiple value operators, such as IN or NOT IN operator.

    返回多行的嵌套查询只能与多个值运算符一起使用,例如IN或NOT IN运算符。

  6. Those values that evaluate to a BLOB, ARRAY, CLOB, or NCLOB cannot include any references in the SELECT LIST.

    那些评估为BLOB,ARRAY,CLOB或NCLOB的值不能在SELECT LIST中包含任何引用。

  7. A nested query cannot form towards a union. Only the statement, which is a single SELECT, is allowed.

    嵌套查询无法形成并集。 仅允许单个SELECT语句。

  8. A nested query cannot be appearing within a set function immediately.

    嵌套查询不能立即出现在set函数中。

  9. Within the nested query, The BETWEEN operator can be used but not with a nested query.

    在嵌套查询中,可以使用BETWEEN运算符,但不能与嵌套查询一起使用。

Repeatedly, Nested queries are mostly used with the SELECT statement. They can also be used within another nested query, INSERT, UPDATE or DELETE.

重复地, 嵌套查询通常与SELECT语句一起使用。 它们也可以在另一个嵌套查询INSERT,UPDATE或DELETE中使用。

带有SELECT语句的嵌套查询 (Nested queries with the SELECT statement)

Nested queries are most often used with the SELECT statement. Its syntax is as follows,

嵌套查询最常与SELECT语句一起使用。 其语法如下,

Example: The Engineers table with their salaries record is given,

示例:给出了带薪水记录的Engineers表,

ID Name Age Address Salary
1 Arun 34 Kanpur 30,000
2 Kamal 23 Lucknow 34,000
3 Ajay 32 Mumbai 25,000
4 Shubham 28 Delhi 26,000
5 Anurag 26 Bangalore 24,000
6 Shivam 27 Hyderabad 23,000
7 karan 24 Noida 32,000
8 Himanshu 33 Chennai 20,000
ID 名称 年龄 地址 薪水
1个 阿伦 34 坎普尔 30,000
2 卡马尔 23 拉克瑙 34,000
3 阿杰 32 孟买 25,000
4 Shubham 28 新德里 26,000
5 阿努拉格 26 班加罗尔 24,000
6 湿婆 27 海得拉巴 23,000
7 卡兰 24 野田 32,000
8 Himanshu 33 钦奈 20,000

Now, The Nested query for the following records,

现在,嵌套查询以下记录,

SQL> SELECT *
FROM Engineers
WHERE ID IN (SELECT ID
FROM Engineers
WHERE SALARY > 26,000) ;

The result is,

结果是

ID Name Age Address Salary
1 Arun 34 Kanpur 30,000
2 Kamal 23 Lucknow 34,000
7 karan 24 Noida 32,000
ID 名称 年龄 地址 薪水
1个 阿伦 34 坎普尔 30,000
2 卡马尔 23 拉克瑙 34,000
7 卡兰 24 野田 32,000

带有UPDATE语句的嵌套查询 (Nested queries with the UPDATE statement)

The nested query can be used with the UPDATE statement in Conjunction. By using the nested query with the UPDATE statement, we can update either single or multiple columns in the table.

嵌套查询可以与Conjunction中的UPDATE语句一起使用。 通过将嵌套查询与UPDATE语句一起使用,我们可以更新表中的单列或多列。

Example: Suppose the Engineers_rally table is already given with the data of Engineers table. Now through the nested query, we are going to update the salary section by 2 times in the Engineers table for all those engineers whose age is greater than 27.

例如:假设Engineers_rally表已经与工程师表的数据给出。 现在,通过嵌套查询,对于年龄在27岁以上的所有工程师,我们将在Engineers表中将薪金部分更新2倍。

QUERY: Nested query to update the data from one table to another

查询:嵌套查询,用于将数据从一个表更新到另一个表

SQL> UPDATE Engineers
SET SALARY= SALARY * 2
WHERE AGE IN (SELECT AGE FROM Engineers_rally)
WHERE AGE >= 27);

The result is,

结果是

ID Name Age Address Salary
1 Arun 34 Kanpur 30,000
2 Kamal 23 Lucknow 34,000
3 Ajay 32 Mumbai 25,000
4 Shubham 28 Delhi 26,000
5 Anurag 26 Bangalore 24,000
6 Shivam 27 Hyderabad 23,000
7 karan 24 Noida 32,000
8 Himanshu 33 Chennai 20,000
ID 名称 年龄 地址 薪水
1个 阿伦 34 坎普尔 30,000
2 卡马尔 23 拉克瑙 34,000
3 阿杰 32 孟买 25,000
4 Shubham 28 新德里 26,000
5 阿努拉格 26 班加罗尔 24,000
6 湿婆 27 海得拉巴 23,000
7 卡兰 24 野田 32,000
8 Himanshu 33 钦奈 20,000

带DELETE语句的嵌套查询 (Nested queries with the DELETE statement)

The Nested query can be used with the DELETE statement in conjunction as the way it was used above with other statements.

嵌套查询可以与DELETE语句一起使用,就像上面与其他语句一起使用时一样。

Example: Suppose the Engineers_rally table is already given with the data of Engineers table. Now through the nested query, we are going to delete the records from the Engineers table of those engineers whose age is greater than 27.

例如 :假设Engineers_rally表已经与工程师表的数据给出。 现在,通过嵌套查询,我们将从年龄超过27岁的工程师的工程师表中删除记录。

Nested query to delete the data from the table,

嵌套查询以从表中删除数据,

SQL> DELETE FROM Engineers
WHERE AGE IN (SELECT AGE FROM Engineers_rally
WHERE AGE >= 27);

The result is,

结果是

ID Name Age Address Salary
2 Kamal 23 Lucknow 34,000
5 Anurag 26 Bangalore 24,000
7 karan 24 Noida 32,000
ID 名称 年龄 地址 薪水
2 卡马尔 23 拉克瑙 34,000
5 阿努拉格 26 班加罗尔 24,000
7 卡兰 24 野田 32,000

连接操作和嵌套查询之间的区别 (Differences between join operations and nested queries)

Now, the differences which we got from the above data between join operations and nested queries are:

现在,我们从上述数据中得到的联接操作和嵌套查询之间的区别是:

Join operations Nested queries
Join operations are better in optimization. Nested queries are not better in optimization.
Join operations takes more time because they fetch whole table data with attributes. Nested queries take less time because they fetch only relevant data from the tables.
Join operations return index data, So on larger dataset working on them is faster. Nested queries return set of data with no cache facility, So on larger dataset working on them is slower.
Joins operations are not easier to read, understand and evaluate. Nested queries are easier to read, understand and evaluate.
Join operations can be used in return rows. Nested queries can be used to return either a scalar value or row set.
Join operations are powerful relational operators Nested queries are not powerful relational operators.
加盟业务 嵌套查询
连接操作在优化方面更好。 嵌套查询在优化方面并不更好。
联接操作会花费更多时间,因为它们会获取具有属性的整个表数据。 嵌套查询花费的时间更少,因为它们仅从表中获取相关数据。
联接操作返回索引数据,因此在较大的数据集上运行速度更快。 嵌套查询返回的数据集没有缓存功能,因此在较大的数据集上运行速度较慢。
联接操作不容易阅读,理解和评估。 嵌套查询更易于阅读,理解和评估。
连接操作可用于返回行。 嵌套查询可用于返回标量值或行集。
联接操作是强大的关系运算符 嵌套查询不是强大的关系运算符。

翻译自: https://www.includehelp.com/dbms/join-operation-vs-nested-query.aspx

数据库 嵌套查询

数据库 嵌套查询_联接操作与嵌套查询 数据库管理系统相关推荐

  1. graphql 嵌套查询_了解GraphQL中的查询

    graphql 嵌套查询 介绍 (Introduction) In this tutorial, we'll take an in-depth look at queries in GraphQL s ...

  2. sql镶嵌查询_【SQL】嵌套查询与子查询

    前言 sql的嵌套查询可以说是sql语句中比较复杂的一部分,但是掌握好了的话就可以提高查询效率.下面将介绍带in的子查询.带比较运算符的子查询.带any/all的子查询.带exists的子查询以及基于 ...

  3. 执行还原操作_【操作技巧】SQL2014数据库存储路径的修改方法

    随着数据库数据量的不断增加,如果造成磁盘空间不足需要修改数据存储路径时,下面提供两种操作方法. 方法一:备份更改存储路径 1. 将数据库做备份. 2. 在准备将数据库移动到的目标盘符中建立一个明显的文 ...

  4. winform下拉框模糊查询_怎样在c#中实现数据库数据的模糊查询

    我想在vs的 dataGridView中 显示查询的内容. 具体是 在文本框中输入一个关键字,然后在数据库的一个表的所有字段进行查询,显示在dataGridView,麻烦大家帮忙解答一下. (下面的是 ...

  5. 清空数据库事务日志_通过事务日志增长加快数据库恢复和长期运行的事务

    清空数据库事务日志 In my previous article in this series Accelerated Database Recovery; Instant Rollback and ...

  6. dapper mysql 多参数查询_使用Dapper进行参数化查询

    在使用Dapper操作Mysql数据库中我介绍了使用dapper进行CURD基本操作,但在示例代码中参数虽然也是通过@开头,但其实不是真正意义的参数化查询,而是拼接sql,这种方式不利于防止sql注入 ...

  7. 电脑上mysql数据库无法登录_无法远程登入MySQL数据库的几种解决办法MySQL综合 -电脑资料...

    方法一: 尝试用MySQL Adminstrator GUI Tool登入MySQL Server,Server却回复错误讯息:Host '60-248-32-13.HINET-IP.hinet.ne ...

  8. mysql不同服务器查询_实战操作SQL Server连接查询不同服务器表数据

    今日产品部要导批数据,但是需要连接查询查询的几个表不在同一服务器上.所以我开始是这么干的: 1.查询一台服务器的数据,并导入本地Excel 2.查询另一台服务器的数据,并导入本地Excel 3.Exc ...

  9. 服务器重启oracle数据库服务器,oracle数据库怎么重启_网站服务器运行维护,oracle,数据库,重启...

    linux操作系统好学吗_网站服务器运行维护 学习大多类似鹿丁解牛,对事物的认识一般都是由浅入深.由表及里的过程,循序才能渐进.学习Linux同样要有一定的顺序和方法,这样学起来就不会感觉到难了. o ...

最新文章

  1. 软件工程几点总结要点
  2. 钉钉需要什么java知识_Java钉钉开发_01_开发前的准备
  3. c#图片base64去转义字符_C#实现字符串与图片的Base64编码转换操作示例|chu
  4. MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
  5. 手势模型和Angular Material的实现
  6. 随手看的一本书《java微服务》,测试成功了其中的第一个样例
  7. python开发视频大全_2019年python开发编程21天快速入门视频教程+书籍大全和面试大礼包...
  8. aes默认填充算法 mysql_Go 实现加密算法系列之对称加密
  9. 查看计算机本机IP地址,本机ip地址查询
  10. 基于R语言的方差分析
  11. 电脑提示文件或目录损坏且无法读取
  12. 《Pajek社会网络探索性分析》书籍简介
  13. 毁三观:妻妹把我压身下
  14. 灵敏度分享码显示服务器不可用,和平精英ss12最稳灵敏度设置方法介绍-2021灵敏度分享码...
  15. oracle中取月初和月末的方法
  16. 32位eclipse使用64位jdk问题
  17. Mac蒲公英sh脚本上传app
  18. 基本触发器和钟控触发器
  19. 平滑处理--拉普拉斯(Laplace Smoothing)
  20. 高一末计算机考试试题,高一计算机第一学期期末试题

热门文章

  1. 大学物理一些公式汇总
  2. Aliexpress速卖通使用虚拟信用卡下单测评教程和注意事项
  3. 【Docker】安装mysql,redis
  4. 〔摘录转载〕字体彩蛋
  5. Java集合源码解析
  6. STM32F103学习记录——通过额外添加串口接口来接入ZigBee/蓝牙模块实现数据的远程传输
  7. 上海··高房价的城市
  8. 【刷题日记】贪心算法经典题目
  9. 深度分析|DEVITA:一个基于区块链技术和Web3生态打造的先进数字医疗平台
  10. Matlab中常见实用函数(敲代码碰到的)