Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Wierd Foreign Key Issue

Author  Topic 

chamdev
Starting Member

2 Posts

Posted - 2009-04-10 : 15:37:18
Hi all, I'm having some strange issue with my attempts to add a foreign key to a table. I've tried modifying the table in the GUI and script with the same results.
Let's say I write:

ALTER TABLE THIS_TABLE
ADD CONSTRAINT FK_THIS FOREIGN KEY (THIS_ID)
REFERENCES THAT_TABLE(THAT_ID)

Executing that block of code gives me an error stating that the alter table command conflicts with the constraint I JUST ADDED (FK_THIS in this case) and that the conflict occurred in the table and column I'm attempting to reference (THAT_TABLE.THAT_ID in this case). To me it is like SQL server is trying to tell me that I've added the constraint to the wrong table. I can band-aid it for now by enforcing the relationship in my application, but we all know that is not at all ideal. Help is always appreciated.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-10 : 15:41:12
The error is referring to the data. You have data that does not meet what the foreign key is enforcing. Check the child table for data that does not have an associated row in the parent table. Either fix/delete that data, or use NO CHECK on the constraint.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

chamdev
Starting Member

2 Posts

Posted - 2009-04-10 : 15:56:43
Thnx for explaining the error in a way that made sense. It's Friday, I'm off in 10 minutes and my brain is dwelling outside my head, so I never would have checked that.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-10 : 16:03:33
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -