Sql 2005 database has a foriegn key constraint error

I have a database that keeps logging errors as a foriegn key constraint error. I'm an infastructure guy so sql is pretty much a mystery to me. i've attached the error, the customer says that the database is working but have concerns about the error. Can I get some help from you SQL guru's out there please?
sql-error.txt
LVL 6
jasin00Asked:
Who is Participating?
 
brad2575Connect With a Mentor Commented:
There is an error in the database "SMS_AR_Report"

stating that you are trying to insert a value into the table "dbo.Topics" in the column 'topic_ID'.

This topic_ID is the foreign key (meaning that this ID has another table that it looks at to see if the number being put here exists in that other table, and if not cause this error).  So the insert statement it is inserting an ID in this field that does NOT exist in the other table.  

Unless I missed it, it does not tell me what table has the foreign key in it that needs to have the topic_id inserted into before the error will not work.

You need to look at this Foreign key in "Topics"  "FK_HST_SuspectData_Topics".   and this should give you the details you need.
0
 
DBAduck - Ben MillerConnect With a Mentor Principal ConsultantCommented:
The concept is this:

You declare a Primary Key in the Primary table that is the parent.  Then in the child table that will have Foreign keys (related records to the Parent) you declare a Foreign Key constraint, that basically says, for every key in this child table (in your case Topic_ID) there must exist a Parent record with the same Topic_ID in the Parent Table (Primary table).

So I have this:
ParentTable (Topic_ID) declared as Primary Key
I insert Topic_ID 1000 into that table.

ChildTable (Topic_ID) declared as Foreign Key pointing to the Primary Key (PrimaryTable.Topic_ID)
I insert Topic_ID 1000 into that table, everything is OK.
I insert Topic_ID 1001 into that table, ERROR, there is no 1001 in Primary Key table, only 1000.

What this does is keep there from being Orphans in the Foreign Key table (Children without Parents) so that your data has integrity referentially.  This way I will never have to worry about the child table having rows that do not relate to the parent table.

Hope this makes sense.  So the long and the short of it is, the inserts that are happening on the Foreign Key table and throwing errors are because the Parent table does not contain that Topic_ID, therefore it is a violation.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.