Oracle PL / SQL – INSERTトリガーの前の例
この記事では、BEFORE INSERT TRIGGER
の使用方法を説明します。これは、INSERT操作が実行される前に発生します。 実際のシナリオでは、次のような目的で主に使用されます
-
データ検証
-
値を自動的に更新します(例:CREATED_BY、CREATION_DATEなど)
1. 表
employee_details
を作成します。このテーブルにさまざまな値を挿入して、トリガーの動作を観察します。
employee_details
--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 );
出力
table EMPLOYEE_DETAILS created.
2. データ検証
2.1 am before insert trigger example to restrict invalid data entry:
-
ユーザーは従業員の生年月日を入力できません。これは、従業員の18歳の規則に準拠していません。
-
ユーザーは将来の死亡日を入力できません。
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. 値を更新
一部の値を自動的に更新する前挿入トリガーの例。
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 |
ファーストネーム |
苗字 |
生年月日 |
死亡日 |
によって作成された |
作成日 |
1 |
パテル |
トーマス |
1999年5月18日 |
2017年5月1日 |
HR |
2017年5月24日 |
-- 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 |
ファーストネーム |
苗字 |
生年月日 |
死亡日 |
によって作成された |
作成日 |
1 |
パテル |
トーマス |
1999年5月18日 |
2017年5月1日 |
HR |
2017年5月24日 |
2 |
パテル |
メシュー |
1990年1月1日 |
2005年5月1日 |
HR |
2017年5月24日 |
3 |
パテル |
メシュー |
1990年1月1日 |
2005年5月1日 |
HR |
2017年5月24日 |