Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 498
danmine Avatar asked Dec 07 '08 03:12

danmine


People also ask

Should you ever delete rows from database?

Yes, you should delete data from your database, but it's often not simple to tell what and when. "There's no way to cleanly determine what data is actually in use" - I would disagree. An "IsDeleted" bit field on each table is a pretty clean way to identify a record as no longer relevant.

What does a row represent in a relational database?

In relational databases, a row is a data record within a table. Each row, which represents a complete record of specific item data, holds different data within the same structure. A row is occasionally referred to as a tuple.

What will completely remove a table and its data from a relational database?

We use the SQL DROP Table command to drop a table from the database. It completely removes the table structure and associated indexes, statistics, permissions, triggers and constraints.

What is delete in relational database?

The SQL DELETE command is used to remove the records from the relational database tables permanently. Using DELETE command without the WHERE clause removes table data completely.


2 Answers

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.

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?

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.

like image 56
MatthewMartin Avatar answered Sep 30 '22 23:09

MatthewMartin


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:

    • EU http://ec.europa.eu/justice_home/fsj/privacy/law/index_en.htm
    • UK http://www.ico.gov.uk/what_we_cover/data_protection.aspx
    • Estonia http://www.riigiteataja.ee/ert/act.jsp?id=748829
  4. On the other hand you might be required by law to keep certain information.

like image 28
dkretz Avatar answered Sep 30 '22 21:09

dkretz