Hi...
I want to know on cascade delete with an example in sql server ?
Please explain anyone ?
Thanks.....
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Priya LingePosted Dec 27, 2011, 11:39 PM
Cascading deletes in SQL Server
1.SQL Server does so via foreign key constraints with the DELETE CASCADE flag set.
2.In the following example, after creating the objects and inserting some data, we delete a USR_ID from the parent data. After
querying the child table (USER_PHONE) a second time, we can see that the cascading delete worked :
3.Example :
CREATE TABLE USERS
(
USR_ID int
,CONSTRAINT [PK_Temp_Users1] PRIMARY KEY CLUSTERED ([USR_ID])
)
CREATE TABLE USER_PHONE
(
USR_ID int
,CONSTRAINT [PK_Temp_Users2] PRIMARY KEY CLUSTERED ([USR_ID])
)
ALTER TABLE [dbo].USER_PHONE WITH CHECK ADD
CONSTRAINT [FK_Temp_UsersPhone_Users] FOREIGN KEY([USR_ID])
REFERENCES [dbo].[Users] ([USR_ID])
ON DELETE CASCADE
GO
INSERT INTO USERS
SELECT 1 UNION SELECT 2 UNION SELECT 3
INSERT INTO USER_PHONE
SELECT 1 UNION SELECT 2 UNION SELECT 3
SELECT * FROM USER_PHONE
DELETE USERS WHERE USR_ID=2
SELECT * FROM USER_PHONE
DROP TABLE USER_PHONE
DROP TABLE USERS
Please check below link for more information.
http://www.java2s.com/Code/SQLServer/Constraints/DELETECASCADEandUPDATECASCADE.htm
Hope this will help you.
Thanks.
Vineet Kumar SainiPosted Dec 28, 2011, 5:53 PM
Jignesh TrivediPosted Dec 27, 2011, 11:48 PM
Please try with create table script.
CREATE TABLE test_child
(SUB1 INT,
[NO] int,
FOREIGN KEY ([NO]) REFERENCES Test_parent
ON DELETE CASCADE)
Please refer the below link
http://msdn.microsoft.com/en-us/library/ms186973(SQL.90).aspx
Satyapriya NayakPosted Dec 27, 2011, 11:00 PM
Please refer the below link
http://rudesyle.wordpress.com/2008/01/28/cascading-deletes-in-sql-server/
http://www.java2s.com/Code/SQLServer/Constraints/DELETECASCADEandUPDATECASCADE.htm
Thanks