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条回答
The star\"
2楼-- · 2019-01-30 15:34

It's up to you and your requirements (some things get rather hard when records exist that...don't).

I will say that a boolean is a bad choice, though. Make it a nullable timestamp. It's pretty handy to know when something was deleted, especially when you deleted too much and want to undo part of the delete.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-30 15:34

I'd like to note that there are (in most countries) use-cases where you can't delete records for legal reasons. Industry and data dependant of course.

In this case I believe the best practice guidleine is to shadow table the "deleted" data which gains you the benefits of actual deletion outlined by MatthewMartin and by extension I have come to find this pattern frequently preferable to creating "active" bit-flags across my data-tables.

查看更多
不美不萌又怎样
4楼-- · 2019-01-30 15:35

It depends on the function of the database. Is it the source of all truth? If yes, then disable rather than delete, as it is easier to recover from bad operations (ie user error). If the database is feed from some upstream data source, delete then unused data. Any recreation/recovery can be done by the upstream system.

查看更多
疯言疯语
5楼-- · 2019-01-30 15:35

As many have already said, the application needs dictated what you want to do. But to me, marking a row seems like not using the right tool for the right thing. We logically think of a delete as a DELETE, so when if you are not allowed to delete for legal reasons, then you don't delete it in the first place. At the same time, i think about all the internal data structure keeping and indexing. Not to mention all the optimizations that can be done to retrieve data, but adding that check(in the view or in the query) affects the performance exponentially with the complexity of the database and the relations the entities have.

In a nutshell, put the deletion logic in the UI layer to prevent user errors and give delete permissions to users who should be able to delete it. Use regular backups for keeping archives. If your application absolutely requires a strict audit history, implement it in triggers and put the audit in an off-site database to avoid all that traffic, check and crap from the production.

查看更多
Rolldiameter
6楼-- · 2019-01-30 15:35

There are two additional solutions for this which I have commonly used. I agree with other individuals who have posted that it is really up to the requirements of your data.

You could prevent the user from deleting the record if it will cause referential integrity problems by using foreign key constraints (provided your RDBMS supports that). A few times I have provided a message to the end-user that "You cannot delete this <object> until you disassociate <parent object> with it." This can work as long as you don't anticipate there are a tremendously high number of associations with another other table or tables.

Another approach is to move any disassociated records to be associated with a record that isn't deleted. For example, say you have a course for which 10 separate class times are associated with it. If you delete the course, you could allow to the user to decided if all 10 classes are deleted or if they are associated with a new or existing course.

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

It depends. (But you guessed that already, I'm sure.)

In practice, the violation of proper usage here is almost always in the direction of deleting.

The main bad consequence of deleting is how often there are dependent records in other tables whose referential integrity is lost when the parent record goes away.

One red herring used to defend deletion (which you've already dealt with properly by dismissing the issue of storage capacity), is expecting that it will make any noticeable difference in query efficiency.

There are too many cases where user or software issues cause someone to need to hit the big "Undo" button; if you delete, you're out of luck (at least without getting special help and aggravating people you'd rather be nice to.)

The terminology I usually use is "Active" and "Inactive".


A few more points to consider (by Totophil):

  1. Deleting a record in some databases will not automatically free up the disk space.
  2. Purging any sensitive information that you no longer require helps avoiding security risks.
  3. Data protection legislation might require your organisation under certain circumstances to purge any identifiable information about an individual. The legislation differs from country to country, some pointers:

  4. On the other hand you might be required by law to keep certain information.

查看更多
登录 后发表回答