SlideShare a Scribd company logo
1 of 3
|||------|----T R I G G E R S---- |------|||

--EJEMPLO 0
SET SERVEROUTPUT ON

CREATE TABLE TABLA_TG(CAMPO1 NUMBER);

CREATE OR REPLACE TRIGGER TG_TABLA_01
      BEFORE INSERT ON TABLA_TG
      DECLARE
      BEGIN
      DBMS_OUTPUT.PUT_LINE('CODIGO LIBERADO ANTES DEL INSERT.');
END;
/

CREATE OR REPLACE TRIGGER TG_TABLA_02
AFTER INSERT ON TABLA_TG
DECLARE
BEGIN
DBMS_OUTPUT.PUT_LINE('CODIGO LIBERADO DESPUES DEL INSERT.');
END;
/

INSERT INTO TABLA_TG VALUES(1022086);

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----------------------
--EJEMPLO 1

SET SERVEROUTPUT ON size 1000000;
CREATE TRIGGER cuota BEFORE INSERT OR
UPDATE OF cuota2 ON DOS FOR EACH ROW// por cada renglon
BEGIN
IF (:new.cuota2<=0) then
      raise_application_error(-20000, 'No se permite una cuota negativa o igual
a cero');
END IF;
END cuota;
/
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------
--EJEMPLO 2

set   serveroutput on size 1000000;

create or replace trigger alumnuub_I_D_U
AFTER INSERT OR DELETE OR UPDATE ON UNO
BEGIN
IF INSERTING THEN
  dbms_output.put_line('Nuevo Alumno se ha Agregado');
end if;
      if updating then
      dbms_output.put_line('Un Alumno se ha Modificado');
      end if;
            if deleting then
            dbms_output.put_line('Un Alumno se ha Eliminado');
            end If;
END alumnuub_I_D_U;
/

insert into uno (clave1)values('1020');
update uno set nombre1='lulu' where clave1='1020';

select * from uno;



update uno set nombre1='lulu',apellido1='flores' where clave1='1020';


delete uno where clave1='1020';
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----------------------
---EJEMPLO 3------


CREAR UN TRIGGER PARA ACTUALIZAR EN CASCADA

---Tabla papa

CREATE TABLE t (a VARCHAR2(10) primary key);

---tabla Hijo0
Create table Tv (B number, a Varchar2(10),
constraint na FOREIGN KEY(A) REFERENCES t(A));

---TRIGGER
Create or REPLACE TRIGGER T_U BEFORE UPDATE
ON t FOR EACH ROW
BEGIN
UPDATE tv SET a=:NEW.a WHERE a=:OLD.a;
END T_U;
/

INSERT INTO t VALUES('tics');


INSERT   INTO   tv   VALUES(10220286,'tics');
INSERT   INTO   tv   VALUES(10220287,'tics');
INSERT   INTO   tv   VALUES(10220288,'tics');
INSERT   INTO   tv   VALUES(10220289,'tics');

actualizar un registro -------
update t set a='inf' where a= 'tics';


----bORRA REGISTRO

CREATE OR REPLACE TRIGGER T_B BEFORE DELETE
ON t FOR EACH ROW
BEGIN
  DELETE tv WHERE a=:OLD.a;
END T_B;
/

DELETE tv where a='inf';
--------------------------------------------------------------------------------
----------------------------------------------------------------------------


--------ACTIVADO , DESACTIVADO Y BORRADO DE TRIGGERS-------------------

DESACTIVACION-----------
ALTER TRIGGER UNO_I_D_U DISABLE;
ACTIVACION----------------
ALTER TRIGGER UNO_I_D_U ENABLE;

BORRAR--------
DROP TRIGGER UNO_I_D_U;
--------------------------------------------------------------------------------
-----------------------------------
--EJEMPLO 3

//TRIGGER PARA ACTUALIZAR EN CASCADA
CREATE TABLE t(a VARCHAR2(10) primary key);
//tabla hijo
CReaTE TABLE tv(B number, a varchar2(10),
constraint na foreign key(a) references t(a));
//TRIGGER

Create or replace trigger t_u before update
On t for each row
Begin
Update tv set a=:new.a where a=:old.a;
End t_u;
/

INSERT INTO T VALUES('ASSAD');
INSERT INTO TV VALUES(111,'ASSAD');

INSERT   INTO   T VALUES('D');
INSERT   INTO   TV VALUES(112,'D');
INSERT   INTO   TV VALUES(12,'Dado');
INSERT   INTO   TV VALUES(912,'D');
INSERT   INTO   TV VALUES(312,'D');

//ACTUALIZA EN CASCADA

update t set a= 'DADO' where a='D';

//BORRADO EN CASCADA ASIA TV
Create or replace trigger b_u before delete
On t for each row
Begin
delete tv where a=:old.a;
End t_u;
/
// BORRADO EN CASCADA

delete tv where a='DADO';

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Triggers

  • 1. |||------|----T R I G G E R S---- |------||| --EJEMPLO 0 SET SERVEROUTPUT ON CREATE TABLE TABLA_TG(CAMPO1 NUMBER); CREATE OR REPLACE TRIGGER TG_TABLA_01 BEFORE INSERT ON TABLA_TG DECLARE BEGIN DBMS_OUTPUT.PUT_LINE('CODIGO LIBERADO ANTES DEL INSERT.'); END; / CREATE OR REPLACE TRIGGER TG_TABLA_02 AFTER INSERT ON TABLA_TG DECLARE BEGIN DBMS_OUTPUT.PUT_LINE('CODIGO LIBERADO DESPUES DEL INSERT.'); END; / INSERT INTO TABLA_TG VALUES(1022086); -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ---------------------- --EJEMPLO 1 SET SERVEROUTPUT ON size 1000000; CREATE TRIGGER cuota BEFORE INSERT OR UPDATE OF cuota2 ON DOS FOR EACH ROW// por cada renglon BEGIN IF (:new.cuota2<=0) then raise_application_error(-20000, 'No se permite una cuota negativa o igual a cero'); END IF; END cuota; / -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ------------------------------ --EJEMPLO 2 set serveroutput on size 1000000; create or replace trigger alumnuub_I_D_U AFTER INSERT OR DELETE OR UPDATE ON UNO BEGIN IF INSERTING THEN dbms_output.put_line('Nuevo Alumno se ha Agregado'); end if; if updating then dbms_output.put_line('Un Alumno se ha Modificado'); end if; if deleting then dbms_output.put_line('Un Alumno se ha Eliminado'); end If; END alumnuub_I_D_U; / insert into uno (clave1)values('1020');
  • 2. update uno set nombre1='lulu' where clave1='1020'; select * from uno; update uno set nombre1='lulu',apellido1='flores' where clave1='1020'; delete uno where clave1='1020'; -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ---------------------- ---EJEMPLO 3------ CREAR UN TRIGGER PARA ACTUALIZAR EN CASCADA ---Tabla papa CREATE TABLE t (a VARCHAR2(10) primary key); ---tabla Hijo0 Create table Tv (B number, a Varchar2(10), constraint na FOREIGN KEY(A) REFERENCES t(A)); ---TRIGGER Create or REPLACE TRIGGER T_U BEFORE UPDATE ON t FOR EACH ROW BEGIN UPDATE tv SET a=:NEW.a WHERE a=:OLD.a; END T_U; / INSERT INTO t VALUES('tics'); INSERT INTO tv VALUES(10220286,'tics'); INSERT INTO tv VALUES(10220287,'tics'); INSERT INTO tv VALUES(10220288,'tics'); INSERT INTO tv VALUES(10220289,'tics'); actualizar un registro ------- update t set a='inf' where a= 'tics'; ----bORRA REGISTRO CREATE OR REPLACE TRIGGER T_B BEFORE DELETE ON t FOR EACH ROW BEGIN DELETE tv WHERE a=:OLD.a; END T_B; / DELETE tv where a='inf'; -------------------------------------------------------------------------------- ---------------------------------------------------------------------------- --------ACTIVADO , DESACTIVADO Y BORRADO DE TRIGGERS------------------- DESACTIVACION----------- ALTER TRIGGER UNO_I_D_U DISABLE;
  • 3. ACTIVACION---------------- ALTER TRIGGER UNO_I_D_U ENABLE; BORRAR-------- DROP TRIGGER UNO_I_D_U; -------------------------------------------------------------------------------- ----------------------------------- --EJEMPLO 3 //TRIGGER PARA ACTUALIZAR EN CASCADA CREATE TABLE t(a VARCHAR2(10) primary key); //tabla hijo CReaTE TABLE tv(B number, a varchar2(10), constraint na foreign key(a) references t(a)); //TRIGGER Create or replace trigger t_u before update On t for each row Begin Update tv set a=:new.a where a=:old.a; End t_u; / INSERT INTO T VALUES('ASSAD'); INSERT INTO TV VALUES(111,'ASSAD'); INSERT INTO T VALUES('D'); INSERT INTO TV VALUES(112,'D'); INSERT INTO TV VALUES(12,'Dado'); INSERT INTO TV VALUES(912,'D'); INSERT INTO TV VALUES(312,'D'); //ACTUALIZA EN CASCADA update t set a= 'DADO' where a='D'; //BORRADO EN CASCADA ASIA TV Create or replace trigger b_u before delete On t for each row Begin delete tv where a=:old.a; End t_u; / // BORRADO EN CASCADA delete tv where a='DADO';