SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Do Joins Require a Foreign Key in Child Table?


Do Joins Require a Foreign Key in Child Table?

Author
Message
Howard Kochman
Howard Kochman
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 32
In reading about SQL Server and database design, I have seen many examples of joins between tables, and they all involved the Primary Key of the Parent matched to the Foreign Key of the Child. Is this a requirement, or just a best practice? Can a Parent be joined to a Child table on a field that has not been defined as a Foreign Key?

Thanks for any comments, because I haven't been able to find a definitive answer to this.
Tom Brown
Tom  Brown
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3312 Visits: 1491
I wouldn't say its a requirement or a best practise

The answer is 'it depends' - on the data.

A Foreign key is used to enforce referential integrity - so you can't add child rows unless the parent key exists. If this makes sense for your data then go ahead, use a foreign key - but there may be circumstances where it doesn't make sense - and the foreign key will simply prevent you from modelling your data correctly.

But it is nearly always a good idea to index your join columns.
Carl Federl
Carl Federl
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6796 Visits: 4352
"Can a Parent be joined to a Child table on a field that has not been defined as a Foreign Key?"

Yes , you can define any join criteria you wish and with outer joins, restriction criteria needs to included in the join instead of the where clause.
I have seen many examples of joins between tables, and they all involved the Primary Key of the Parent matched to the Foreign Key of the Child. Is this a requirement, or just a best practice?

I would recommend that if you are not joining based on Primary Key to Foreign Key columns, then you need to document why you are not. Some relational DBMS even include a short cut for a Primary to Foreign key join , named imaginatively enough, a "key join".

SQL = Scarcely Qualifies as a Language
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97267 Visits: 33013
You can join anything on anything through a TSQL statement. No requirements at all. You might get some... interesting... results, but there are no strict requirements.

However, if you're talking about setting up RI, then you have to define the parent/child relationship off either a primary key or a unique constraint. No choices there.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
PaulB-TheOneAndOnly
PaulB-TheOneAndOnly
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13961 Visits: 4639
Is this a requirement

No.

or just a best practice?

No.

Can a Parent be joined to a Child table on a field that has not been defined as a Foreign Key?

Yes.

Having said that please take into consideration that other RDBMS tecnologies e.g. Oracle's Star Transformation - call for defining FKs in the disabled/rely state for all navigation candidate columns. By doing that you are not enforcing the FK constraint but telling Oracle to initiate star-transformation when those columns are referenced in a query's predicate. This practice also calls for building bitmap indexes as opposed to traditional indexes ;-)

_____________________________________
Pablo (Paul) Berzukov

Author of Understanding Database Administration available at Amazon and other bookstores.

Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search