create table department (
id_dept int primary key,
name varchar2(50),
total_sal number(10,2) default 0
)
Table created.
create table employee (
id_empl int primary key,
name varchar2(50),
salary number(8,2) default 0,
id_dept int references department
)
Table created.
insert into employee (id_empl, name, salary) values (1, 'gabriel', 999999.99)
1 row(s) inserted.
select * from employee
ID_EMPL | NAME | SALARY | ID_DEPT | 1 | gabriel | 999999.99 | - |
---|
create sequence seq_empl
Sequence created.
select seq_empl.nextval from dual
NEXTVAL | 1 |
---|
create or replace trigger tg_empl_before_insert
before insert
on employee
for each row
begin
select seq_empl.nextval into :new.id_empl from dual;
end;
Trigger created.
insert into employee (name,salary) values ('yoda', 3000.00);
select * from employee
ID_EMPL | NAME | SALARY | ID_DEPT | 1 | gabriel | 999999.99 | - | 2 | yoda | 1000 | - |
---|
create sequence seq_dept
Sequence created.
create or replace trigger tg_dept_before_insert
before insert
on department
for each row
begin
select seq_dept.nextval into :new.id_dept from dual;
end;
Trigger created.
insert into department (name) values ('tecnology')
1 row(s) inserted.
select * from department
ID_DEPT | NAME | TOTAL_SAL | 1 | tecnology | 0 |
---|
create or replace trigger tg_empl_after_insert
after insert
on employee
for each row
when (new.id_dept is not null)
begin
update department dept set dept.total_sal = dept.total_sal + :new.salary where :new.id_dept = dept.id_dept;
end;
Trigger created.
insert into employee (name, salary, id_dept) values ('maria', 2000.0, 1)
1 row(s) inserted.
select * from department
ID_DEPT | NAME | TOTAL_SAL | 1 | tecnology | 2000 |
---|
create or replace trigger tg_empl_after_update
after update
on employee
for each row
begin
update department deptOld set deptOld.total_sal = deptOld.total_sal - :old.salary where deptOld.id_dept = :old.id_dept;
update department deptNew set deptNew.total_sal = deptNew.total_sal + :new.salary where deptNew.id_dept = :new.id_dept;
end;
Trigger created.
update employee set salary = 6000.0 where id_empl = 3
1 row(s) updated.
select * from department
ID_DEPT | NAME | TOTAL_SAL | 1 | tecnology | 6000 |
---|
insert into department (name) values ('rh')
1 row(s) inserted.
insert into employee (name, salary, id_dept) values ('jose', 1000.0, 2);
select * from department
ID_DEPT | NAME | TOTAL_SAL | 2 | rh | 1000 | 1 | tecnology | 6000 |
---|
update employee set id_dept = 2 where id_empl = 3
1 row(s) updated.
select * from department
ID_DEPT | NAME | TOTAL_SAL | 2 | rh | 7000 | 1 | tecnology | 0 |
---|
update employee set salary = 9000.0, id_dept = 1 where id_empl = 1
1 row(s) updated.
update employee set id_dept = 2 where id_empl = 2;
ID_DEPT | NAME | TOTAL_SAL | 2 | rh | 7000 | 1 | tecnology | 9000 |
---|
select * from department;
create or replace trigger tg_empl_after_delete
after delete
on employee
for each row
begin
update department dept set dept.total_sal = dept.total_sal - :old.salary where dept.id_dept = :old.id_dept;
end;
Trigger created.
delete from employee where id_empl = 4
1 row(s) deleted.
select * from employee
ID_EMPL | NAME | SALARY | ID_DEPT | 1 | gabriel | 9000 | 1 | 2 | yoda | 1000 | - | 3 | maria | 6000 | 2 |
---|
select * from department
ID_DEPT | NAME | TOTAL_SAL | 2 | rh | 6000 | 1 | tecnology | 9000 |
---|