views:

91

answers:

2

Hi is there a script that a can use to enable cascading when deleting a record from an exsiting table Thankd

+1  A: 

Here is a handy script for doing cascade deletes

http://www.sqlteam.com/article/performing-a-cascade-delete-in-sql-server-7

Avitus
Hi Im using sql server 2005, it has this feature enabled but I believe you need to add to a table. It is possible via sql management studio , just wondering if it is possible to do so via a tsql script
mike
+2  A: 
ALTER TABLE [wm].[TABLE_NAME]  WITH NOCHECK ADD  CONSTRAINT [FK_TABLE_NAME_PARENT_TABLE_NAME] FOREIGN KEY([FOREIGN_KEY])
REFERENCES [wm].[PARENT_TABLE_NAME] ([PRIVATE_KEY])
ON DELETE CASCADE
GO

TABLE_NAME - name of the table there the childs are stored. PARENT_TABLE_NAME - name of the table there the parents are stored. This placeholders can be equal

FK_TABLE_NAME_PARENT_TABLE_NAME - just name for the constraint

FOREIGN_KEY - field in the child table for the connection with the parents, for example - ParentID PRIMARY_KEY - field in the parents table, for example - ID

ALTER TABLE [wm].[Thumbs]  WITH NOCHECK ADD  CONSTRAINT [FK_Thumbs_Documents] FOREIGN KEY([DocID])
REFERENCES [wm].[Documents] ([ID])
ON DELETE CASCADE
GO
VMAtm
ok Thanks VMAtm looks good but Im not sure what all the plceholders mean
mike
say I have 2 tablesDocumentThumbseach thumb belongs to a document.Can you dive me a concrete example using these tables?
mike
TABLE_NAME - name of the table there the childs are stored.PARENT_TABLE_NAME - name of the table there the parents are stored.This placeholders can be equalFK_TABLE_NAME_PARENT_TABLE_NAME - just name for the constraintFOREIGN_KEY - field in the child table for the connection with the parents, for example - ParentIDPRIMARY_KEY - field in the parents table, for example - ID
VMAtm
I need the names for the ID of document in two your tables
VMAtm
Makes sense . Thanks a lot I will mark as the answer
mike
Good luck in your projects
VMAtm