Share on Google+Share on Google+

MySQL BEGIN Transaction

Advertisement
This example illustrates how to use the BEGIN keyword in a Transaction.

MySQL BEGIN Transaction

This example illustrates how to use the BEGIN keyword in a Transaction.

In this example we create a table 'trans' with column 'id', 'item' and 'quantity'. Here in the query below we insert one row in the table. Now we execute a BEGIN command and then UPDATE the existing row. After updating the row we can ROLLBACK the data of the table as below in the table.

Query

 CREATE TABLE trans
 (
 id int not null auto_increment,
 item varchar(30) not null,
 quantity varchar(10) not null,
 primary key(id)
 )type=innodb;

Query

DESC trans;

Output

+----------+---------------+------+-----+---------+----------------+
| Field    | Type          | Null | Key | Default | Extra          |
+----------+---------------+------+-----+---------+----------------+
| id       | int(11)       | NO   | PRI |         | auto_increment |
| item     | varbinary(30) | NO   |     |         |                |
| quantity | varbinary(10) | NO   |     |         |                |
+----------+---------------+------+-----+---------+----------------+

Query

INSERT INTO trans (id,item,quantity) VALUES (NULL,'Computer','5');		

Output

+----+----------+----------+
| id | item     | quantity |
+----+----------+----------+
| 1  | Computer | 5        |
+----+----------+----------+

Query

BEGIN;

Query

UPDATE trans SET quantity ='4' WHERE id=1;

Output

+----+----------+----------+
| id | item     | quantity |
+----+----------+----------+
| 1  | Computer | 4        |
+----+----------+----------+

Query

ROLLBACK;

Output

+----+----------+----------+
| id | item     | quantity |
+----+----------+----------+
| 1  | Computer | 5        |
+----+----------+----------+

Query

COMMIT;

Advertisements

Advertisement

Posted on: December 9, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.