Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

deleting a record that has a foreign key

Posted on 2013-12-19
4
Medium Priority
?
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

719 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