SQL Server 'Recycle Bin'
I'm building a Content Management website which will also include other features. When an admin member deletes an item through the admin panel, I would like this item to be moved to a 'recycle bin' for 30 days (then automatically deleted).
What is the best way of implementing this feature?
One idea I had was to 开发者_如何学运维have a 'Deleted' bit column in my table, then only show records WHERE Hide=0
. However this would mean having to remember to put this condition in place everytime I SELECT
from the table.
Another Idea I had was to have a second table where the records would be moved to when deleted. However I have many tables being used in my website, so this would mean doubling the amount of tables, and having duplicate table structures (which could cause consistency issues in the future).
Ideally I would like to have a 'RecycleBin' table which all the records are moved to, but this could contain 100s of columns to be able to store data from all the different tables.
If anyone has any other ideas it would be most appreciated.
Thanks.
Most implementations I know use a deleted
column (not a boolean, but a timestamp, so you can tell when it was deleted). Yes, you will need to add this to all your queries, but maybe you are using an ORM layer, so you only have to add it once? I would suggest going this way.
Wikipedia uses multiple tables for the current and the archived versions of their articles, but that is because they are out-of-league huge, and the current versions are requested much more often than the older versions.
If you want to use one RecycleBin
table, you might want to consider serializing the rows and putting them in a value
column instead of storing all individual columns. Of course, this only works if you don't need to query on the contents of individual columns of deleted items, since that will be very costly.
Using an IsDeleted column is a good technique.
You can combine this with a delete instead of trigger which will use the flag instead of a regular delete operation. You can also wrap the table with views, so that only non-delete rows are exposed in the view.
精彩评论