Aportes a la Arquitectura de Le Corbusier y Mies Van der Rohe
Trigger
1. EQUIPO : Investigation Systems
Estudiantes : Aguilar Rafael Jaime
Diaz Guerrero Imer Hobet
CURSO : Base de Datos II
TEMA : TRIGGER
DOCENTE : Marco Aurelio Porro Chulli
Ingeniería de Sistemas y Telemática
3. Activadores
Es una clase especial de procedimiento
almacenado que se ejecuta
automáticamente cuando se produce un
evento en el servidor de bases de datos.
Ingeniería de Sistemas y Telemática
4. Sintaxis
CREATE TRIGGER <Nombre del
Trigger>
ON <Nombre de la Tabla>
AFTER <INSERT, DELETE, UPDATE>
AS
BEGIN
-- Inserta aquí las
instrucciones
END
Ingeniería de Sistemas y Telemática
6. Creación De Activadores
CREATE TRIGGER tr_InsertaVideoFormato
ON TbVideo
FOR Insert
AS
DECLARE @idfor Char(3)
SELECT @idfor=idformato FROM Inserted
UPDATE TbFormato
SET cantvideo=cantvideo+1
WHERE idformato=@idfor
Go
Ingeniería de Sistemas y Telemática
7. Modificación
CREATE TRIGGER tr_Actualiza_Producto
On TbProducto
INSTEAD OF UPDATE
AS
DECLARE @CODPRO CHAR(4)
SELECT @CODPRO=CodProducto FROM inserted
DECLARE @PRECIOUNI DECIMAL(9,2)
SELECT @PRECIOUNI=PrecioUnitario FROM INSERTED
UPDATE TbProducto SET PrecioUnitario=(@PRECIOUNI+PrecioUnitario)/2
WHERE CodProducto=@CODPRO
PRINT 'Registro Actualizado'
RETURN
go
Ingeniería de Sistemas y Telemática
8. Eliminación
CREATE TRIGGER tr_EliminaVideoFormato
ON TbVideo
FOR Delete
AS
DECLARE @idfor Char(3)
SELECT @idfor=idformato FROM Deleted
UPDATE TbFormato
SET cantvideo=cantvideo-1
WHERE idformato=@idfor
Go
Ingeniería de Sistemas y Telemática
11. Cuando se inserta un registro en la
Tabla TbVideo se incrementa en 1 en
campo cantvideo de la tabla TbFormato
CREATE TRIGGER tr_InsertaVideoFormato
ON TbVideo
FOR Insert
AS
DECLARE @idfor Char(3)
SELECT @idfor=idformato FROM Inserted
UPDATE TbFormato
SET cantvideo=cantvideo+1
WHERE idformato=@idfor
Go
Ingeniería de Sistemas y Telemática
12. Cuando se elimina un registro en la
Tabla TbVideo se disminuye en 1 en
campo cantvideo de la tabla TbFormato
CREATE TRIGGER tr_EliminaVideoFormato
ON TbVideo
FOR Delete
AS
DECLARE @idfor Char(3)
SELECT @idfor=idformato FROM Deleted
UPDATE TbFormato
SET cantvideo=cantvideo-1
WHERE idformato=@idfor
Go
Ingeniería de Sistemas y Telemática
13. Crear un activador que controle la
eliminación de una tabla en la bdvideo
CREATE TRIGGER tr_seguridad_Datos
ON DATABASE
FOR DROP_TABLE,ALTER_TABLE
AS
BEGIN
RAISERROR('No se tiene permiso
para eliminar tablas',16,1)
ROLLBACK TRANSACTION
END
RESULTADO
Ingeniería de Sistemas y Telemática
14. Cuando se elimina un registro en la Tabla
TbVideo se disminuye en 1 en campo
cantvideo de la tabla TbFormato
CREATE TRIGGER tr_EliminaVideoFormato
ON TbVideo
FOR Delete
AS
DECLARE @idfor Char(3)
SELECT @idfor=idformato FROM Deleted
UPDATE TbFormato
SET cantvideo=cantvideo-1
WHERE idformato=@idfor
Go
Ingeniería de Sistemas y Telemática