You are on page 1of 4

Práctica Básica de Transacciones

Profa. María Lourdes Geizzelez

mysql> create database operaciones;

Query OK, 1 row affected (0.00 sec)

mysql> use operaciones;

Database changed

mysql> create table producto(id int not null primary key


auto_increment,

-> descripcion varchar(45)not null,

-> cantidad int);

Query OK, 0 rows affected (0.20 sec)

mysql> insert into producto (descripcion,cantidad)


values("TortaCapita",30);

Query OK, 1 row affected (0.13 sec)

mysql> insert into producto (descripcion,cantidad) values("TE",150);

Query OK, 1 row affected (0.08 sec)

//Inicio la transacción

mysql> start transaction;

Query OK, 0 rows affected (0.00 sec)


//Modifico la cantidad del producto cuyo id sea 1(30*2=60)

mysql> update producto set cantidad= cantidad*2 where id=1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from producto;

+----+-------------+----------+

| id | descripcion | cantidad |

+----+-------------+----------+

| 1 | TortaCapita | 60 |//Tengo ahora 60 tortas de capita

| 2 | TE | 150 |

+----+-------------+----------+

2 rows in set (0.00 sec)

mysql> rollback; // Cancelo la modificación(deshace la ultima modif.)

Query OK, 0 rows affected (0.13 sec)

mysql> select * from producto;// Observa que tengo en cantidad 30 y


no 60 tortas

+----+-------------+----------+

| id | descripcion | cantidad |

+----+-------------+----------+

| 1 | TortaCapita | 30 |
| 2 | TE | 150 |

+----+-------------+----------+

2 rows in set (0.00 sec)

//Realizo otra modificacion

mysql> update producto set cantidad= cantidad*3 where id=1;

Query OK, 1 row affected (0.05 sec)

Rows matched: 1 Changed: 1 Warnings: 0

// Muestro cantidad = 90 antes se tenía cantidad =30

mysql> select * from producto;

+----+-------------+----------+

| id | descripcion | cantidad |

+----+-------------+----------+

| 1 | TortaCapita | 90 |

| 2 | TE | 150 |

+----+-------------+----------+

2 rows in set (0.00 sec)

//Almaceno la Modificación Permanentemente, Finaliza la transacción

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from producto;

+----+-------------+----------+

| id | descripcion | cantidad |
+----+-------------+----------+

| 1 | TortaCapita | 90 |

| 2 | TE | 150 |

+----+-------------+----------+

2 rows in set (0.00 sec)

//Pruebo colocando un Rollback, pero NO deshace.

mysql> rollback;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from producto;

+----+-------------+----------+

| id | descripcion | cantidad |

+----+-------------+----------+

| 1 | TortaCapita | 90 |

| 2 | TE | 150 |

+----+-------------+----------+

2 rows in set (0.00 sec)

mysql>

You might also like