views:

53

answers:

2

Users in our application participate in forums and create various entities. In the schema you mark references from those tables back to the User table as foreign key references. We are unable to remove this user entry from the database, since it has several foreign key constraints which can be removed.

One way of addressing would be to make the User field in other tables as nullable, so that you can mark these values as nullable before removing the user entry. Removing information from other tables is not an option at all as it will lead to inconsistency (e.g. remove a forum post belonging to the user will cause a problem).

The other option was to just mark the User entry as removable and don't make it available as part of the user queries. This theoretically means that no user can ever be removed from the system and might be an issue if someone would want to use a similar loginid as the removed user.

Would like to know, how others tackle this issue?

+11  A: 

We just usually soft-delete the users. In other words, have a column in the users table indicating that they're no longer active and modify all the queries so that they only work on active users.

That has two advantages:

  • it doesn't stuff up foreign key constraints with other tables; and
  • it preserves all the data on the off-chance that you need to recover it at some point.

Unless your user quantity is into the billions or your turnover is massively high, keeping old users around will not stress most databases.

This can be used to enact a similar scheme to what SO has. When users "disappear", the questions and answers authored by them still have author information but it's greyed out.

If you NULLed the user information (or even if you had a single Unknown user to assign the posts to if you didn't want to allow NULLs), you would not have this information.

paxdiablo
+1 always soft delete users. Also, often in auditing purposes, user name is stored, not the user id FK, just in case a dim-witted administrator resurrects an inactive user and reassigns the id to a new staffer.
nonnb
There might be some data protection issues with this, so run it past a lawyer, or someone who at least looks like a lawyer.
Tom Anderson
i usually set deleted column to true whenever user gets deleted and for workable queries select only users with deleted = false
KoolKabin
You could create views on your tables that automatically filter out soft-deleted users. That'll save a deal of time and trouble later when everyone keeps forgetting to add the 'deleted = 0' condition to the where clauses.
Brian Hooper
@Tom - Are you saying we will have issues, if we keep the removed user data in the production system
@Tom - What they don't know won't hurt them... right? ;D
Stann0rz
IANAL, but in the UK, we have a law called the Data Protection Act 1998 which regulates the use of data on people. One rule is "Personal data processed for any purpose or purposes shall not be kept for longer than is necessary for that purpose or those purposes". If you hold a person's data for the purpose of serving them as a user of your site, then it might only be necessary, and so legal, to keep it as long as they are registered - or it might not!. See http://www.ico.gov.uk/for_organisations/data_protection_guide/principles_3_to_5_information_standards/retaining_personal_data.aspx
Tom Anderson
+2  A: 

I would go with the approach you mentioned last i.e using a Soft Delete. Have a "Active" flag and mark it as inactive once the user is deleted.

Regarding wanting to use the same userid, i would suggest that dont make userId your primary key.

In that case you are free to use the same userid - as long as you make the check that there is no other "Active" user - you do not allow the old user to re-activate his Id unless there is no other "Active" user

However, this approach requires that the foreign key for all the other tables should be some IDENTITY kind of column and not your userid itself. Provided that is done (and that MAY require a lot of schema changes if you are not using ID's already), i dont see any other potential issues with this approach

InSane