May 7, 2008 at 3:22 pm
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
May 8, 2008 at 12:34 am
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.
April 29, 2009 at 5:25 am
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)
April 29, 2009 at 6:17 am
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.
[/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:
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
April 20, 2011 at 4:57 pm
Pages you mentioned do not exist anymore, so could you please write it? Thank you.
April 20, 2011 at 5:39 pm
polichlorek (4/20/2011)
Pages you mentioned do not exist anymore, so could you please write it? Thank you.
______________________________________________________________________
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. SelburgApril 20, 2011 at 5:41 pm
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. SelburgViewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply