Documentos de Académico
Documentos de Profesional
Documentos de Cultura
• Calcule el valor promedio de los cursos cuyas horas sean mayores a 40.
CREATE FUNCTION `valorpromedio`(`input` INT) RETURNS INT(11) NOT DETE
RMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN DECLARE prome
dio int; SELECT AVG(valor_cur) INTO promedioFROM curso WHERE horas_cur>
='40'; RETURN promedio; END
SELECT `valorpromedio`(@p0) AS `valorpromedio`;
• Obtener el sueldo promedio de los profesores de la categoría 1.
CREATE FUNCTION `sueldopromedio`(`input` INT) RETURNS INT(11) NOT DET
ERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN DECLARE prom
edio int; SELECT AVG(sal_prof) INTO promedioFROM profesor WHERE cate_prof
='1'; RETURN promedio; END
SELECT `sueldopromedio`(@p0) AS `sueldopromedio`;
• Muestre el nombre del profesor con menor sueldo.
CREATE FUNCTION `menorsueldo`(`inout` INT) RETURNS VARCHAR(30) NOT
DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN DECLARE
nombre varchar(30);SELECT nom_prof INTO nombre FROM profesor where sal_p
rof=(select min(sal_prof) from profesor); RETURN nombre; END
SELECT `menorsueldo`(@p0) AS `menorsueldo`;
Borrado
Profesor
CREATE TRIGGER `profesordelete` AFTER DELETE ON `profesor` FOR EACH R
OW INSERT INTO trigger_profesores (DocumentoAnterior, NombreAnterior, Apelli
doAnterior, CategoriaAnterior,SalarioAnterior, DocumentoNuevo, NombreNuevo, A
pellidoNuevo, CategoriaNuevo, SalarioNuevo) VALUES (OLD.doc_prof, OLD.nom
_prof, OLD.ape_prof, OLD.cate_prof, OLD.sal_prof);
Curso
CREATE TRIGGER `cursodelete` AFTER DELETE ON `curso` FOR EACH ROW I
NSERT INTO trigger_cursos(CodigoAnterior, NombreAnterior, HorasAnterior, Valo
rAnterior, CodigooNuevo,NombreNuevo,HorasNuevo, ValorNuevo) VALUES (OLD
.cod_curs, OLD.nom_curs, OLD.horas_cur, OLD.valor_cur);
Estudiante
CREATE TRIGGER `estudiantedelete` AFTER DELETE ON `estudiante` FOR EA
CH ROW INSERT INTO trigger_estudiante (DocumentoAnterior,NombreAnterior,
ApellidoAnterior, EdadAnterior, DocumentoNuevo, NombreNuevo, ApellidoNuevo,
EdadNuevo) VALUES (OLD.doc_est, OLD.nom_est, OLD.ape_est, OLD.edad_est)
• En ORACLE; para las tablas Cliente, Articulo, Pedido.
Actualización:
Cliente
create or replace trigger "CLIENTEUPDATE"
BEFORE
update on "CLIENTE"
for each row
begin
INSERT INTO trigger_cliente (CodigoAnterior, NombreAnterior, ApellidoAnterior,
DireccionAnterior,DepartamentoAnterior, MesCumpleAnterior, CodigoNuevo,
NombreNuevo, ApellidoNuevo, DireccionNuevo, DepartamentoNuevo,
MesCumpleNuevo) VALUES (:OLD.id_cli, :OLD.nom_cli, :OLD.ape_cli,
:OLD.dir_cli, :OLD.dep_cli, :OLD.mes_cum_cli, :NEW.id_cli,
:NEW.nom_cli, :NEW.ape_cli, :NEW.dir_cli, :NEW.dep_cli, :NEW.mes_cum_cli);
end;
Articulo
create or replace trigger "ARTICULOUPDATE"
BEFORE
update on "ARTICULO"
for each row
begin
INSERT INTO trigger_articulo (CodigoAnterior, NombreAnterior, AutorAnterior,
EditorialAnterior, PrecioAnterior, CodigoNuevo, NombreNuevo, AutorNuevo,
EditorialNuevo, PrecioNuevo) VALUES (:OLD.id_art, :OLD.tit_art, :OLD.aut_art,
:OLD.edi_art, :OLD.prec_art, :NEW.id_art, :NEW.tit_art, :NEW.aut_art,
:NEW.edi_art, :NEW.prec_art);
end;
Pedido
create or replace trigger "PEDIDOUPDATE"
BEFORE
update on "PEDIDO"
for each row
begin
INSERT INTO trigger_pedido (CodigoAnterior, DocumentoClienteAnterior,
FechaAnterior, ValorAnterior, CodigoNuevo, DocumentoClienteNuevo,
FechaNuevo, ValorNuevo) VALUES (:OLD.id_ped, :OLD.id_cli_ped,
:OLD.fec_ped, :OLD.val_ped, :NEW.id_ped, :NEW.id_cli_ped, :NEW.fec_ped,
:NEW.val_ped);
end;
Borrado
Cliente
create or replace trigger "CLIENTEDELETE"
AFTER
delete on "CLIENTE"
for each row
begin
INSERT INTO trigger_cliente (CodigoAnterior, NombreAnterior, ApellidoAnterior,
DireccionAnterior,DepartamentoAnterior, MesCumpleAnterior, CodigoNuevo,
NombreNuevo, ApellidoNuevo, DireccionNuevo, DepartamentoNuevo,
MesCumpleNuevo) VALUES (:OLD.id_cli, :OLD.nom_cli, :OLD.ape_cli,
:OLD.dir_cli, :OLD.dep_cli, :OLD.mes_cum_cli);
end;
Articulo
create or replace trigger "ARTICULODELETE"
AFTER
delete on "ARTICULO"
for each row
begin
INSERT INTO trigger_articulo (CodigoAnterior, NombreAnterior, AutorAnterior,
EditorialAnterior, PrecioAnterior, CodigoNuevo, NombreNuevo, AutorNuevo,
EditorialNuevo, PrecioNuevo) VALUES (:OLD.id_art, :OLD.tit_art, :OLD.aut_art,
:OLD.edi_art, :OLD.prec_art);
end;
Pedido
create or replace trigger "PEDIDODELETE"
AFTER
delete on "PEDIDO"
for each row
begin
INSERT INTO trigger_pedido (CodigoAnterior, DocumentoClienteAnterior,
FechaAnterior, ValorAnterior, CodigoNuevo, DocumentoClienteNuevo,
FechaNuevo, ValorNuevo) VALUES (:OLD.id_ped, :OLD.id_cli_ped,
:OLD.fec_ped, :OLD.val_ped);
end;
• En el Motor de su preferencia (ORACLE O MySQL), para las tablas,
Automotores, Aseguramientos, Incidentes.
Actualización:
Automotores
CREATE TRIGGER `automotoresupdate` BEFORE UPDATE ON `automotores` F
OR EACH ROW INSERT INTO trigger_automotores (PlacaAnterior, MarcaAnterior
, TipoAnterior, ModeloAnterior,NumeroPasajerosAnterior, CilindrajeAnterior, Nume
roChasisAnterior, PlacaNuevo, MarcaNuevo, TipoNuevo, ModeloNuevo, NumeroP
asajerosNuevo, CilindrajeNuevo, NumeroChasisNuevo)VALUES (OLD.autoplaca,
OLD.automarca, OLD.autotipo, OLD.automodelo, OLD.autonumpasajeros, OLD.au
tocilindraje, OLD.autonumchasis, NEW.autoplaca, NEW.automarca, NEW.autotipo,
NEW.automodelo, NEW.autonumpasajeros, NEW.autocilindraje, NEW.autonumch
asis)
Aseguramientos
CREATE TRIGGER `aseguramientosupdate` BEFORE UPDATE ON `aseguramie
ntos` FOR EACH ROW INSERT INTO trigger_aseguramientos(CodigoAnterior, Fe
chaInicioAnterior,FechaExpiracionAnterior, ValorAseguradoAnterior, EstadoAnterio
r, CostoAnterior, PlacaAnterior, CodigoNuevo, FechaInicioNuevo, FechaExpiracion
Nuevo, ValorAseguradoNuevo,EstadoNuevo, CostoNuevo, PlacaNuevo) VALUES
(OLD.asecodigo, OLD.asefechainicio, OLD.asefechaexpiracion, OLD.asevaloraseg
urado, OLD.aseestado, OLD.asecosto, OLD.aseplaca,NEW.asecodigo, NEW.asef
echainicio, NEW.asefechaexpiracion, NEW.asevalorasegurado, NEW.aseestado,
NEW.asecosto, NEW.aseplaca)
Incidentes
CREATE TRIGGER `incidentesupdate` BEFORE UPDATE ON `incidentes` FOR E
ACH ROW INSERT INTO trigger_incidentes(CodigoAnterior, FechaAnterior, Placa
Anterior, LugarAnterior,CantidadHeridosAnterior, CantidadFatalidadesAnterior, Ca
ntidadAutosInvolucradosAnterior, CodigoNuevo, FechaNuevo, PlacaNuevo, Lugar
Nuevo, CantidadHeridosNuevo,CantidadFatalidadesNuevo, CantidadAutosInvolucr
adosNuevo) VALUES (OLD.incicodigo, OLD.incifecha, OLD.inciplaca, OLD.incilug
ar, OLD.incicantheridos, OLD.incicanfatalidades,OLD.incicanautosinvolucrados, N
EW.incicodigo, NEW.incifecha, NEW.inciplaca, NEW.incilugar, NEW.incicantherido
s, NEW.incicanfatalidades, NEW.incicanautosinvolucrados)
Borrado
Automotores
CREATE TRIGGER `automotoresdelete` AFTER DELETE ON `automotores` FOR
EACH ROW INSERT INTO trigger_automotores (PlacaAnterior, MarcaAnterior, Tip
oAnterior, ModeloAnterior,NumeroPasajerosAnterior, CilindrajeAnterior, NumeroCh
asisAnterior, PlacaNuevo, MarcaNuevo, TipoNuevo, ModeloNuevo, NumeroPasaje
rosNuevo, CilindrajeNuevo, NumeroChasisNuevo)VALUES (OLD.autoplaca, OLD.
automarca, OLD.autotipo, OLD.automodelo, OLD.autonumpasajeros, OLD.autocili
ndraje, OLD.autonumchasis)
Aseguramientos
CREATE TRIGGER `aseguramientosdelete` AFTER DELETE ON `aseguramiento
s` FOR EACH ROW INSERT INTO trigger_aseguramientos(CodigoAnterior, Fecha
InicioAnterior,FechaExpiracionAnterior, ValorAseguradoAnterior, EstadoAnterior, C
ostoAnterior, PlacaAnterior, CodigoNuevo, FechaInicioNuevo, FechaExpiracionNu
evo, ValorAseguradoNuevo,EstadoNuevo, CostoNuevo, PlacaNuevo) VALUES (O
LD.asecodigo, OLD.asefechainicio, OLD.asefechaexpiracion, OLD.asevalorasegur
ado, OLD.aseestado, OLD.asecosto, OLD.aseplaca)
Incidentes