Should I delete or disable a row in a relational d

2019-01-30 15:23发布

In a brand new program where space isn't really that big a deal, is it better to delete a row or to disable a row by let's say a boolean "Disabled" and have the program just ignore it?

For example, if I wanted to remove a user from a program.

18条回答
叼着烟拽天下
2楼-- · 2019-01-30 15:53

Unless you have a specific need for managing your own deletions, you are better off just deleting the rows.

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-30 15:53

It's a judgment call, but I have ended up adding "disabled" columns on tables where I previously thought I could just delete row. I'd say most of the time you're safer adding a disabled column. This can get tricky with n:n relations however, so that's something to consider.

查看更多
相关推荐>>
4楼-- · 2019-01-30 15:54

It depends. If it is disabled then it is easier to undelete / to see that someone actually deleted the record (for auditing).

You may also have a technical requirement to not delete records. For example, if you wanted to synchronize your database with another user by just sending changed records you wouldn't be able to do that if it was actually deleted.

查看更多
祖国的老花朵
5楼-- · 2019-01-30 15:55

If you will need the deleted data sometimes, but not very often: you can move the records into a separate database/table (e.g. users and users_deleted, or better somedb.users and somedb_deleted.users).

This way, the data is still accessible through a query (although it won't be as simple as the normal one), yet it doesn't clutter the original database and you don't have to code around it.

查看更多
时光不老,我们不散
6楼-- · 2019-01-30 15:59

After reading a book on temporal database design, I came to believe in the philosophy that every record of temporal significance needs to have at least 4 timestamp columns. Those four are: created, deleted, start, end. The created and deleted timestamps are fairly self-explanatory. Your system shouldn't look at records where deleted is before now(). The start and end columns determine when the data applies to your system. It's for keeping a history of changes. If you need to update a record, you'd set it's end time to now(), copy it, update the copy, and set the copy's start time to now(). That way, when you need to look at the way something was historically, you can have the system figure it out. You could also set the start to some point in the future to have a change take place automatically at that time, or set the end to a future time to have it automatically go away at that time. Setting the created/deleted timestamps to the future doesn't really make sense...

查看更多
7楼-- · 2019-01-30 15:59

You need to have it in functional requirements. If it is not said there explicitly you will have to figure out it yourself.

In most cases it is better to store such records in separate table. You then avoid various situations where one table refers another table and you need to decide should records in second table be treated as deleted as well or not.

查看更多
登录 后发表回答