February 1, 2018 at 2:50 pm
Hi,
I have an unusual problem in that I have to remove records from two tables that they have foreign keys pointing to each other.
Silly I know but that's how it's been designed and I can't drop the constraints in order to delete the records.
If I try deleting records from either table I get a foreign key constraint error message.
If there's any advice out there around removing these records without disabling the constraints I would appreciate it.
If this isn't clearly explained please let me know.
Thanks,
Emaon
February 1, 2018 at 3:16 pm
Try doing both in a single transaction:
begin tran
delete tbl1 where ...
delete tbl2 where...
commit;
February 1, 2018 at 3:18 pm
Joe Torre - Thursday, February 1, 2018 3:16 PMTry doing both in a single transaction:
begin tran
delete tbl1 where ...
delete tbl2 where...
commit;
If this doesn't work and one (or both) of the fields is nullable, update the field to NULL, delete the record from the other table, and then delete the original record.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
February 1, 2018 at 3:40 pm
If you want to delete the rows that are related - you could modify the tables to use ON DELETE CASCADE. However, if you do not want to delete the related rows you are going to have to update the foreign keys to a different value then delete the rows.
That is - on table 1 update the rows with value 'x' to value of 'y' and delete from table2 the rows that have the value 'x'. Then update the values in table2 with value 'a' to value 'b' - then delete from table1 the rows that have value 'a'.
Before doing any of this - I would recommend that you copy the data in both tables so you can put the data back if something doesn't work right.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
February 2, 2018 at 12:49 am
Joe Torre - Thursday, February 1, 2018 3:16 PMTry doing both in a single transaction:
begin tran
delete tbl1 where ...
delete tbl2 where...
commit;
Won't work. Constraints are checked at the statement level. The first statement will violate the constraints and fail. The second will then violate the constraints and fail as well.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 14, 2018 at 10:02 am
Hi everyone,
much belated thanks for all your help on this.
I tried various things but the approach that worked for me was "update the field to NULL, delete the record from the other table, and then delete the original record."
Post reply
Cheers,
Eamon
February 15, 2018 at 8:14 am
Just for curiosity sake, are you able and allowed you tell what the tables are. In all my design years, I can't recall something like this. I would just like to see it so I can add it to my bag of design tips. Thanks,
February 16, 2018 at 4:33 am
Have you tried updating the FK column values to NULL before deleting the rows?
February 16, 2018 at 4:44 am
Have you tried updating the FK column values to NULL before deleting the rows?
He already stated that this was the approach that worked for him.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply