自然连接和内部连接之间的区别是什么?
当前回答
内联接,联接两个列名相同的表。
自然连接,连接两个列名和数据类型相同的表。
其他回答
不同的是,内部(equi/default)连接和自然连接,自然连接公共列中的win将在单次显示,但内部/equi/default/简单连接公共列将显示两次。
内连接和自然连接基本相同,但有细微的区别。不同的是,在自然连接中不需要指定条件,但在内部连接中条件是必须的。如果我们在内连接中指定条件,则生成的表就像笛卡尔积。
SQL在很多方面并不忠实于关系模型。SQL查询的结果不是一个关系,因为它可能有重复名称的列、“匿名”(未命名)列、重复行、空值等。SQL不将表视为关系,因为它依赖于列排序等。
SQL中NATURAL JOIN背后的思想是更容易更忠实于关系模型。两个表的NATURAL JOIN的结果将有按名称重复的列,因此没有匿名列。类似地,提供了UNION对应和EXCEPT对应,以解决SQL在遗留UNION语法中对列排序的依赖。
然而,与所有编程技术一样,它需要纪律才能发挥作用。成功的NATURAL JOIN的一个要求是一致地命名列,因为连接隐含在具有相同名称的列上(遗憾的是,SQL中重命名列的语法很冗长,但副作用是鼓励在基本表和视图中命名列时遵守纪律:)
注意,SQL NATURAL JOIN是等价连接**,但这并不妨碍它的有用性。如果NATURAL JOIN是SQL中唯一支持的连接类型,那么它仍然是关系完整的。
While it is indeed true that any NATURAL JOIN may be written using INNER JOIN and projection (SELECT), it is also true that any INNER JOIN may be written using product (CROSS JOIN) and restriction (WHERE); further note that a NATURAL JOIN between tables with no column names in common will give the same result as CROSS JOIN. So if you are only interested in results that are relations (and why ever not?!) then NATURAL JOIN is the only join type you need. Sure, it is true that from a language design perspective shorthands such as INNER JOIN and CROSS JOIN have their value, but also consider that almost any SQL query can be written in 10 syntactically different, but semantically equivalent, ways and this is what makes SQL optimizers so very hard to develop.
下面是一些语义等价的查询示例(使用常用的零件和供应商数据库):
SELECT *
FROM S NATURAL JOIN SP;
-- Must disambiguate and 'project away' duplicate SNO attribute
SELECT S.SNO, SNAME, STATUS, CITY, PNO, QTY
FROM S INNER JOIN SP
USING (SNO);
-- Alternative projection
SELECT S.*, PNO, QTY
FROM S INNER JOIN SP
ON S.SNO = SP.SNO;
-- Same columns, different order == equivalent?!
SELECT SP.*, S.SNAME, S.STATUS, S.CITY
FROM S INNER JOIN SP
ON S.SNO = SP.SNO;
-- 'Old school'
SELECT S.*, PNO, QTY
FROM S, SP
WHERE S.SNO = SP.SNO;
**关系自然连接不是一种均连接,而是一种投影。——philipxy
自然连接只是一种避免输入的快捷方式,假设连接是简单的,并且匹配相同名称的字段。
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)
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)