Oracle PL / SQL - Exemple avant INSERT Trigger
Cet article vous montre comment utiliserBEFORE INSERT TRIGGER
, il se déclenche AVANT qu’une opération INSERT ne soit exécutée. Dans des scénarios réels, il est principalement utilisé à des fins telles que
-
La validation des données
-
Mettre à jour les valeurs automatiquement (par exemple CREATED_BY, CREATION_DATE, etc.)
1. Table
Créez unemployee_details
, nous allons essayer d'insérer différentes valeurs dans ce tableau et observer le comportement du déclencheur.
Détails de l'employé
--Creating employee_details table. CREATE TABLE employee_details ( EMP_ID number(10) primary key, FIRST_NAME varchar2(50), LAST_NAME varchar2(50), DATE_OF_BIRTH date, DATE_OF_DEATH date, CREATED_BY varchar2(20), CREATED_DATE date );
Sortie
table EMPLOYEE_DETAILS created.
2. La validation des données
2.1 am before insert trigger example to restrict invalid data entry:
-
L'utilisateur ne peut pas saisir la date de naissance des employés, ce qui n'est pas conforme à la règle de 18 ans de l'employé.
-
L'utilisateur ne peut pas saisir la date future du décès.
2.2 Create a trg_before_emp_insr
trigger on table employee_details
trg_before_emp_insr
CREATE OR REPLACE TRIGGER trg_before_emp_insr BEFORE INSERT on employee_details FOR EACH ROW DECLARE emp_age number; BEGIN -- Finding employee age by date of birth SELECT MONTHS_BETWEEN(TO_DATE(sysdate,'DD-MON-YYYY'), TO_DATE(:new.DATE_OF_BIRTH,'DD-MON-YYYY'))/12 INTO EMP_AGE FROM DUAL; -- Check whether employee age is greater than 18 or not IF (EMP_AGE < 18) THEN RAISE_APPLICATION_ERROR(-20000,'Employee age must be greater than or equal to 18.'); END IF; -- Allow only past date of death IF(:new.DATE_OF_DEATH > sysdate) THEN RAISE_APPLICATION_ERROR(-20000,'Date of death can not be Future date.'); END IF; END;
2.3 Normal data.
-- setting date format to to 'DD-MON-YYYY' alter session set nls_date_format = 'DD-MON-YYYY'; INSERT INTO employee_details VALUES (1,'Patel','Thomas','18-MAY-1999','01-MAY-2017','HR',sysdate); -- output 1 rows inserted.
2.4 Test trigger raise error – Employee age must be greater than or equal to 18.
-- setting date format to to 'DD-MON-YYYY' alter session set nls_date_format = 'DD-MON-YYYY'; INSERT INTO employee_details VALUES (2,'Patel','Peter','18-MAY-2010','01-MAY-2017','HR',sysdate); -- error Error report - ORA-20000: Employee age must be greater than or equal to 18. ORA-06512: at "SYSTEM.TRG_BEFORE_EMP_INSR", line 18 ORA-04088: error during execution of trigger 'SYSTEM.TRG_BEFORE_EMP_INSR'
2.5 Test trigger raise error – Date of death can not be Future date.
-- setting date format to to 'DD-MON-YYYY' alter session set nls_date_format = 'DD-MON-YYYY'; INSERT INTO employee_details VALUES (3,'Patel','Thomas','18-MAY-1999','01-MAY-2040','HR',sysdate); -- error Error report - ORA-20000: Date of death can not be Future date. ORA-06512: at "SYSTEM.TRG_BEFORE_EMP_INSR", line 23 ORA-04088: error during execution of trigger 'SYSTEM.TRG_BEFORE_EMP_INSR'
3. Mettre à jour les valeurs
Un exemple de déclencheur avant insertion pour mettre à jour automatiquement certaines valeurs.
trg_before_emp_insr_userinfo
CREATE OR REPLACE TRIGGER trg_before_emp_insr_userinfo BEFORE INSERT ON employee_details FOR EACH ROW DECLARE username varchar2(20); BEGIN -- Replaced by the current logged in user "HR" by a trigger. SELECT USER INTO username FROM dual; -- Setting created_by and created_Date values. :NEW.CREATED_BY := username; :NEW.CREATED_DATE := sysdate; END;
-- setting date format to to 'DD-MON-YYYY' alter session set nls_date_format = 'DD-MON-YYYY'; select * from employee_details;
EMP_ID |
PRÉNOM |
NOM DE FAMILLE |
DATE DE NAISSANCE |
DATE DE DÉCÈS |
CRÉÉ PAR |
CREATED_DATE |
1 |
Patel |
Thomas |
18-MAI-1999 |
01-MAI-2017 |
HR |
24-MAI-2017 |
-- setting date format to to 'DD-MON-YYYY' alter session set nls_date_format = 'DD-MON-YYYY'; INSERT INTO employee_details VALUES (2,'Patel','Methew','01-JAN-1990','01-MAY-2005',null,null); INSERT INTO employee_details VALUES (3,'Patel','Methew','01-JAN-1990','01-MAY-2005','XYZ',null); select * from employee_details;
EMP_ID |
PRÉNOM |
NOM DE FAMILLE |
DATE DE NAISSANCE |
DATE DE DÉCÈS |
CRÉÉ PAR |
CREATED_DATE |
1 |
Patel |
Thomas |
18-MAI-1999 |
01-MAI-2017 |
HR |
24-MAI-2017 |
2 |
Patel |
Methew |
01-JANV-1990 |
01-MAI-2005 |
HR |
24-MAI-2017 |
3 |
Patel |
Methew |
01-JANV-1990 |
01-MAI-2005 |
HR |
24-MAI-2017 |