|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Wednesday, February 08, 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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 7:30 AM
Points: 1,114,
Visits: 1,140
|
|
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’! **
|
|
|
|
|
SSC 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)
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 10:33 AM
Points: 10,989,
Visits: 10,529
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Sunday, May 01, 2011 2:47 AM
Points: 1,
Visits: 3
|
|
| Pages you mentioned do not exist anymore, so could you please write it? Thank you.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 7:55 AM
Points: 2,582,
Visits: 3,552
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 7:55 AM
Points: 2,582,
Visits: 3,552
|
|
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
|
|
|
|