Академический Документы
Профессиональный Документы
Культура Документы
UN TRIGGER O DISPARADOR ES UN BLOQUE DE CDIGO QUE SE EJECUTA AUTOMTICAMENTE CUANDO OCURRE ALGN EVENTO (COMO INSERCIN, ACTUALIZACIN O BORRADO) SOBRE UNA TABLA ES DECIR CUANDO SE INTENTAN MODIFICAR LOS DATOS DE UNA TABLA ASOCIADA A UN DISPARADOR.
create or replace trigger NOMBREDISPARADOR MOMENTO delete On NOMBRETABLA NIVEL statement o for each row begin CUERPO DEL TRIGGER End NOMBRE DISPARADOR
Ejemplo. SQL> create table productos( id_producto number(6), Nombre_Producto varchar(40), Precio_Producto varchar(30), Cantidad_Producto number(4), constraint pk_productos primary key (id_producto)); Table created. SQL> create table control( user_name varchar(50), fecha date); Table created.
Creamos el disparador
SQL> create trigger ingresar_productos before insert on productos begin insert into control values (user,sysdate); end ingresar_productos; /
Trigger created.
Session altered.
Leccin 3
Disparador de mltiples eventos Disparador (old , new) Disparador de condiciones whem
SQL> create table productos( 2 id_producto number(6), 3 Nombre_Producto varchar(40), 4 Precio_Producto varchar(30), 5 Cantidad_Producto number(4), 6 constraint pk_productos primary key (id_producto));
Table created.
SQL> create table control( 2 user_name varchar(50), 3 fecha date 4 operacion varchar(20)); Table created.
Luego creamos un disparador a nivel de sentencia que se dispare cada vez que se ingrese , actualice y se elimine un registro de la tabla SQL> create or replace trigger cambios_productos 2 before insert or update or delete 3 on productos 4 for each row 5 begin 6 if inserting then 7 insert into control values (user,sysdate,'insercion'); 8 end if; 9 if updating then 10 insert into control values (user,sysdate,'actualizacion'); 11 end if; 12 if deleting then insert into control values (user,sysdate,'borrado'); 13 end if; 14 end cambios_productos; 15 / Trigger created.
SQL> select * from control; USER_NAME FECHA OPERACION -------------------- -------- -------------------SYSTEM 13/11/12 insercion
SQL> insert into productos values (10,'arroz',3,20); 1 row created. SQL> insert into productos values (20,'atun',5,30); 1 row created. SQL> insert into productos values (30,'cerillo',1,15); 1 row created.
SQL> update productos set precio=precio+precio*0.1 where codigo=30; 1 row updated. SQL> select * from control; USUARIO FECHA CODIGO PRECIO_ANTERIOR PRECIO_NUEVO -------------------- -------- ---------- --------------- -----------SYSTEM 13/11/12 10 20 100 SYSTEM 13/11/12 30 15 16,5
Todo lo visto anterior mente era sobre system a continuacin lo vamos hacer sobre usuarios determinados crearemos dos usuarios y le estaremos asignando los privilegios correspondientes volveremos a crear las mismas tablas pero sobre un usuario y que el otro usuario le inserte los datos, tambin crearemos el mismo trigger. Lo dicho anteriormente continuacin. lo estaremos mostrando a
SQL> create table mercancia( 2 IdMercancia number(6), 3 NombreMercancia varchar(40), 4 PrecioMercancia varchar(30), 5 CantidadMercancia number(4), 6 constraint pk_mercancia primary key(IdMercancia));
Table created.
SQL> create table administracion( 2 username varchar(50), 3 fecha date); Table created.
ESTO ES EL TRIGGER QUE SE ESTAR EJECUTANDO CUANDO SE INSERTEN DATOS SOBRE MERCANCA.
SQL> create trigger tr_ingresar_mercancia 2 before insert 3 on mercancia 4 begin 5 insert into administracion values(user, sysdate); 6 end tr_ingresar_mercancia; 7 /
Trigger created.
ESTE COMANDO ES PARA ALTERAR LA HORA Y LA FECHA EN LA QUE SE INSERTEN LOS DATOS SOBRE LA TABLA CONTROL.
SQL> alter session set NLS_DATE_FORMAT = 'DD/MM/YYY HH24:MI';
Tabla que muestra los usuarios que insertaron as como tambin la fecha de insercin el comando de alter session es para modificar la fecha a continuacin mostraremos otra pantalla.