instead of trigger 예제 , 여러 테이블로 이루어진 뷰에 DML이 가해질 때 대신
trigger를 동작시킬 수 있는데...[ ORALCE TRIGGER]
이때 사용하는 것이 instead of trigger 이다.
CREATE TABLE department (
deptno NUMBER PRIMARY KEY,
deptname VARCHAR2(20),
manager_num NUMBER
);
CREATE TABLE employee (
empno NUMBER PRIMARY KEY,
empname VARCHAR2(20),
deptno NUMBER REFERENCES dept(deptno),
startdate DATE
);
CREATE VIEW manager_info AS
SELECT d.deptno, d.deptname, e.empno, e.empname
FROM employee e, department d
WHERE e.empno = d.manager_num;
CREATE OR REPLACE TRIGGER manager_info_insert
INSTEAD OF INSERT ON manager_info
FOR EACH ROW
DECLARE
employeeCount NUMBER;
BEGIN
SELECT COUNT(*) INTO employeeCount
FROM employee e
WHERE e.deptno = :new.deptno;
IF employeeCount >= 1 THEN
UPDATE department d
SET manager_num = :new.empno
WHERE d.deptno = :new.deptno;
ELSE
insert into department(deptno, deptname, manager_num)
values (:new.deptno, :new.deptname, 0);
insert into employee (empno, empname, deptno, startdate)
values (:new.empno, :new.empname, :new.deptno, sysdate);
END IF;
END;
/
SQL> INSERT INTO manager_info VALUES (300,'Sports',1003,'Jack');
1 개의 행이 만들어졌습니다.
SQL> commit;
커밋이 완료되었습니다.
SQL> select * from department;
DEPTNO DEPTNAME MANAGER_NUM
---------- -------------------- -----------
댓글 없음:
댓글 쓰기