Random Hadith

Narrated Jabir bin 'Abdullah: On the day of Al-Hudaibiya, Allah's Apostle said to us' "You are the best people on the earth!" We were 1400 then. If I could see [+] [Bukhari 059:475]

Mutiara Hati

Untuk mencapai puncak, Anda harus melalui anak tangga dan terus menerus naik, maka Anda akan mencapai puncak yang Anda inginkan.

Announcement

 

December 2009
M T W T F S S
« Nov   Jan »
 123456
78910111213
14151617181920
21222324252627
28293031  

Archives

Categories

Tags

free counters

Membuat TRIGGER pada database MySQL

Trigger adalah suatu prosedur yang otomatis akan dijalankan apabila “pemicu” dari trigger itu dipanggil.

sebagai contoh: penghapusan satu tabel akan memicu suatu trigger untuk menghapus pada tabel yang lain yang telah ditunjuk terlebih dahulu. dibawah ini saya berikan satu contoh penggunaan trigger pada database MySQL.

Sebagai contoh kita siapkan terlebih dahulu sebuah database dan tabel yang akan diuji coba seperti dibawah ini:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
create database mytrigger;
 
create table coba (
kode varchar(5) not null,nama varchar(35) not null,
primary key (kode))
 
create table coba2 (
kode varchar(5) not null,nama varchar(35) not null,
primary key (kode))
 
create table tran (
kode varchar(5) not null,
kodetran varchar(5),jumlah double,
primary key nkode (kode,kodetran))

Kemudian buatlah sebuah trigger dengan kode seperti dibawah ini:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
delimiter $$
 
create trigger auto_insert_coba2
before insert on coba for each row
begin
  insert into coba2 (kode,nama) values (NEW.kode,NEW.nama);
end$$
 
create trigger auto_update_coba2
before update on coba for each row
begin
  update coba2 set nama=NEW.nama where kode=NEW.kode;
end$$
 
create trigger auto_delete_coba2
before delete on coba for each row
begin
delete from coba2 where kode=OLD.kode;
delete from tran where kode=OLD.kode;
end$$

pada trigger di atas setiap kali perubahan dilakukan terhadap tabel COBA maka secara otomatis
tabel COBA2 akan dipengaruhi / terpengaruh.
Pembuatan trigger ini dapat digunakan untuk berbagai keperluan, selamat mencoba…..!

Jangan lupa untuk memberikan komentar anda. terima kasih.

Popularity: 12% [?]

11 comments to Membuat TRIGGER pada database MySQL

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

CommentLuv Enabled
[+] kaskus emoticons nartzco