Skip to main content
se añadieron 172 caracteres en el cuerpo
Origen Enlace
wchiquito
  • 1.2k
  • 8
  • 9

Los cambios del disparador (trigger) forman parte de la transacción, por ello, los cambios se revierten al hacer ROLLBACK.

Ejemplo:

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.19    |
+-----------+
1 row in set (0.00 sec)

mysql> DROP PROCEDURE IF EXISTS `sp_transaction`; 
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `tbl_one`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `tbl_one` (
    ->   `id` BIGINT UNSIGNED NOT NULL PRIMARY KEY,
    ->   `value` INT
    -> ); ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TRIGGER `ins_value` BEFORE INSERT ON `tbl_one`
    -> FOR EACH ROW
    -> SET NEW.`value` := NEW.`value` * 100;
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER //

mysql> CREATE PROCEDURE `sp_transaction`(`error` BOOL)
    -> BEGIN
    ->   START TRANSACTION;
    ->   INSERT INTO `tbl_one` (`id`, `value`)
    ->   VALUES (1, `error`);
    ->   IF `error` THEN
    ->     ROLLBACK;
    ->   ELSE
    ->     COMMIT;
    ->   END IF;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CALL `sp_transaction`(1); -- ROLLBACK
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT `id`, `value`
    -> FROM `tbl_one`;
Empty set (0.01 sec)

mysql> CALL `sp_transaction`(0); -- COMMIT
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT `id`, `value`
    -> FROM `tbl_one`;
+----+-------+
| id | value |
+----+-------+
|  1 |     0 |
+----+-------+
1 row in set (0.00 sec)

Ver db-fiddledb-fiddle.

Los cambios del disparador (trigger) forman parte de la transacción, por ello, los cambios se revierten al hacer ROLLBACK.

Ejemplo:

mysql> DROP PROCEDURE IF EXISTS `sp_transaction`; 
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `tbl_one`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `tbl_one` (
    ->   `id` BIGINT UNSIGNED NOT NULL PRIMARY KEY,
    ->   `value` INT
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TRIGGER `ins_value` BEFORE INSERT ON `tbl_one`
    -> FOR EACH ROW
    -> SET NEW.`value` := NEW.`value` * 100;
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER //

mysql> CREATE PROCEDURE `sp_transaction`(`error` BOOL)
    -> BEGIN
    ->   START TRANSACTION;
    ->   INSERT INTO `tbl_one` (`id`, `value`)
    ->   VALUES (1, `error`);
    ->   IF `error` THEN
    ->     ROLLBACK;
    ->   ELSE
    ->     COMMIT;
    ->   END IF;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CALL `sp_transaction`(1); -- ROLLBACK
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT `id`, `value`
    -> FROM `tbl_one`;
Empty set (0.01 sec)

mysql> CALL `sp_transaction`(0); -- COMMIT
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT `id`, `value`
    -> FROM `tbl_one`;
+----+-------+
| id | value |
+----+-------+
|  1 |     0 |
+----+-------+
1 row in set (0.00 sec)

Ver db-fiddle.

Los cambios del disparador (trigger) forman parte de la transacción, por ello, los cambios se revierten al hacer ROLLBACK.

Ejemplo:

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.19    |
+-----------+
1 row in set (0.00 sec)

mysql> DROP PROCEDURE IF EXISTS `sp_transaction`; 
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `tbl_one`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `tbl_one` (
    ->   `id` BIGINT UNSIGNED NOT NULL PRIMARY KEY,
    ->   `value` INT
    -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TRIGGER `ins_value` BEFORE INSERT ON `tbl_one`
    -> FOR EACH ROW
    -> SET NEW.`value` := NEW.`value` * 100;
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER //

mysql> CREATE PROCEDURE `sp_transaction`(`error` BOOL)
    -> BEGIN
    ->   START TRANSACTION;
    ->   INSERT INTO `tbl_one` (`id`, `value`)
    ->   VALUES (1, `error`);
    ->   IF `error` THEN
    ->     ROLLBACK;
    ->   ELSE
    ->     COMMIT;
    ->   END IF;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CALL `sp_transaction`(1); -- ROLLBACK
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT `id`, `value`
    -> FROM `tbl_one`;
Empty set (0.01 sec)

mysql> CALL `sp_transaction`(0); -- COMMIT
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT `id`, `value`
    -> FROM `tbl_one`;
+----+-------+
| id | value |
+----+-------+
|  1 |     0 |
+----+-------+
1 row in set (0.00 sec)

Ver db-fiddle.

Origen Enlace
wchiquito
  • 1.2k
  • 8
  • 9

Los cambios del disparador (trigger) forman parte de la transacción, por ello, los cambios se revierten al hacer ROLLBACK.

Ejemplo:

mysql> DROP PROCEDURE IF EXISTS `sp_transaction`; 
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `tbl_one`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `tbl_one` (
    ->   `id` BIGINT UNSIGNED NOT NULL PRIMARY KEY,
    ->   `value` INT
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TRIGGER `ins_value` BEFORE INSERT ON `tbl_one`
    -> FOR EACH ROW
    -> SET NEW.`value` := NEW.`value` * 100;
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER //

mysql> CREATE PROCEDURE `sp_transaction`(`error` BOOL)
    -> BEGIN
    ->   START TRANSACTION;
    ->   INSERT INTO `tbl_one` (`id`, `value`)
    ->   VALUES (1, `error`);
    ->   IF `error` THEN
    ->     ROLLBACK;
    ->   ELSE
    ->     COMMIT;
    ->   END IF;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CALL `sp_transaction`(1); -- ROLLBACK
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT `id`, `value`
    -> FROM `tbl_one`;
Empty set (0.01 sec)

mysql> CALL `sp_transaction`(0); -- COMMIT
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT `id`, `value`
    -> FROM `tbl_one`;
+----+-------+
| id | value |
+----+-------+
|  1 |     0 |
+----+-------+
1 row in set (0.00 sec)

Ver db-fiddle.