Sunday 15 July 2007

Oracle: Natural Joins

If you setup your database structure and within two tables you use the same column name, you can then ask oracle to use these columns as the join using the Natural Join facility. For example:

Customer Table
CUSCODE
NAME
ADDRESS
BALANCE

Contact Table
CUSCODE
FULLNAME
JOBTITLE

select cuscode, name, fullname, jobtitle from contact natural join customer

gives this result, automatically using CUSCODE as the Join:

SJC001        Steve’s Bargains Limited                Steve Cholerton        Head of Bargains
SJC001        Steve’s Bargains Limited                Joey Bloggs                Sales Manager

“Well, he’s kind of had it in for me ever since I accidentally ran over his dog. Actually, replace ‘accidentally’ with ‘repeatedly,’ and replace ‘dog’ with ‘son.’ - Lionel Hutz

No comments: