Foreign key

  • Hardy21 (5/17/2012)


    Good One.

    If Table2 declared as following than all 3 tables created without any issue.

    create table Table2

    ( id_tb2 int primary key references table1(id_tb1),

    value2 varchar(100))

    Yes, sure

    That is the point of the question.

    Regards

    IgorMi

    Igor Micev,My blog: www.igormicev.com

  • Great question. I had not worked with REFERENCES before, but learned something new today.

    Have a great weekend everyone!!

    "El" Jerry.

    "El" Jerry.

    "A watt of Ottawa" - Gerardo Galvan

    To better understand your help request, please follow these best practices.[/url]

  • Nice question and good discussion. Learned something new today!

  • bitbucket-25253 (5/17/2012)


    Well my answer was declared to be incorrect, so went to read the explanation, following the link given ..... nada / nothing / not a word to support what is deemed the correct answer.

    The link explains that the target of the foreign key reference must have a PRIMARY or UNIQUE constraint. What do you think is missing or incorrect?

    So went and entered the code in SQL 2008R2, triple checked what I had entered and it matched what was given in the question. When pressing the F5 key for SSMS (2008R2) .. and again using SSMS checked after the error message and what do you know ... what was shown did NOT match what was give as the correct answer.

    Can you share the code you ran and the output you saw? I see the output I expected on 2005, 2008, 2008 R2, and 2012; two tables are created, and an error message of the type shown is returned for the third.

  • mtassin (5/18/2012)


    When you use Primary Key with no other qualifiers in a create table statement several things happen.

    1. The column is immediately flagged as NOT NULL

    2. A Clustered Index is created on the column

    3. The column is the Primary key of the table.

    Been that way since SQL 7, when I started with SQL.

    Just a comment regarding point 2:

    CREATE TABLE dbo.Example (id integer PRIMARY KEY, ak integer NULL UNIQUE CLUSTERED);

    Also, I prefer not to rely on defaults - it costs very little to be explicit. My choice is to always specify NULL/NOT NULL and CLUSTERED/NONCLUSTERED.

  • Nice question, thanks.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Nice question - confused me at first until I realised no key was being created in table 2.

    This type of question really shows that's it worth reading something carefully!

    :exclamation: "Be brave. Take risks. Nothing can substitute experience." :exclamation:

  • Interesting concept, but the answer(s) options should have been more specific as to what actually tables are expected to be created. Otherwise, great explanation.

    Thanks.

  • Nice question.

Viewing 9 posts - 31 through 38 (of 38 total)

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