-1

I need a script that can reverse sql queries like insert/create/add column ...

exemple :

input : INSERT INTO myTable VALUES (1, 1);

output : DELETE FROM myTable WHERE id=1;

SelVazi
  • 10,028
  • 2
  • 13
  • 29
  • @TimBiegeleisen How is this a duplicate? – Barmar Nov 12 '20 at 08:30
  • I've never heard of a tool that can do this. – Barmar Nov 12 '20 at 08:31
  • It will become much trickier when you come to `UPDATE`, and reverse a `DELETE FROM thatTable WHERE id = 13;` Especially if there are WHEN ON DELETE/UPDATE CASCADE fk's. – jarlh Nov 12 '20 at 08:39
  • There exists unreversable INSERTs - at all (INSERT .. ODKU) or on the base of current data state (INSERT IGNORE). The task is in general unsolvable. – Akina Nov 12 '20 at 09:38

1 Answers1

1

If you want to "reverse" operations, then use transactions.

This allows you to have a block of code that modifies the database. Then, if you decide that you don't want to keep all the changes -- say if an error occurs -- then you can ROLLBACK the transaction, undoing all the changes.

Once the data has been committed to the database, SQL is persistent and remembers it. You can do other operations that have the effect of undoing the changes, but that requires appropriate application logic.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786