Foreign Key Error

  • Keep getting this error :

    Error :

    Msg 1776, Level 16, State 0, Line 1

    There are no primary or candidate keys in the referenced table 'tbl_property' that match the referencing column list in the foreign key 'fk_Trace_num'.

    Msg 1750, Level 16, State 0, Line 1

    Could not create constraint. See previous errors.

    I have created two tables , and am trying to create a relationship but somehow keeps getting the above error.

    I have created a composite key in the table and now want to create a foreign key for that in another table.

    Kindly advise.

  • sdhamani1020 (6/11/2013)


    Keep getting this error :

    Error :

    Msg 1776, Level 16, State 0, Line 1

    There are no primary or candidate keys in the referenced table 'tbl_property' that match the referencing column list in the foreign key 'fk_Trace_num'.

    Msg 1750, Level 16, State 0, Line 1

    Could not create constraint. See previous errors.

    I have created two tables , and am trying to create a relationship but somehow keeps getting the above error.

    I have created a composite key in the table and now want to create a foreign key for that in another table.

    Kindly advise.

    Your table tbl_property does not have a primary key. You have to create one first.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Another thing to consider is that the foreign key needs to be the same data type as the primary key.

    When considering what to make your primary key, you should take a multitude of things into consideration. This includes that the values will be unique, small in size, static and that they have room to grow.

    Unique: They uniquely identify the row, so the values must be unique. Example: Don't use a name.

    Small in Size: Also, every nonclustered index will inherit the primary key as a part of the index. The impact on index size and index rows per page dictates that they should be kept small. Example: Don't use a name or nvarchar(255).

    Static: The values shouldn't change much because you don't want things shuffled around in the index because you update a value. Example: Don't use a date.

    Room to Grow: You don't want to have a key that will run out of room because you won't be able to insert any more rows into the table. Example: Don't use an integer on a table where you expect 250M rows a day.

    I am, but no means, the foremost authority on indexing, but this is a decent first-pass list as to how to design your keys. It will give you a decent start in the right direction when creating your nonclustered indexes as well.

Viewing 3 posts - 1 through 2 (of 2 total)

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