Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

deleting a record that has a foreign key

Posted on 2013-12-19
4
Medium Priority
?
290 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 66

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 66

Accepted Solution

by:
Jim Horn earned 1000 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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

916 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