DISPARADORES TRIGGERS 3

Páginas: 2 (399 palabras) Publicado: 20 de noviembre de 2015
DISPARADORES
(TRIGGERS)

DISPARADORES
(Triggers)





Son porciones de código sql.
Se invocan cuando hay un evento o suceso
No aceptan argumentos
Se ejecuta cuando hay operación básica de:INSERT,
DELETE, UPDATE
Usos:
• Auditorias.
• Restricciones.
• Reportar eventos o sucesos.

DISPARADORES
(Triggers)

Table
DML

View
Instead of

System

ESTRUCTURA TRIGGERS
EVENTO

CONDICION

CREATE [ORREPLACE] TRIGGER nombre
{BEFORE | AFTER | INSTEAD OF}
{INSERT | DELETE | UPDATE [OF ]} ON

[FOR EACH ROW | STATEMENT]
[WHEN condición]
INSERTING - DELETING - UPDATING

ACCION[DECLARE…]
BEGIN
cuerpo del trigger
[EXCEPTION…]
END;

BASE DE DATOS
Creamos la siguiente tabla:

01

Create table PRODUCTOS(
Codigo number not null,
Nombre varchar2(45) not null,
Saldo number,
ConstraintPK_PRODUCTOS primary key(codigo));

Creamos la siguiente tabla:

03

Create table REGISTROS(
VALOR VARCHAR2(200) NOT NULL
);

Creamos la siguiente tabla:

02

Create table MOVIMIENTOS(
Consecutivonumber not null,
Producto number not null,
Tipo_movimiento varchar2(1) not null,
Cantidad number,
Constraint FK_Mov_PRODUCTO FOREIGN KEY
(producto) references productos (codigo)
);

create or replace viewV_PRODUCTOS as
select CODIGO, NOMBRE SALDO
from PRODUCTOS
where SALDO >10

04

Ejemplo de TRIGGERS (DML)
Crear un triggers que actualice el saldo de la tabla productos, sumando o restando lacantidad del movimiento al saldo, si el tipo de movimiento es igual “S” lo sumara y si es
“R” lo restara.
01
create or replace trigger actualiza_productos
after insert on movimientos
for each row
declare
--local variables here
begin
if :new.tipo_movimiento = ‘E' then
update productos set saldo = saldo + :new.cantidad
where codigo = :new.producto;
else
update productos set saldo = saldo - :new.cantidadwhere codigo = :new.producto;
end if;
end actualiza_productos;

Ejemplo de TRIGGERS (Instead OF)
Crear un triggers que actualice la tabla REGISTROS usando una vista.

create or replace trigger...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • triggers
  • Triggers
  • Triggers
  • TRIGGERS
  • Triggers
  • Triggers
  • triggers
  • Triggers

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS