Avatar of wasabi3689
wasabi3689
Flag for United States of America asked on

FK delete question

I have 3 tables. WORK, SYSTEM and TASKS

The relationship among these 3 tables are

WORK has PK WORK_ID
SYSTEM has WORK_ID but it is FK as name "ST_WRK_FK"
TASKS has WORK_ID but it is FK as name "ET_WRK_FK"

Now I want to remove records in WORK. First I delete all its child tables records associated with it

step 1
delete
from SYSTEM
Where....

step 2
delete
from TASKS
where....

step 3
delete
from WORK
where

Now I turn out the following error


Msg 547, Level 16, State 0, Line 4
The DELETE statement conflicted with the REFERENCE constraint "ET_WRK_FK". The conflict occurred in database "test1", table "dbo.TASKS", column 'WORK_ID'.
The statement has been terminated.


I check the tables TASKS and SYSTEM, all the associated records are removed. I don't understand why I cannot remove records from WORK which is the parent table.

What I went wrong? How to fix this issue?
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Ioannis Paraskevopoulos

8/22/2022 - Mon
Ioannis Paraskevopoulos

Hi,

It seems that it errors when you try to delete from TASKS and not when trying to delete from WORK. And from what the message says it seems that TASKS has a foreign key to WORK.

Giannis
wasabi3689

ASKER
yes, TASKS has a FK for WORKS, but I have removed all the records first for TASKS before I clean up parent WORKS
TempDBA

while deleting from tables, try including schema as well.
i.e.
delete from dbo.tasks
where.....

If this doesn't work out, then please paste the table structure here for all the three tables.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
wasabi3689

ASKER
I resolved the first error, but I come up another error

Foreign Key Base Table            WORK
Foreign Key Columns              PARENT_WORK_ID
Primary/Unique Key Base Table       WORK
Primary/Unique Key Column       WORK_ID


Message
Executed as user: NT AUTHORITY\SYSTEM. The DELETE statement conflicted with the SAME TABLE REFERENCE constraint "WF_WF_FK". The conflict occurred in database "test1", table "dbo.WORK", column 'PARENT_WOR_ID'. [SQLSTATE 23000] (Error 547)  The statement has been terminated. [SQLSTATE 01000] (Error 3621).  NOTE: The step was retried the requested number of times (1) without succeeding.  The step failed.


It seems I missing another delete statement for child before I delete records in work table???
wasabi3689

ASKER
The structure of WF_WF_FK

Foreign Key Base Table            WORK
Foreign Key Columns              PARENT_WORK_ID
Primary/Unique Key Base Table       WORK
Primary/Unique Key Column       WORK_ID
Ioannis Paraskevopoulos

Hi,

Please open object explorer in SSMS and browse to table WORK. Right click on the table and choose 'Design'.

From the menu go to the tab 'Table Designer' and choose 'Relationships'

This will list all the relationships of this object, Foreign and Primary keys. If you have any problem figuring out which is the one that stops you from completing your task you may post  back.

Giannis
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
wasabi3689

ASKER
This is the error message I got

Message
Executed as user: NT AUTHORITY\SYSTEM. The DELETE statement conflicted with the SAME TABLE REFERENCE constraint "WF_WF_FK". The conflict occurred in database "test1", table "dbo.WORK", column 'PARENT_WOR_ID'. [SQLSTATE 23000] (Error 547)  The statement has been terminated. [SQLSTATE 01000] (Error 3621).  NOTE: The step was retried the requested number of times (1) without succeeding.  The step failed.


Here is the object WORK relationship

Please take a look at the attachment
Capture10.JPG
Ioannis Paraskevopoulos

Hey, i got it. WORK table is referencing itself. This means that you have a master detail relationship in the same table.

So lets consider a schema like the following:

ID    ParentID   Value
1      NULL        'Value1'
2      1              'Value2'
3      1              'Value3'
4      NULL        'Value4'
5      4               'Value5'

In this situation if you try to delete the row with id 1 you will get this error if you try to delete it before deleting rows with id 2,3

Giannis
ASKER CERTIFIED SOLUTION
Ioannis Paraskevopoulos

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question