I have Problem with my delete Tsql when it come to delete the associate record can someone help me how to fix it
ALTER PROCEDURE [dbo].[Delete_Resource]
@ResID INT,
@UserID uniqueidentifier
AS
BEGIN
DELETE FROM [ideaPark_DB].[dbo].[Topics_Resources]
WHERE [ideaPark_DB].[dbo].[Topics_Resources].ResourceID = @ResID
DELETE FROM [ideaPark_DB].[dbo].[Likes]
WHERE [ideaPark_DB].[dbo].[Likes].resourceID = @ResID
declare @BookmarkID INT;
SET @BookmarkID = (SELECT [ideaPark_DB].[dbo].[Bookmarks].id FROM [ideaPark_DB].[dbo].[Bookmarks] WHERE [ideaPark_DB].[dbo].[Bookmarks].resourceID = @ResID and Bookmarks.userID = @UserID)
DELETE FROM [ideaPark_DB].[dbo].[Bookmarks]
WHERE [ideaPark_DB].[dbo].[Bookmarks].resourceID = @ResID
DELETE FROM [ideaPark_DB].[dbo].Bookmarks_Groups
WHERE Bookmarks_Groups.bookmarkID = @BookmarkID
delete FROM [ideaPark_DB].[dbo].[Resources_Relationship]
WHERE [ideaPark_DB].[dbo].[Resources_Relationship].fk_parent = @ResID
DELETE FROM [ideaPark_DB].[dbo].[Skills_Resources]
WHERE [ideaPark_DB].[dbo].[Skills_Resources].ResourceID = @ResID
DELETE FROM [ideaPark_DB].[dbo].[Resources]
WHERE [ideaPark_DB].[dbo].[Resources].Id = @ResID
END
the error
A critical error has occurred. The DELETE statement conflicted with the REFERENCE constraint "FK_Resources_Relationship_Resources1". The conflict occurred in database "ideaPark_DB", table "dbo.Resources_Relationship", column 'fk_child'. The statement has been terminated. -
Add the following before your final delete from Resources
delete FROM [ideaPark_DB].[dbo].[Resources_Relationship]
WHERE [ideaPark_DB].[dbo].[Resources_Relationship].fk_chid = @ResID
Your Resources_Relationship
table has two FK's back to the Resources
table, you were only handling one of those.
I see two issues:
The first:
DELETE FROM [ideaPark_DB].[dbo].[Bookmarks]
WHERE [ideaPark_DB].[dbo].[Bookmarks].resourceID = @ResID
DELETE FROM [ideaPark_DB].[dbo].Bookmarks_Groups
WHERE Bookmarks_Groups.bookmarkID = @BookmarkID
I'd change the order of these delete because bookmark_groups is middle table between bookmarks and groups.
The second:
After this delete:
DELETE FROM [ideaPark_DB].[dbo].[Resources_Relationship]
WHERE [ideaPark_DB].[dbo].[Resources_Relationship].fk_parent = @ResID
You must add this:
DELETE FROM [ideaPark_DB].[dbo].[Resources_Relationship]
WHERE [ideaPark_DB].[dbo].[Resources_Relationship].fk_child = @ResID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.