Retrieving deleted rows from MYSQL Database - mysql

I have accidentally deleted all rows of the database is there a way to retrieve them back. Since I am working of department server I wont be take backup. But I know in oracle I could rollback the DML commands. I tried to use rollback but its not working?
or I have to create whole data base again?

Sorry, but if the transaction with the delete statement has already been committed then I don't think you can recover the lost data unless you have a backup.
To avoid this accident, I'd advise always testing your WHERE clause using a SELECT query first, before running a DELETE statement. Then you will notice if it will delete rows that you didn't intend to delete.

Related

Is there "UNDO" for a mysql update?

I was updating a database. I tried on live databse. it works actually but update more rows than expected. I want to be certain about what happened.
Unfortunately NO; there's ROLLBACK command in MySql which let you undo the latest operation, but once you committed once, it is not possible rollback the committed changes..
However, if an error occurs during statement execution, the statement is rolled back.
Check this for more information.
Meanwhile, you need to do some extra-work, you can delete those rows which are added by mistake.

two mysql inserts both or nothing

I have a form that posts to php. As part of the post, I need to insert data into 2 different tables. Right now I am just doing one insert followed by the second insert.
You can probably already tell what I'm going to ask next :)
I need to be able to ensure both inserts happen. If the second one doesn't happen, I need the first one not to happen or be rolled back.
My understanding is this is possible in mysql if it is using innodb, which is the default for mysql 5.5 and above however I'm on mysql 5.3 which is the latest release for centos. Not sure how I can tell, and if innodb is turned on, how do I write my query? And then of course I might have to assume that anyone else who might use my program might not have the right version of mysql with innodb.
Thanks
When you create your table you can specify wich engine it should run. To be able to use transactions it need to in InnoDB. That part of CREATE TABLE is at the end in table options. If your table isn't already InnoDB you can change it with ALTER TABLE.
It's possible to set auto_commit to false from php and that is what you want to do. Then everything you do will be inside one transaction until you do a commit or rollback.
The other alternativ is to manually send START TRANSACTION, COMMIT or ROLLBACK. There is an example in the php manual.
START TRANSACTION
if(first insert failed)
{
ROLLBACK
}
else if(second insert failed)
{
ROLLBACK
}
else
{
COMMIT
}

Is there any way to rollback after commit in MySQL?

I did a big mistake that I updated a table without 'where' clause in MySQL :'(
It is auto-committed.
Is there any way to rollback from it?
No, there's no query that will "undo" a committed data-modifying query.
If you have a backup of the database, you can restore the backup and use DBA tools (in MySQL's case, it's mysqlbinlog) to "replay" all data-modifying queries from the logs since the backup back to the database, but skip over the problem query.
If you don't have a backup and all logs since the that backup, there's nothing you can do to recover the data.
Look up transaction logs. I'll update with more info but the logs may only stay around for a limited time so time is of the essence!
See: http://dev.mysql.com/doc/refman/5.0/en/point-in-time-recovery.html
If you have enabled mysql binlog and also it is of ROW based format, then you can get the value for each row before & after the update. Using that you can restore the table's state.

mysql: working around implicit transaction commits?

I wrote a tool for our project, for applying sql update files that were committed, to the DB. Whenever run (on deployment), it calculates the list of update files which need to be applied, and applies them iniside a transaction.
Recently I became aware of an issue: mysql would implicitly commit a transaction, whenever DDL statements (like create) are executed. http://dev.mysql.com/doc/refman/5.0/en/implicit-commit.html
This is an issue for me, as sometimes an sql update file contains several statements, which as I understand will result in committing the transaction in the middle of executing the update file. This is a problem, because whenever a subsequent update will fail (which happens from time to time) I want to be able to rollback the transaction, or at least track which update files where applied (completely) and which were not.
Is there a way around the implicit transactions issue? I.e. is there a way to rollback a sequence of DDL statements whenever one of them fail?
Any other suggestions how I can handle the issue?
Thanks
Gidi
No. MySQL does not support transactional DDL. You either need to separate your DDL statements from DML statements, or perhaps try to use migration tool like RuckUsing

Recording MySQL DELETE statements

We have a MySQL->Oracle ETL using Informatica that works great for all statements except DELETE. Unfortunately, the DELETE makes the record go away such that Informatica never sees it again to remove/expire it in Oracle.
How have people gone about recording MySQL DELETE statements?
The tables are InnoDB (ACID-compliant) with unique primary keys on all records (auto_increment). We're using the open-source MySQL on Windows.
We'd prefer not to use a general query log for performance reasons. We'd also prefer to keep the stock MySQL binary and not recompile our own special DELETE statement.
A possible solution is to never delete anything from your database. I avoid deleting from the database because then the information is lost forever. I prefer to mark information as invalid or obsolete by adding an appropriate column to the table.
Another similar solution is to use a trigger to insert the record you want to delete into an audit table and then delete the information. Use the insert with Informatica to do the same thing on the Oracle side.