Rollback an executed query - mysql

Suppose I have a field(age) with integer values like:
age(23,34,12,23,14,55)
and I need to update all values to 18 so I created an update query like update person set age=18 where condition, If I want to revert the query i.e what will be rollback query I mean how can I get old values of the field.

You cannot, if you committed the query. Only inside transactions you can roll back the changes that haven't been committed yet.
You have to keep track of previous values if you want to have some kind of a rollback mechanism.

Mysql Workbench don't support rollback option.
You can use Toad for Mysql since it supports rollback options.
you can download the link from here:
http://toad-for-mysql.en.softonic.com/

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.

Reserving mySQL auto-incremented IDs?

We want to obtain an auto-increment ID from mySQL without actually storing it until the other non-mysql related processes are successfully completed, so that the entry is not stored if an exception or application crash happens. We need to use the ID as a key for the other processes. In essence we want to “reserve” the auto-increment and insert the rows into mySQL as the last step. We don’t want to insert any row until we know the entire process has completed successfully.
Is it possible to do this sort of auto-increment reservation in mySQL?
Note: I know about the SQL transactions. But our process contains non-SQL stuff that need to happen outside of the DB. These process may take few mins to several hours. But we don't want any other process using the same auto-increment ID. That is why we want a "reserve" an auto-increment ID without really inserting any data into the DB. –
The only way to generate an auto-increment value is to attempt the insert. But you can roll back that transaction, and still read the id generated. In MySQL 5.1 and later, the default behavior is that auto-increment values aren't "returned" to the stack when you roll back.
START TRANSACTION;
INSERT INTO mytable () VALUES ();
ROLLBACK;
SELECT LAST_INSERT_ID() INTO #my_ai_value;
Now you can be sure that no other transaction will try to use that value, so you can use it in your external processes, and then finally insert a value manually that uses that id value (when you insert a specific id value, MySQL does not generate a new value).
Have you considred using mysql tranactions?
The essense of it, you start a transaction, if all sql statements are correct and can be complteted, then you commit your transaction. If not, then you rollback as if nothing happened.
More details can be read in this link:
http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-transactions.html
you can use temporary table along with transaction
if transaction complete temp table will be gone and move data to real table
http://www.tutorialspoint.com/mysql/mysql-temporary-tables.htm

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
}

MySQL which is the method to rollback a transaction effective?

I saw in multiple answers here and on google that rollback a transaction implies only the rollback of the last command, and i read also that implies ALL commands. (neither both documented or referenced by)
That I need to do is create a store procedure that insert/update on table A, get the last ID of A, insert that ID into B, get the last id of B, insert it into C, etc, etc, etc.
I want to know which is the method to commit or rollback all commands in the transaction, in order to start the transaction and if something fails, get back everything as the original.
SQL code with IF error and last_id will be preciated, because also I saw a LOT of differents ways to get the last id and I don't know which is better.
By the way, all tables are InnoDB
Kind regards,
If you BEGIN a transaction then nothing will get applied until you COMMIT it. Dropping your connection or issuing a ROLLBACK is the same as never committing it.
This is, of course, presuming you have autocommit set on, which is usually the case.
You can roll-back individual commands if you wrap them as transactions as well.
More information is available in the documentation.
Keep in mind that MyISAM and other engines do not support transactions, where InnoDB does. Further, only INSERT, UPDATE, DELETE and REPLACE statements are able to be rolled back. Other things, like alterations to the schema, are not.
As documented under START TRANSACTION, COMMIT, and ROLLBACK Syntax:
These statements provide control over use of transactions:
[ deletia ]
ROLLBACK rolls back the current transaction, canceling its changes.

How can I undo a mysql statement that I just executed?

How can I undo the most recently executed mysql query?
If you define table type as InnoDB, you can use transactions. You will need set AUTOCOMMIT=0, and after you can issue COMMIT or ROLLBACK at the end of query or session to submit or cancel a transaction.
ROLLBACK -- will undo the changes that you have made
You can only do so during a transaction.
BEGIN;
INSERT INTO xxx ...;
DELETE FROM ...;
Then you can either:
COMMIT; -- will confirm your changes
Or
ROLLBACK -- will undo your previous changes
Basically: If you're doing a transaction just do a rollback. Otherwise, you can't "undo" a MySQL query.
For some instrutions, like ALTER TABLE, this is not possible with MySQL, even with transactions (1 and 2).
You can stop a query which is being processed by this
Find the Id of the query process by => show processlist;
Then => kill id;
in case you do not only need to undo your last query (although your question actually only points on that, I know) and therefore if a transaction might not help you out, you need to implement a workaround for this:
copy the original data before commiting your query and write it back on demand based on the unique id that must be the same in both tables; your rollback-table (with the copies of the unchanged data) and your actual table (containing the data that should be "undone" than).
for databases having many tables, one single "rollback-table" containing structured dumps/copies of the original data would be better to use then one for each actual table. it would contain the name of the actual table, the unique id of the row, and in a third field the content in any desired format that represents the data structure and values clearly (e.g. XML). based on the first two fields this third one would be parsed and written back to the actual table. a fourth field with a timestamp would help cleaning up this rollback-table.
since there is no real undo in SQL-dialects despite "rollback" in a transaction (please correct me if I'm wrong - maybe there now is one), this is the only way, I guess, and you have to write the code for it on your own.