Difference between cross product (cross join, Cartesian product) and natural join

喜欢而已 提交于 2021-01-27 07:55:48

问题


While writing in SQL, how would I know if I should use cross product (cross join, Cartesian product) or natural join?


回答1:


CROSS JOIN creates all possible pairings of rows from two tables, whether they match or not. You don't use any join condition for a cross product, because the condition would always be true for any pairing.

An example of using CROSS JOIN: you have tables of ShoeColors and ShoeSizes, and you want to know how many possible combinations there are. SELECT COUNT(*) FROM ShoeColors CROSS JOIN ShoeSizes;

NATURAL JOIN is just like an INNER JOIN, but it assumes the condition is equality and applies for all columns names that appear in both tables. I never use NATURAL JOIN, because I can't assume that just because columns have the same name, that they should be related. That would require a very strict column naming convention, and practically no real-world project has such discipline.




回答2:


In simple words,cross join performed a cartesian product among the rows of two different tables..where the columns name may or may not be matched......but in natural join its mandatory that in order to perform join operation columns name of two tables must be matched



来源:https://stackoverflow.com/questions/26317745/difference-between-cross-product-cross-join-cartesian-product-and-natural-joi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!