I am using Mysql and by mistake deleted very important details.Is there a way how to recover deleted records in Mysql?
I know this is not really what you are asking but I find it important that you know this.
The system I suggest is called soft delete. It works as follow:
Make a new column in your table (for ex the table people):
ID | Name | Deleted |
---|---|---|
1 | Bob | 0 |
2 | Frank | 1 |
3 | Alice | 0 |
With 0 as undeleted and 1 as deleted in last column with the type BOOLEAN
. Name and ID are respectively a NVARCHAR
and an INT
.
If you want delete a record, you don't delete it with a DELETE
statement but update it like this:
UPDATE people SET Deleted = 1 WHERE ID = 1
Result:
ID | Name | Deleted |
---|---|---|
1 | Bob | 1 |
2 | Frank | 1 |
3 | Alice | 0 |
You know ID's 1 and 2 are deleted because the boolean in column Deleted
is on 1
. Alice with ID 3 is not deleted because the boolean is on 0
.
The pro of this system is that you can recover data with only one statement!
UPDATE people SET Deleted = 0 WHERE Deleted = 1
Result:
ID | Name | Deleted |
---|---|---|
1 | Bob | 0 |
2 | Frank | 0 |
3 | Alice | 0 |
Now all record are recovered because the boolean in column Deleted
is on 0.
If you use DELETE
statement, it is impossible to recover all records! They are deleted for always and you can never recover it if you are using the DELETE
statements. You can only use a backup file for recover it but has also contra's like:
By soft delete you change only one column and you have your data back.
Edit:
The contra of the system is that (like you have said) the data is not exactly remove from your database. It's only a column you change from 0 to 1. But if you know that you can make money from data... is this another story.
If you want to delete it exactly you can use a DELETE
statement.