Suppose I perform A natural join B, where:
A's schema is: (aID), where (aID) is a primary key.
B's schema is: (aID,bID), where (aID, bID) is a composite primary key.
Would performing the natural join work in this case? Or is it necessary for A to have both aID and bID for this to work?
A "natural" join uses the names of columns to match between tables. It uses any matching names, regardless of key definitions.
Hence,
will use
AId
, because that is the only column with the same name.In my opinion,
natural join
is an abomination. For one thing, it ignores explicitly declared foreign key relationships. These are the "natural join" keys, regardless of their names.Second, the join keys are not clear in the
SELECT
statement. This makes debugging the query much more difficult.Third, I cannot think of a SQL construct where adding a column or removing a column from a table takes a working query and changes the number of rows in the result set.
Further, I often have common columns on my tables --
CreatedAt
,CreatedOn
,CreatedBy
. Just the existence of these columns precludes using natural joins.