Do Joins Require a Foreign Key in Child Table?

  • 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.

  • 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.

  • "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

  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • 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.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply