Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top