Skip to content Skip to sidebar Skip to footer

Kumpulan Soal Trigger Sistem Basis Data

 

 

 


Perhatikan code SQL berikut!
DELIMITER //
CREATE TRIGGER post_hutang_nota AFTER INSERT ON tbl_hutang_nota FOR EACH ROW
BEGIN
UPDATE tbl_hutang_bayar SET status = 1 WHERE id_hutang_nota = new.id_hutang_nota;
End
// 
DELIMITER ;
Perintah untuk membuat trigger adalah .........CREATE TRIGGER

new.table_name maksudnya adalah nilai record yang baru pada tabel 
 

old.table_name maksudnya adalah nilai record yang lama pada tabel 


Perhatikan code SQL berikut!
DELIMITER //
CREATE TRIGGER tbl_beli_subt_upd AFTER UPDATE ON tbl_beli
FOR EACH ROW
BEGIN

IF new.harga_beli <=> old.harga_beli and new.jumlah <=> old.jumlah THEN
UPDATE tbl_beli SET sub_total = (new.harga_beli * new.jumlah) WHERE id_beli = old.id_beli;
END IF;

IF new.harga_beli = old.harga_beli and new.jumlah <=> old.jumlah THEN
UPDATE tbl_beli SET sub_total = (old.harga_beli * new.jumlah) WHERE id_beli = old.id_beli;
END IF;

IF new.harga_beli <=> old.harga_beli and new.jumlah = old.jumlah THEN
UPDATE tbl_beli SET sub_total = (new.harga_beli * old.jumlah) WHERE id_beli = old.id_beli;
END IF;

END;
// 
DELIMITER;
trigger_event terjadi pada saat .........update

Perhatikan code SQL berikut!
DELIMITER //
CREATE TRIGGER upd_sbt AFTER INSERT ON tbl_beli
FOR EACH ROW
BEGIN
set @id_beli = new.id_beli;
set @harga_beli = new.harga_beli;
set @jumlah = new.jumlah;
set @sub_total = @harga_beli * @jumlah;
UPDATE tbl_beli SET sub_total = @sub_total WHERE id_beli = @id_beli
END;
// 
DELIMITER;
trigger_event terjadi pada saat ......... INSERT

Perhatikan code SQL berikut!
DELIMITER //
CREATE TRIGGER trig_del_transaksi after DELETE ON tbl_transaksi FOR EACH ROW
BEGIN
IF old.ktg_transaksi = 'Penjualan' THEN 
UPDATE tbl_barang SET stok = stok + old.jmlh WHERE kode = old.kode_brg;
END IF;
IF old.ktg_transaksi = 'Pembelian' THEN 
UPDATE tbl_barang SET stok = stok - old.jmlh WHERE kode = old.kode_brg;
END IF;
END 
// 
DELIMITER ;
trigger_event terjadi pada saat setelah proses .........DELETE

Perhatikan code SQL berikut!
DELIMITER //
CREATE TRIGGER trig_upd_transaksi after UPDATE ON tbl_transaksi FOR EACH ROW
BEGIN
IF new.ktg_transaksi = 'Penjualan' THEN 
UPDATE tbl_barang SET stok = stok + old.jmlh - new.jmlh WHERE kode = new.kode_brg;
END IF;
IF new.ktg_transaksi = 'Pembelian' THEN 
UPDATE tbl_barang SET stok = stok - old.jmlh + new.jmlh, hrgbli = new.hrgbli WHERE kode = new.kode_brg;
END IF;
END 
// 
DELIMITER ;
trigger_event terjadi pada saat setelah proses .........UPDATE

Perhatikan code SQL berikut!
DELIMITER //
CREATE TRIGGER trig_transaksi after INSERT ON tbl_transaksi FOR EACH ROW
BEGIN
IF new.ktg_transaksi = 'Penjualan' THEN 
UPDATE tbl_barang SET stok = stok - new.jmlh WHERE kode = new.kode_brg;
END IF;
IF new.ktg_transaksi = 'Pembelian' THEN 
UPDATE tbl_barang SET stok = stok + new.jmlh, hrgbli = new.hrgbli WHERE kode = new.kode_brg;
END IF;
END 
// 
DELIMITER ;
Nama trigger diatas adalah .........TRIG_TRANSAKSI

Perhatikan code SQL berikut!
DELIMITER //
CREATE TRIGGER tbl_beli_subt_upd AFTER UPDATE ON tbl_beli
FOR EACH ROW
BEGIN

IF new.harga_beli <=> old.harga_beli and new.jumlah <=> old.jumlah THEN
UPDATE tbl_beli SET sub_total = (new.harga_beli * new.jumlah) WHERE id_beli = old.id_beli;
END IF;

IF new.harga_beli = old.harga_beli and new.jumlah <=> old.jumlah THEN
UPDATE tbl_beli SET sub_total = (old.harga_beli * new.jumlah) WHERE id_beli = old.id_beli;
END IF;

IF new.harga_beli <=> old.harga_beli and new.jumlah = old.jumlah THEN
UPDATE tbl_beli SET sub_total = (new.harga_beli * old.jumlah) WHERE id_beli = old.id_beli;
END IF;

END;
// 
DELIMITER;
trigger_time terjadi pada saat .........AFTER

Perhatikan code SQL berikut!
DELIMITER //
CREATE TRIGGER upd_sbt AFTER INSERT ON tbl_beli
FOR EACH ROW
BEGIN
set @id_beli = new.id_beli;
set @harga_beli = new.harga_beli;
set @jumlah = new.jumlah;
set @sub_total = @harga_beli * @jumlah;
UPDATE tbl_beli SET sub_total = @sub_total WHERE id_beli = @id_beli
END;
// 
DELIMITER;
trigger_time terjadi pada saat .........AFTER

Andriyansyah Yusuf
Andriyansyah Yusuf Life is not fair, get used to it.

Post a Comment for "Kumpulan Soal Trigger Sistem Basis Data"