SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to use on delete cascade in SQL Server 2005


How to use on delete cascade in SQL Server 2005

Author
Message
K-SQL-SSC
K-SQL-SSC
SSC-Enthusiastic
SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)

Group: General Forum Members
Points: 105 Visits: 63
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
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4398 Visits: 3670
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’! **
Pravin Patel-491467
Pravin Patel-491467
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 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)
Paul White
Paul White
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15706 Visits: 11355
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 :-D



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
polichlorek
polichlorek
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 3
Pages you mentioned do not exist anymore, so could you please write it? Thank you.
Jason Selburg
Jason Selburg
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3663 Visits: 4110
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
Jason Selburg
Jason Selburg
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3663 Visits: 4110
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. :-D

______________________________________________________________________

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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search