Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cascade Delete turned on?

Tags:

sql

sql-server

Is there a way to check if the table(s) have Cascade Delete turned on? I'm looking at the script of the table (from SQL Server) and I don't see any indication of Cascade Delete.

like image 648
webdad3 Avatar asked Jun 07 '13 14:06

webdad3


People also ask

How do I enable delete cascade?

Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.

Should I use on delete cascade?

ON DELETE CASCADE is fine, but only when the dependent rows are really a logical extension of the row being deleted. For example, it's OK for DELETE ORDERS to delete the associated ORDER_LINES because clearly you want to delete this order, which consists of a header and some lines.

What does cascade on delete mean?

CASCADE. It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is either deleted or updated when the parent data is deleted or updated.

What is disable on delete cascade in Oracle?

Anyway, you can disable the constraint by using: alter table TABLE_NAME disable constraint FK_CONSTRAINT_NAME; ( use 'enable' to re-enable it ).


3 Answers

You can use INFORMATION_SCHEMA for standard approach, ex.

select * from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
where DELETE_RULE ='CASCADE'
like image 134
msi77 Avatar answered Oct 05 '22 14:10

msi77


Please use sys.foreign_keys for foreign key relations.

The column - delete_referential_action helps you know if there is a delete on cascade.

http://technet.microsoft.com/en-us/library/ms189807.aspx

Below View help with similar works:

sys.default_constraints for default constraints on columns
sys.check_constraints for check constraints on columns
sys.key_constraints for key constraints (e.g. primary keys)
sys.foreign_keys for foreign key relations

Source: SQL Server 2008- Get table constraints

like image 39
RGV Avatar answered Oct 05 '22 12:10

RGV


I found how to do this:

I scripted the FK on the table to a new query window:

ALTER TABLE [dbo].[myTable]  WITH CHECK ADD  CONSTRAINT [FK_myTable_myTableHeaders] FOREIGN KEY([ID])
REFERENCES [dbo].[myTableHeaders] ([_ID])
ON DELETE CASCADE
GO

This how I was able to confirm it.

like image 1
webdad3 Avatar answered Oct 05 '22 13:10

webdad3