How to use on delete cascade in SQL Server 2005

  • Hi All,

    I have to delete specific rows from Parent table, So once I delete those records from the parent table then it should be deleted from the related child tables.

    So,How can I use the delete statement in T-SQL ? can I use the on delete Cascade ? How?

    If any one having a script the please help me....

    Thanks in Advanced...

    Regards,

    KP

  • In the design of the foreign key you can specify a delete rule. Like you said the CASCADE option is what you need. The following is from BOL:

    ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/uirfsql9/html/08812343-e9a6-4a0f-91fd-80d95ab4b71f.htm

    INSERT and UPDATE Specification Category

    Expand to show information for the Delete Rule and the Update Rule for the relationship.

    Delete Rule

    Specify what happens if a user tries to delete a row with data that is involved in a foreign key relationship:

    No Action An error message tells the user that the deletion is not allowed and the DELETE is rolled back.

    Cascade Deletes all rows containing data involved in the foreign key relationship.

    Set Null Sets the value to null if all foreign key columns for the table can accept null values. Applies to SQL Server 2005 only.

    Set Default Sets the value to the default value defined for the column if all foreign key columns for the table have defaults defined for them. Applies to SQL Server 2005 only.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • For use delete cascade use following Create table script...

    CREATE TABLE test_child

    (SUB1 INT,

    [NO] int,

    FOREIGN KEY ([NO]) REFERENCES Test_parent

    ON DELETE CASCADE)

  • Happy to help:

    SELECT readily_available_info

    FROM BooksOnline AS MyPrimaryReferenceBook

    WHERE contents_title = 'cascading referential integrity constraints'

    OR index_title = 'ON DELETE clauses'


    [font="Courier New"]

    (1 row(s) affected)

    Table 'BOL'. Scan count 0, logical reads 3, physical reads 1, read-ahead reads 0.

    Table 'MSDN'. Scan count 0, logical reads 7, physical reads 7, read-ahead reads 0.

    Table 'UserGoogleSearch'. Scan count 0, logical reads 0, physical reads 0.

    SQL Server parse and compile time:

    CPU time = 0 ms, elapsed time = 1 ms.

    SQL Server Execution Times:

    CPU time = 1 ms, elapsed time = 3 ms.

    [/font]

    Sometimes it is faster to look it up for yourself rather than to ask for teh codez.

    http://msdn.microsoft.com/en-us/library/ms186973(SQL.90).aspx

    and a more generally useful resource:

    http://tinyurl.com/cvmuq2 😀

  • Pages you mentioned do not exist anymore, so could you please write it? Thank you.

  • polichlorek (4/20/2011)


    Pages you mentioned do not exist anymore, so could you please write it? Thank you.

    This is a current link -->

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • polichlorek (4/20/2011)


    Pages you mentioned do not exist anymore, so could you please write it? Thank you.

    And just as SQLKiwi said, you might get faster results by trying your search in SQL Books Online. 😀

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply