Solved

Cascade Deletion - Store Procedure

Posted on 1998-09-16
3
184 Views
Last Modified: 2010-03-19
We have tried implementing cascade deletion store procedures for referenced table(Primary key) and its referencing tables (Foreign key). But it was not successful. Since, the records in the referencing tables must be deleted first before those in the referenced table. Could someone provide some advices.
0
Comment
Question by:osca
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
spiridonov earned 50 total points
ID: 1090076
Use delete trigger on parent table:
Create tr_del trigger on tab_parent
as
delete from tab_child
from deleted
where deleted.id=tab_child.parent_id

When you delete a row from table tab_parent (primary key 'id'),
delete trigger will delete all rows from tab_child table with foreign key parent_id matching deleted data.


0
 

Author Comment

by:osca
ID: 1090077
We did try this method, but it didn't work. The error was due to the presence of foreign key in the referencing tables, therefore the 'deleted' 'tmp table' cannot be created in the first place, i.e. no deletion on the referenced table (with primary key) can be performed, until all the records in the referencing tables are removed.
0
 
LVL 7

Expert Comment

by:spiridonov
ID: 1090078
You can't have both. You either use REFERENCES constraint or do all referencial integrity enforcement by using triggers: you have to create INSERT, DELETE and UPDATE triggers instead of REFERENCSES. Or  you can implement it in your application. In this case all delete operations should be done only through your application
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now