Solved

deleting a record that has a foreign key

Posted on 2013-12-19
4
278 Views
Last Modified: 2013-12-20
I have three tables in sql.  They are connected to each other via foreign keys.  If I want to delete the record associated in all 3 tables how do I do this.  When I simply deleting a record, I got this error:

Msg 547, Level 16, State 0, Line 1
The DELETE statement conflicted with the REFERENCE constraint "FK_tblOrgObjectives_tblOrgGoals". The conflict occurred in database "CAPRegistration", table "dbo.tblOrgObjectives", column 'GoalID'.
The statement has been terminated.
0
Comment
Question by:al4629740
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39729495
You can do this, but you'll have to do it IN ORDER from the child tables, working your way up to the parent table.

Spell out for us the relationship between these three tables.
0
 

Author Comment

by:al4629740
ID: 39729524
Dependence on each table is in this order
tblOrgGoals > tblOrgObjectives > tblPI


Primary > Foreign
tblOrgGoals > tblOrgObjectives : share the column GoalID


tblOrgObjectives > tblOrgPI : share the column ObjectiveID
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 39729762
>If I want to delete the record associated in all 3 tables
Just to get this show started, how 'about.....
Declare @GoalID int = 42

DELETE FROM tblOrgPI
JOIN tblOrgObjectives ON  tblOrgPI.ObjectiveID  =  tblOrgObjectives.ObjectiveID 
WHERE tblOrgObjectives.GoalID = @GoalID

DELETE FROM tblOrgObjectives WHERE GoalID = @GoalID

DELETE FROM tblOrgGoals WHERE GoalID = @GoalID

Open in new window

0
 
LVL 13

Assisted Solution

by:magarity
magarity earned 250 total points
ID: 39729837
What you really need is 'ON DELETE CASCADE' which will automatically track down all the foreign keys for a row being deleted and delete them too:

http://msdn.microsoft.com/en-us/library/ms186973.aspx

Rewrite your constraint scripts to include this clause and rebuild them. It will take a little time now but then in the future your deletes will be easy.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question