11

is there a script that can be used to enable cascaded deletion for existing tables. Thanks.

Rohan Prabhu
  • 7,180
  • 5
  • 37
  • 71
mike
  • 113
  • 1
  • 1
  • 4

1 Answers1

15
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 where the children are stored.
  • PARENT_TABLE_NAME: name of the table where 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
  • 27,943
  • 17
  • 79
  • 125
  • ok Thanks VMAtm looks good but Im not sure what all the plceholders mean – mike May 07 '10 at 13:00
  • say I have 2 tables Document Thumbs each thumb belongs to a document. Can you dive me a concrete example using these tables? – mike May 07 '10 at 13:02
  • 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 – VMAtm May 07 '10 at 13:04
  • I need the names for the ID of document in two your tables – VMAtm May 07 '10 at 13:05