自然连接和内部连接之间的区别是什么?
当前回答
内部连接是这样一种连接,即连接表中的匹配行对于返回的第一个表中的一行是必需的 外部连接是这样一种连接,即对于返回的第一个表中的一行,已连接表中的匹配行不需要 自然连接是一种连接(可以是自然左连接,也可以是自然右连接),它假设连接条件是两个表中同名列匹配的位置
我会避免像使用瘟疫一样使用自然连接,因为自然连接是:
not standard sql [SQL 92] and therefore not portable, not particularly readable (by most SQL coders) and possibly not supported by various tools/libraries not informative; you can't tell what columns are being joined on without referring to the schema your join conditions are invisibly vulnerable to schema changes - if there are multiple natural join columns and one such column is removed from a table, the query will still execute, but probably not correctly and this change in behaviour will be silent hardly worth the effort; you're only saving about 10 seconds of typing
其他回答
内部连接是这样一种连接,即连接表中的匹配行对于返回的第一个表中的一行是必需的 外部连接是这样一种连接,即对于返回的第一个表中的一行,已连接表中的匹配行不需要 自然连接是一种连接(可以是自然左连接,也可以是自然右连接),它假设连接条件是两个表中同名列匹配的位置
我会避免像使用瘟疫一样使用自然连接,因为自然连接是:
not standard sql [SQL 92] and therefore not portable, not particularly readable (by most SQL coders) and possibly not supported by various tools/libraries not informative; you can't tell what columns are being joined on without referring to the schema your join conditions are invisibly vulnerable to schema changes - if there are multiple natural join columns and one such column is removed from a table, the query will still execute, but probably not correctly and this change in behaviour will be silent hardly worth the effort; you're only saving about 10 seconds of typing
自然连接:SQL Join子句组合关系数据库中2个或多个表的字段。自然连接基于两个表中具有相同名称的所有列,以及两个表中所有匹配列中具有相等值的选定行。
—两个列的名称和数据类型必须相同。
使用子句:在自然连接中,如果表具有相同名称但数据类型不同的列,则连接会导致错误。为了避免这种情况,可以使用USING子句修改join子句。USING子句指定应用于连接的列。
自然连接只是一种避免输入的快捷方式,假设连接是简单的,并且匹配相同名称的字段。
SELECT
*
FROM
table1
NATURAL JOIN
table2
-- implicitly uses `room_number` to join
和……一样。
SELECT
*
FROM
table1
INNER JOIN
table2
ON table1.room_number = table2.room_number
然而,你不能用快捷格式做的是更复杂的连接…
SELECT
*
FROM
table1
INNER JOIN
table2
ON (table1.room_number = table2.room_number)
OR (table1.room_number IS NULL AND table2.room_number IS NULL)
NATURAL连接只是特定INNER连接(或“等价连接”)的简短语法,一旦语法被解开,两者都表示相同的关系代数操作。它不是一种“不同类型”的连接,就像OUTER(左/右)或CROSS连接一样。
参见维基百科上的equal -join部分:
自然连接提供了等价连接的进一步专门化。通过比较两个表中在连接表中具有相同列名的所有列,隐式地产生连接谓词。对于每对名称相同的列,生成的连接表只包含一列。 大多数专家都认为天然接头是危险的,因此强烈反对使用。危险来自于无意中添加了一个与另一个列同名的新列……
也就是说,所有的NATURAL连接都可以写成INNER连接(反之则不然)。为此,只需显式地创建谓词——例如USING或ON——并且,如Jonathan Leffler所指出的,选择所需的结果集列以避免“重复”。
快乐的编码。
(NATURAL关键字也可以应用于LEFT和RIGHT连接,同样适用。自然左/右连接只是特定左/右连接的简短语法。)
mysql> SELECT * FROM tb1 ;
+----+------+
| id | num |
+----+------+
| 6 | 60 |
| 7 | 70 |
| 8 | 80 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+------+
6 rows in set (0.00 sec)
mysql> SELECT * FROM tb2 ;
+----+------+
| id | num |
+----+------+
| 4 | 40 |
| 5 | 50 |
| 9 | 90 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+------+
6 rows in set (0.00 sec)
内连接:
mysql> SELECT * FROM tb1 JOIN tb2 ;
+----+------+----+------+
| id | num | id | num |
+----+------+----+------+
| 6 | 60 | 4 | 40 |
| 7 | 70 | 4 | 40 |
| 8 | 80 | 4 | 40 |
| 1 | 1 | 4 | 40 |
| 2 | 2 | 4 | 40 |
| 3 | 3 | 4 | 40 |
| 6 | 60 | 5 | 50 |
| 7 | 70 | 5 | 50 |
| 8 | 80 | 5 | 50 |
.......more......
return 36 rows in set (0.01 sec)
AND NATURAL JOIN :
mysql> SELECT * FROM tb1 NATURAL JOIN tb2 ;
+----+------+
| id | num |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+------+
3 rows in set (0.01 sec)
推荐文章
- 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
- 查询JSON类型内的数组元素
- 确定记录是否存在的最快方法
- 获得PostgreSQL数据库中当前连接数的正确查询
- 在SQL选择语句Order By 1的目的是什么?
- 我如何循环通过一组记录在SQL Server?
- 如何从命令行通过mysql运行一个查询?
- 外键约束可能导致循环或多条级联路径?
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何选择每一行的列值不是独特的
- 如何改变字符集(和排序)在整个数据库?
- mySQL:: insert到表,数据从另一个表?
- 如何在Postgres中获得两个字段的MIN() ?