views:

35

answers:

3

I am making a website where users can post 'Posts' and then users can 'Comment' on these posts. I have a database with 3 tables. One contains User Information, one contains Post Information and the final one contains Comment Information.

I want to set up rules so that if a User is deleted, all of their posts and comments are deleted, and if the user deletes one of their posts then all of the associated comments are deleted. This however sets up 'Multiple CASCADE paths'.

I have been looking for a solution and found some information on triggers. Would they be the best thing to use? If I used them, would I have to change all of the CASCADES to be done by triggers?

Thanks

Clivest

A: 

I think this can be setup fairly easily, without the error you encountered, and without the use of triggers, as follows:

1) The foreign key between Users and Posts should be set up to be cascade delete
2) The foreign key between Posts and Comments should be set up to be cascade delete
Randy Minder
Thanks for the reply. I have considered this, but what if a user, other than the one who created the post, comments. If their account is deleted then your solution wouldn't delete their comment
Clivest
+1  A: 

use declarative referential integrity.

create table foo
(
  id   int         not null primary key ,
  foo  varchar(32) not null ,
)
create table bar
(
  id     int not null primary key ,
  foo_id int     null foreign key references foo ( id ) on delete cascade ,
)

deleting a row from foo will delete all related rows in bar.

Be careful with cascading deletes, though -- fat-fingering a delete statement can cause a whole lot of damage very quickly, not unlike rm(1) in *nix. Cascading deletes can also chew up your transaction log pretty quickly, if you delete a lot of data in one fell swoop.

Nicholas Carey
Thanks for the reply. I am a SQL beginner. Is this possible to set up in the 'Relationships...' option on SQL Server Management Studios?
Clivest
When you define foreign key constraints in the SQL Server Management Studio, you can select the referential action(s) to be taken when the related primary key value is deleted or updated (but you should learn to write the DDL (CREATE/ALTER statements) instead. It makes getting your schema under source control [b]much[/b] simpler.
Nicholas Carey
A: 

Another option would be to create two dedicated stored procedures that would do these delete steps as needed

CREATE PROCEDURE dbo.DeleteUser @UserID VARCHAR(50)
AS BEGIN
   DELETE FROM dbo.Comments
   WHERE Author = @UserID

   DELETE FROM dbo.Posts
   WHERE Author = @UserID

   DELETE FROM dbo.User
   WHERE UserID = @UserID
END

and for the second rule:

CREATE PROCEDURE dbo.DeletePost @PostID INT
AS BEGIN
   DELETE FROM dbo.Comments
   WHERE PostID = @PostID

   DELETE FROM dbo.Posts
   WHERE ID = @PostID
END

In this case, you have total control over what really happens, there's no unexpected surprises from cascading deletes, and no need to use triggers either.

marc_s
This was what I was looking for. Thanks.
Clivest