Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)

I need to delete the data in an SQL Server table (parent) which has a relationship with another table (child).

I have tried the basic delete query. But it is not working.

DELETE FROM table WHERE ...

It resulted with the below error.

The DELETE statement conflicted with the REFERENCE constraint ...

I want to keep the table's schema. I know that I just need to add some words in the query, I've ever done this previously, but I simply couldn't recall it.

1 Answer

0 votes
by (12.7k points)

You can also disable and re-enable the foreign key constraints before and later erasing:

alter table MyOtherTable nocheck constraint all
delete from MyTable
alter table MyOtherTable check constraint all

Looking for SQL Tutorial? Come and join the SQL Certification course to gain more knowledge on SQL.

Browse Categories

...