This example illustrates how to use the BEGIN keyword in a Transaction.
This example illustrates how to use the BEGIN keyword in a 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;