oracle 我应该删除还是禁用关系数据库中的一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/347160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:32:46  来源:igfitidea点击:

Should I delete or disable a row in a relational database?

mysqlsqloraclerelational

提问by danmine

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.

例如,如果我想从程序中删除用户。

回答by dkretz

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):

还有几点需要考虑(由 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.

  1. 删除某些数据库中的记录不会自动释放磁盘空间。
  2. 清除不再需要的任何敏感信息有助于避免安全风险。
  3. 数据保护立法可能要求您的组织在某些情况下清除有关个人的任何可识别信息。立法因国家而异,一些提示:

  4. 另一方面,法律可能要求您保留某些信息。

回答by MatthewMartin

Not deleting will create a new class of bugs for all future queries. Don't forget that query writing is often done by power users (i.e. non-IT professionals), and junior developers. So now every table that has invalid data marked only by a BIT active flag will need an additional AND in the WHERE clause for every query from now until forever. This will help users fall into the pit of failure instead of the pit of success. However, I strongly encourage you to implement these flag systems anyhow because without bad design, there is no need for maintenance developers to fix the numerous bugs it will create.

不删除将为所有未来的查询创建一类新的错误。不要忘记,查询编写通常由高级用户(即非 IT 专业人员)和初级开发人员完成。因此,现在每个具有仅由 BIT 活动标志标记的无效数据的表都需要在 WHERE 子句中为从现在到永远的每个查询添加一个 AND。这将帮助用户掉入失败的坑而不是成功的坑。但是,我强烈建议您无论如何都要实现这些标志系统,因为如果没有糟糕的设计,维护开发人员就不需要修复它会产生的大量错误。

How valuable is it to have historical data in the table? If the business if forward looking, having old data in the tables can just be a burden-- it cause problems when creating constraints (all constraints will have to be modified to exclude data you wish wasn't there). Data quality assurance is complicated by having to continually re-identify what is "old crap we are afraid to delete but never want to ever use or update again" and new stuff we care about.

表中的历史数据有多大价值?如果业务是前瞻性的,则表中的旧数据可能只是一种负担——它在创建约束时会导致问题(必须修改所有约束以排除您希望不存在的数据)。数据质量保证很复杂,因为必须不断重新识别什么是“我们害怕删除但再也不想使用或更新的旧垃圾”以及我们关心的新东西。

Is it being deleted because it was a mistake? If the row corresponds to an entity in real life, maybe it is interesting to keep and set a "vaporized", "dead", "left the building" flag. If you accidentally inserted a row that corresponds to no entity in real life, a DELETE is not a bad thing. Are imaginary customers that never existed important to keep in the customer table?

是不是因为错误而被删除?如果该行对应于现实生活中的实体,那么保留并设置“蒸发”、“死亡”、“离开建筑物”标志可能会很有趣。如果您不小心插入了与现实生活中没有实体对应的行,则 DELETE 并不是一件坏事。将从未存在过的假想客户保留在客户表中是否重要?

And finally, personality plays a big role. People can be packrats with data, too. If a DBA keeps all his newspapers from 30 years back and don't like deleting data, maybe he should make sure he's making data design decisions based on the merits and not an irrelevant personal preference.

最后,个性起着重要作用。人们也可以打包数据。如果 DBA 保留 30 年前的所有报纸并且不喜欢删除数据,也许他应该确保根据优点而不是无关的个人偏好做出数据设计决策。

回答by rmeador

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...

在阅读了一本关于时态数据库设计的书后,我开始相信每个具有时态意义的记录至少需要有 4 个时间戳列的哲学。这四个是:创建、删除、开始、结束。创建和删除的时间戳是不言自明的。您的系统不应查看在 now() 之前已删除的记录。开始和结束列确定数据何时适用于您的系统。这是为了保持更改的历史记录。如果你需要更新一条记录,你可以将它的结束时间设置为 now(),复制它,更新副本,并将副本的开始时间设置为 now()。这样,当您需要查看某些事物的历史方式时,您可以让系统弄清楚。您还可以将开始设置为将来的某个时间点,以便在那时自动进行更改,或将结束时间设置为将来的时间,使其在那个时间自动消失。将创建/删除的时间戳设置为未来并没有真正意义......

回答by Bert

If you do use a deleted, visible, isactive, etc column, you can abstract away having to remember to use it by using views.

如果您确实使用了已删除、可见、处于活动状态等列,则可以通过使用视图来抽象掉必须记住使用它的情况。

回答by Dustin

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.

不过,我会说布尔值是一个糟糕的选择。使其成为可为空的时间戳。知道何时删除了某些内容非常方便,尤其是当您删除太多并想撤消部分删除时。

回答by MusiGenesis

Adding a "DELETED" column to your table and marking rows instead of deleting them creates a lot more work for you with little (if any) benefit. Now, every time you write a query you have to remember to include "WHERE DELETED IS NOT NULL" (or whatever).

将“DELETED”列添加到您的表并标记行而不是删除它们会为您带来更多的工作,但收益很少(如果有的话)。现在,每次编写查询时,您都必须记住包含“WHERE DELETED IS NOT NULL”(或其他内容)。

A better approach is to delete data when you need to delete data, and rely on your regular backup process to ensure that no data is ever lost. If for some reason you need to keep some deleted data handy (for searches, maybe), you're better off just copying the data to a different table created for this purpose and then deleting the originals.

更好的方法是在需要删除数据时删除数据,并依靠您的常规备份过程来确保不会丢失任何数据。如果出于某种原因您需要保留一些已删除的数据(可能用于搜索),您最好将数据复制到为此目的创建的不同表中,然后删除原始数据。

I've inherited many databases over the years, and this strategy of flagging records instead of deleting them is unfortunately very common, and (in my experience at least) always leads to major problems down the road.

多年来,我继承了许多数据库,不幸的是,这种标记记录而不是删除它们的策略非常普遍,并且(至少以我的经验)总是会导致重大问题。

回答by dan gibson

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.

您可能还有不删除记录的技术要求。例如,如果您只想通过发送更改的记录来将您的数据库与另一个用户同步,如果它被实际删除,您将无法做到这一点。

回答by Din

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.

在大多数情况下,最好将此类记录存储在单独的表中。然后,您可以避免一个表引用另一个表的各种情况,您需要决定是否将第二个表中的记录也视为已删除。

回答by Piskvor left the building

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

如果您有时需要删除的数据,但不是经常需要:您可以将记录移动到单独的数据库/表中(例如usersand users_deleted,或更好的somedb.usersand 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.

这样,数据仍然可以通过查询访问(虽然它不会像普通的那样简单),但它不会使原始数据库变得混乱,您也不必围绕它进行编码。

回答by Mark Harrison

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

除非您特别需要管理自己的删除,否则最好只删除行。