Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete from a database?

I know of two ways to delete data from a database table

  • DELETE it forever
  • Use a flag like isActive/isDeleted

Now the problem with isActive is that I have to track everywhere in my SQL queries that whether the record is active or not. Using DELETE however gets rid of the data forever.

What would be the best way to backup this data?

Assuming I have multiple tables in a database, should I have a common function which just backs everything up and stores it in another table (in XML probably?) or is there any other way.

I am using MySQL but am curious about techniques used in other DBs as well.

like image 468
Abhinav Avatar asked Nov 23 '09 17:11

Abhinav


People also ask

Can we delete records from database?

The Delete command in SQL is a part of the Data Manipulation Language, a sub-language of SQL that allows modification of data in databases. This command is used to delete existing records from a table. Using this, you can either delete specific records based on a condition or all the records from a table.

What is delete command in SQL?

The DELETE command is used to delete existing records in a table.


2 Answers

Replace the table with a view that hides the inactive items.

Or write a trigger on DELETE that backs up the row to an archive table.

like image 113
Joe Koberg Avatar answered Sep 21 '22 03:09

Joe Koberg


You could use a trigger that fires on deleting records to back them up into some kind of graveyard table.

like image 29
Joey Avatar answered Sep 23 '22 03:09

Joey