SQL>
SQL> create table company(
2 product_id number(4) not null,
3 company_id NUMBER(8) not null,
4 company_short_name varchar2(30) not null,
5 company_long_name varchar2(60)
6 );
Table created.
SQL> create table product_audit(
2 product_id number(4) not null,
3 num_rows number(8) not null
4 );
Table created.
SQL>
SQL>
SQL>
SQL>
SQL> CREATE OR REPLACE TRIGGER myTrigger
2 AFTER INSERT OR DELETE ON company
3 FOR EACH ROW
4 BEGIN
5 IF INSERTING THEN
6 UPDATE product_audit
7 SET num_rows =num_rows+1
8 WHERE product_id =:NEW.product_id;
9 IF (SQL%NOTFOUND) THEN
10 INSERT INTO product_audit VALUES (:NEW.product_id,1);
11 END IF;
12 ELSIF DELETING THEN
13 UPDATE product_audit
14 SET num_rows =num_rows-1
15 WHERE product_id =:OLD.product_id;
16 END IF;
17 END;
18 /
Trigger created.
SQL>
SQL>
SQL> insert into company values(1,1001,'A Inc.','Long Name A Inc.');
1 row created.
SQL> insert into company values(1,1002,'B Inc.','Long Name B Inc.');
1 row created.
SQL> insert into company values(1,1003,'C Inc.','Long Name C Inc.');
1 row created.
SQL> insert into company values(2,1004,'D Inc.','Long Name D Inc.');
1 row created.
SQL> insert into company values(2,1005,'E Inc.','Long Name E Inc.');
1 row created.
SQL> insert into company values(2,1006,'F Inc.','Long Name F Inc.');
1 row created.
SQL>
SQL>
SQL>
SQL>
SQL> drop table product_audit;
Table dropped.
SQL>
SQL> drop table company;
Table dropped.
SQL>
|