|
|
|
Forum 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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 5:16 AM
Points: 1,196,
Visits: 1,319
|
|
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.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 11:54 AM
Points: 2,224,
Visits: 4,080
|
|
"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
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 12:48 PM
Points: 13,371,
Visits: 25,150
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 1:08 PM
Points: 2,982,
Visits: 4,396
|
|
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.
|
|
|
|