OK, first check this solution
SQL> create table tb_coba1 (id number , nis number , nilai_a number , semester number);
Table created.
SQL> create table tb_coba2 (id number , nis number , nilai_b number , semester number);
Table created.
SQL> insert into tb_coba1 values (1,1,1,1);
1 row created.
SQL> insert into tb_coba1 values (1,2,2,2);
1 row created.
SQL>
SQL> create or replace trigger t_cb
2 after insert on tb_coba1
3 for each row
4 begin
5 insert into TB_COBA2 (nis , nilai_b , semester)
6 select :new.nis , :new.nilai_a , :new.semester
7 from dual
8 where not exists (
9 select 1 from tb_coba2
10 where nis = :new.nis and semester = :new.semester
11 );
12 end;
13 /
Trigger created.
SQL> sho err
No errors.
SQL>
SQL> insert into TB_COBA1 values (3,3,3,3);
1 row created.
SQL> select * from tb_coba2;
ID NIS NILAI_B SEMESTER
---------- ---------- ---------- ----------
3 3 3
SQL> insert into TB_COBA1 values (4,3,3,3);
1 row created.
SQL> select * from tb_coba2;
ID NIS NILAI_B SEMESTER
---------- ---------- ---------- ----------
3 3 3
That *looks* like it will work...but its actually wrong. Because what if TWO people do that insert at the same time. Each will NOT see the others insert (because we have not committed yet), so you will STILL end up with 2 occurrence of "3,3" in the table.
How do we fix that ?
Option 1 - make the nis/semester unique on TB_COBA2 using a constraint. Then our trigger has to take care of the potential error that might then come back
SQL> alter table tb_coba2 add constraint tb_coba2_uq unique ( nis, semester );
Table altered.
SQL> create or replace trigger t_cb
2 after insert on tb_coba1
3 for each row
4 begin
5 insert into TB_COBA2 (nis , nilai_b , semester)
6 select :new.nis , :new.nilai_a , :new.semester
7 from dual
8 where not exists (
9 select 1 from tb_coba2
10 where nis = :new.nis and semester = :new.semester
11 );
12 exception
13 when dup_val_on_index then null;
14 end;
15 /
Trigger created.
Now if two people try to insert the same data, one will wait until the other is finished (committed).
If you dont want a constraint, then you can be more brutal and lock the whole table when you insert, ie,
SQL> create or replace trigger t_cb
2 after insert on tb_coba1
3 for each row
4 begin
5 lock table TB_COBA2 in exclusive mode;
6
7 insert into TB_COBA2 (nis , nilai_b , semester)
8 select :new.nis , :new.nilai_a , :new.semester
9 from dual
10 where not exists (
11 select 1 from tb_coba2
12 where nis = :new.nis and semester = :new.semester
13 );
14 end;
15 /
Trigger created.