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?
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.
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.
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.
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. The syntax added to the foreign key is " ON DELETE CASCADE "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With