SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Foreign Key Error


Foreign Key Error

Author
Message
sdhamani1020
sdhamani1020
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 36
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.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59234 Visits: 17947
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Ed Wagner
Ed Wagner
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44055 Visits: 10835
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.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search