Solved

deleting a record that has a foreign key

Posted on 2013-12-19
4
276 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

840 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