📜  SQL中自然连接和交叉连接的区别

📅  最后修改于: 2021-09-14 02:04:29             🧑  作者: Mango

1. 自然连接:
自然连接基于相同的属性名称和数据类型连接两个表。结果表将包含两个表的所有属性,但每个公共列只有一个副本。

例子:
考虑下面给出的两个表:

学生桌

标记表

考虑给定的查询

SELECT * 
FROM Student S NATURAL JOIN Marks M;

输出 :

2.交叉连接:
如果没有指定条件,Cross Join 将产生两个表的交叉或笛卡尔积。结果表将包含两个表的所有属性,还包括重复或公共列。

例子:
考虑以上两个表,查询如下:

SELECT * 
FROM Student S CROSS JOIN Marks M; 

输出:

SQL中Natural JOIN和CROSS JOIN的区别

SR.NO. NATURAL JOIN CROSS JOIN
1. Natural Join joins two tables based on same attribute name and datatypes. Cross Join will produce cross or cartesian product of two tables .
2. In Natural Join, The resulting table will contain all the attributes of both the tables but keep only one copy of each common column In Cross Join, The resulting table will contain all the attribute of both the tables including duplicate columns also
3. In Natural Join, If there is no condition specifies then it returns the rows based on the common column In Cross Join, If there is no condition specifies then it returns all possible pairing of rows from both the tables whether they are matched or unmatched
4. SYNTAX:
SELECT * FROM table1 NATURAL JOIN table2;
SYNTAX:
SELECT * FROM table1 CROSS JOIN table2;