Solved

Cascade Deletion - Store Procedure

Posted on 1998-09-16
3
195 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Importing Backpac? 1 21
SQL Server Count where two id types exist in column 8 27
SQL Server 2012 r2 - Sum totals 2 23
What's wrong with this T-SQL Foreign Key? 7 41
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

785 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