Está en la página 1de 7

TRIGGER USANDO EL UPDATE

Se crea la tabla que va a auditar

create table auditoria_cliente (id_audi integer primary key auto_increment,


audi_nombre_anterior varchar(30), audi_nombre_nuevo varchar(30), audi_fecha_modificacion
datetime, audi_usuario varchar(45), audi_id_cliente int(11) not null, audi_accion varchar(45));

Se crea el trigger

create trigger auditoria_modificacliente before update on cliente

for each row

insert into auditoria_cliente

(audi_nombre_anterior, audi_nombre_nuevo, audi_fecha_modificacion, audi_usuario,


audi_id_cliente, audi_accion)

values (old.nom_cliente,new.nom_cliente,now(),current_user(),new.id_cliente,'Actualización');

se modifica un campo de la tabla cliente….

update cliente set nom_cliente ='Juan Jose' where id_cliente = 2222;

Se revisa la modificación de la tabla cliente

mysql> select * from cliente;

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

| id_cliente | nom_cliente |

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

| 1111 | Isabella |

| 2222 | Juan Jose |

| 3333 | Alex |

| 4444 | Andrea |
+------------+-------------+

4 rows in set (0.00 sec)

Se revisa la tabla auditoria_cliente

mysql> select * from auditoria_cliente;

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

| id_audi | audi_nombre_anterior | audi_nombre_nuevo | audi_fecha_modificacion |


audi_usuario | audi_id_cliente | audi_accion |

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

| 1 | Juanjo | Juan Jose | 2018-06-11 19:10:46 | root@localhost | 2222 |


Actualización |

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

1 row in set (0.00 sec)

DISPARADOR PARA ELIMINAR

create trigger auditoria_eliminacliente after delete on cliente

-> for each row

-> insert into auditoria_cliente

-> (audi_nombre_anterior, audi_fecha_modificacion, audi_usuario, audi_id_cliente,


audi_accion)

-> values (old.nom_cliente,now(),current_user(),old.id_cliente,'Eliminación');

Query OK, 0 rows affected (0.19 sec)

CONSULTAR LA TABLA CLIENTE

mysql> select * from cliente;


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

| id_cliente | nom_cliente |

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

| 1111 | Isabella |

| 2222 | Juan Jose |

| 3333 | Alex |

| 4444 | Andrea |

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

4 rows in set (0.00 sec)

ELIMINAMOS UN REGISTRO DE LA TABLA CLIENTE

mysql> delete from cliente where id_cliente = 4444;

Query OK, 1 row affected (0.16 sec)

CONSULTAMOS LA TABLA AUDITORIA_CLIENTE

mysql> select * from auditoria_cliente;

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

| id_audi | audi_nombre_anterior | audi_nombre_nuevo | audi_fecha_modificacion |


audi_usuario | audi_id_cliente | audi_accion |

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

| 1 | Juanjo | Juan Jose | 2018-06-11 19:10:46 | root@localhost | 2222 |


Actualización |

| 2 | Andrea | NULL | 2018-06-11 19:26:10 | root@localhost | 4444 |


Eliminación |

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

2 rows in set (0.00 sec)


TRIGGER PARA INSERCION DE DATOS

create trigger auditoria_insertacliente before insert on cliente

-> for each row

-> insert into auditoria_cliente

-> (audi_nombre_nuevo, audi_fecha_modificacion, audi_usuario, audi_id_cliente, audi_accion)

-> values (new.nom_cliente,now(),current_user(),new.id_cliente,'Inserción');

Query OK, 0 rows affected (0.07 sec)

CONSULTAMOS LA TABLA CLIENTE

mysql> select * from cliente;

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

| id_cliente | nom_cliente |

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

| 1111 | Isabella |

| 2222 | Juan Jose |

| 3333 | Alex |

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

3 rows in set (0.00 sec)

INSERTAMOS UN REGISTRO A LA TABLA CLIENTE

mysql> insert into cliente values (4444,'Andrea');

Query OK, 1 row affected (0.20 sec)

CONSULTAMOS LA TABLA AUDITORIA

mysql> select * from auditoria_cliente;

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

| id_audi | audi_nombre_anterior | audi_nombre_nuevo | audi_fecha_modificacion |


audi_usuario | audi_id_cliente | audi_accion |
+---------+----------------------+-------------------+-------------------------+----------------+-----------------
+---------------+

| 1 | Juanjo | Juan Jose | 2018-06-11 19:10:46 | root@localhost | 2222 |


Actualización |

| 2 | Andrea | NULL | 2018-06-11 19:26:10 | root@localhost | 4444 |


Eliminación |

| 3 | NULL | Andrea | 2018-06-11 19:34:07 | root@localhost | 4444 |


Inserción |

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

3 rows in set (0.00 sec)

POR ULTIMO, MODIFICAMOS EL REGISTRO ANDREA POR ANDREA YANETH

mysql> update cliente set nom_cliente ='Andrea Yaneth' where id_cliente = 4444;

Query OK, 1 row affected (0.16 sec)

Rows matched: 1 Changed: 1 Warnings: 0

CONSULTAMOS LA TABLA AUDITORIA_CLIENTE

mysql> select * from auditoria_cliente;

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

| id_audi | audi_nombre_anterior | audi_nombre_nuevo | audi_fecha_modificacion |


audi_usuario | audi_id_cliente | audi_accion |

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

| 1 | Juanjo | Juan Jose | 2018-06-11 19:10:46 | root@localhost | 2222 |


Actualización |

| 2 | Andrea | NULL | 2018-06-11 19:26:10 | root@localhost | 4444 |


Eliminación |

| 3 | NULL | Andrea | 2018-06-11 19:34:07 | root@localhost | 4444 |


Inserción |

| 4 | Andrea | Andrea Yaneth | 2018-06-11 19:35:42 | root@localhost |


4444 | Actualización |
+---------+----------------------+-------------------+-------------------------+----------------+-----------------
+---------------+

4 rows in set (0.00 sec)

-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------

delimiter//

create function contarclientes () returns int

begin

declare cantidad int

select count (*) into cantidad from tbl_cliente

return cantidad

end

//

FUNCION CONCATENAR SALUDO CON NOMBRES Y APELLIDOS

delimiter //

create function fullname (nom_cli varchar(30), ape_cli varchar(30))

returns varchar(30)

begin

declare message varchar(30);

set message = "Hola";

return concat(message, " ", nom_cli, " ", ape_cli);

end;

//

Query OK, 0 rows affected (0.018 sec)


SE EJECUTA ASÍ

select fullname ("andrea", "gonzalez")//

Crear funciones en mySQL - Bytes - Bing video

delimiter *

MariaDB [administracion1]> create function contarclientes()

-> returns int

-> begin

-> declare cantidad int;

-> return cantidad;

-> end;

-> *

Query OK, 0 rows affected (0.011 sec)

SE EJECUTA

Select contarclientes(); //

También podría gustarte