Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I edit a table in order to enable CASCADE DELETE?

I have a table representing users. When a user is deleted I get:

DELETE statement conflicted with the REFERENCE constraint

Apparently, CASCADE DELETE is not as easy as I imagined in SQL Server, and the option needs to be added to the table.

The problem is: I cannot figure out how to add the CASCADE DELETE option.

I'm using: SQL Server 2008. Any ideas how to do this?

like image 817
RadiantHex Avatar asked Dec 15 '10 21:12

RadiantHex


People also ask

What is on delete cascade and on delete restrict?

1) ON DELETE CASCADE means if the parent record is deleted, then any referencing child records are also deleted. ON UPDATE defaults to RESTRICT, which means the UPDATE on the parent record will fail. 2) ON DELETE action defaults to RESTRICT, which means the DELETE on the parent record will fail.

How do you drop a table in SQL Server with Cascade constraints?

In SQL Server Management Studio, go to Options / SQL Server Object Explorer / Scripting, and enable 'Generate script for dependent objects'. Then right click the table, script > drop to > new query window and it will generate it for you. Also works for dropping all objects in a db.

What is on delete cascade and on delete Set default?

It means that the child data is set to NULL when the parent data is deleted or updated. SET DEFAULT. It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is set to their default values when the parent data is deleted or updated.


1 Answers

Read this Microsoft article first. Read Me. I use the GUI during design so here is a picture of how it is selected in SSMS. alt text The syntax added to the foreign key is " ON DELETE CASCADE "

like image 117
RC_Cleland Avatar answered Sep 20 '22 22:09

RC_Cleland