Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Do Joins Require a Foreign Key in Child Table? Expand / Collapse
Author
Message
Posted Wednesday, June 17, 2009 1:45 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 23, 2012 5:27 PM
Points: 1, 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.
Post #736931
Posted Wednesday, June 17, 2009 2:04 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 2:08 AM
Points: 1,269, Visits: 1,400
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.
Post #736953
Posted Wednesday, June 17, 2009 3:49 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 9:50 AM
Points: 2,285, Visits: 4,219
"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
Post #737045
Posted Thursday, June 18, 2009 7:00 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:20 PM
Points: 14,793, Visits: 27,268
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #737494
Posted Thursday, January 21, 2010 5:54 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:15 AM
Points: 3,068, Visits: 4,639
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.
Post #851179
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse