Difficulties in understanding natural join [closed]

烈酒焚心 提交于 2019-12-10 19:43:58

问题


I'm having problems understanding the main point of natural join in database systems. According to the definition, the natural join selects the combination of 2 tables having the same values of columns whose names equal.

My problem is: what if there are two different values of the same named columns? They will be dropped, but what if I need some information from that dropped tuples?

Can someone please explain this to me using examples?


回答1:


The values don't need to be unique, and you shouldn't have "dropped" tupples.

A natural join is for lazy, trusting and bad programmers:

  • lazy: can't be bothered typing in the join conditions
  • trusting: hopes that when same-named columns are added to both tables that their queries don't suddenly break
  • bad: the essence of good programming is clarity. Using a natural join obscures how the joins are being made, so the reader of your query must check the table definitions to figure out what's going on

IMHO using natural joins is a terrible idea and they should never be used. You gain virtually nothing (you save typing maybe a dozen chars) and lose a lot.

EDIT:

natural joins are like any other join in terms of inner/outer: the default is inner join, but you can soecify left or right joins too, for example in mysql:

select ...
from t1
natural left join t2


来源:https://stackoverflow.com/questions/14546617/difficulties-in-understanding-natural-join

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