Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to use on delete cascade in SQL Server 2005 Expand / Collapse
Author
Message
Posted Wednesday, May 7, 2008 3:22 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, February 8, 2012 3:19 AM
Points: 87, Visits: 61
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
Post #496734
Posted Thursday, May 8, 2008 12:34 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 7:34 AM
Points: 2,241, Visits: 2,696
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’! **
Post #496883
Posted Wednesday, April 29, 2009 5:25 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, May 17, 2012 2:51 AM
Points: 34, Visits: 111
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)
Post #706669
Posted Wednesday, April 29, 2009 6:17 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:50 AM
Points: 11,192, Visits: 11,098
Happy to help:

SELECT readily_available_info
FROM BooksOnline AS MyPrimaryReferenceBook
WHERE contents_title = 'cascading referential integrity constraints'
OR index_title = 'ON DELETE clauses'



(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.


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




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #706711
Posted Wednesday, April 20, 2011 4:57 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, May 1, 2011 2:47 AM
Points: 1, Visits: 3
Pages you mentioned do not exist anymore, so could you please write it? Thank you.
Post #1096684
Posted Wednesday, April 20, 2011 5:39 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 7:04 AM
Points: 2,705, Visits: 3,774
polichlorek (4/20/2011)
Pages you mentioned do not exist anymore, so could you please write it? Thank you.


This is a current link --> [url=http://msdn.microsoft.com/en-us/library/ms186973.aspx][/url]


______________________________________________________________________

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

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

Jason L. Selburg
Post #1096689
Posted Wednesday, April 20, 2011 5:41 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 7:04 AM
Points: 2,705, Visits: 3,774
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 you'll likely increase the number and quality of responses you get to your question.

Jason L. Selburg
Post #1096690
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse