tags:

views:

195

answers:

4

What is the database concept equivalent to "when deleting the user, delete all his posts"? And is this a good thing?

Another example: if your website is a programming forum, you need to find and delete comments related to that topic before deleting the topic.

Should this be handled automatically in the database layer?

+6  A: 

Cascading updates, usually used in conjunction with foreign key references. Different DBMS offer varying levels of support.

In the specific case of a forum or similar web site, I'd suggest using "soft" deletion - flag the rows in the databases as being deleted, which will prevent them from being viewed or returned in lists or search results, but don't remove them completely. This facilitates undeletion, etc. to counter shoddy or biased moderation.

In addition, I'd suggest that automatically deleting a user's posts when you delete their user account may not be the best behaviour in all cases - certainly, when dealing with troll/spam accounts, you may want to remove junk posts, but you don't necessarily want to blast away all the information in other cases, particularly as it introduces issues with broken references (e.g. external references, cross-linking from other posts, etc.)

Rob
+11  A: 

cascading deletes

I would hesitate to recommend real deletion - instead using a soft deletion which marks a record as deleted - in this case, you might use cascading updates (or not, since the original topic has already been marked as deleted).

Cade Roux
A: 

Not sure if this is what you wanted to find out, but in MySQL the type of thing (I think) you're asking about is called a trigger. It's basically an SQL statement that you associate with a table and an action on that table; for example, you can set a statement that will execute whenever a user's record is deleted which will delete all comments/posts/whatever associated with that user.

see http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html and links therein (that's for MySQL, of course... other DBs may differ)

David Zaslavsky
A: 

The answer to your question is cascading deletes. For the record, I hate user deletion as a forum feature. If people want to leave, great ... I want to see the history of what they did while they were there.

skiphoppy
Privacy and data protection issues may prevent a site retaining that data. I for one would hope that any personal info supplied was at least anonymised if I close an account with a site.
Kev
@Kev, if you are worried, about privacy, perhaps you shouldn't create the account or post to the site in the first place.
Zoredache
+1 (if I had votes left)
Rob