• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Dasar Database
 

Dasar Database

on

  • 340 views

Dasar dasar Database

Dasar dasar Database

Statistics

Views

Total Views
340
Views on SlideShare
340
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Dasar Database Dasar Database Document Transcript

    • Nama : Arozisokhi ZebuaNim : 1042100986Mata Ujian : Basis Data LanjutJawaban soal:--Nomor 1—create database arozisokhi_zebua_uts_bdl;use arozisokhi_zebua_uts_bdl;create table provinsi(id_provinsi int not null auto_increment,nama_provinsi varchar(200),primary key(id_provinsi));create table kabupaten(id_kabupaten int not null auto_increment,id_provinsi int not null,nama_kabupaten varchar(200),foreign key(id_provinsi) references provinsi(id_provinsi),primary key (id_kabupaten, id_provinsi));create table alamat(id int not null auto_increment,id_provinsi int not null,id_kabupaten int not null,foreign key(id_provinsi) references provinsi(id_provinsi),foreign key(id_kabupaten) references kabupaten(id_kabupaten),primary key (id, id_kabupaten, id_provinsi)
    • );create table biodata(id int not null auto_increment,nama varchar(200),tgl_lahir date,primary key(id));create table transaksi(id int not null auto_increment,id_biodata int not null,tanggal_transaksi date,jumlah_transaksi int,foreign key (id_biodata) references biodata(id),primary key(id,id_biodata));
    • --Nomor 2----Procedure isi_Provinsi--delimiter $$createprocedure isi_provinsi()begindeclare p int;set p=0;label : loopset p=p+1;insert into provinsi (id_provinsi, nama_provinsi)values(p, concat("provinsi",p));if p <12 then iterate label; end if;leave label;end loop label;end$$delimiter ;call isi_provinsi();--Procedure Isi kabupaten--delimiter $$createprocedure isi_kabupaten()begindeclare p int;set p=0;label : loopset p=p+1;insert into kabupaten (id_provinsi, id_kabupaten, nama_kabupaten)
    • values(p, p, concat("kabupaten",p));if p <12 then iterate label; end if;leave label;end loop label;end$$delimiter ;call isi_kabupaten();--Procedure isi_alamat--delimiter $$createprocedure isi_alamat()begindeclare p int;set p=0;label : loopset p=p+1;insert into alamat (id, id_provinsi, id_kabupaten)values(p,p,p);if p <12 then iterate label; end if;leave label;end loop label;end$$delimiter ;call isi_alamat();--Procedure isi_biodata--delimiter $$create
    • procedure isi_biodata()begindeclare p int;set p=0;label : loopset p=p+1;insert into biodata (id, nama, tgl_lahir)values(p, concat("Biodata",p), concat("1992","-",p,"-",p));if p <12 then iterate label; end if;leave label;end loop label;end$$delimiter ;call isi_biodata();---Procedure isi_transaksi pertama--delimiter $$createprocedure isi_transaksi_pertama()begindeclare p int;set p=0;label : loopset p=p+1;insert into transaksi (id, id_biodata,tanggal_transaksi,jumlah_transaksi)values(p,p,concat("2000","-",p,"-",p),concat(p));if p <12 then iterate label; end if;leave label;end loop label;
    • end$$delimiter ;call isi_transaksi_pertama();---Procedure isi_transaksi kedua--delimiter $$createprocedure isi_transaksi_kedua()begindeclare p int;declare urut int;set p=0;set urut=12;label : loopset p=p+1;set urut = urut+1;insert into transaksi (id, id_biodata,tanggal_transaksi,jumlah_transaksi)values(urut, p,concat("2000","-",p,"-",p),concat(urut));if p <12 then iterate label; end if;leave label;end loop label;end$$delimiter ;call isi_transaksi_kedua();--procedure isi_transaksi_ketiga--delimiter $$create
    • procedure isi_transaksi_ketiga()begindeclare p int;declare urut int;declare urut2 int;set p=0;set urut=24;set urut2=15;label : loopset p=p+1;set urut = urut+1;set urut2 = urut2+1;insert into transaksi (id, id_biodata,tanggal_transaksi,jumlah_transaksi)values(urut, p,concat("2000","-",p,"-",p),concat(urut2));if p <12 then iterate label; end if;leave label;end loop label;end$$delimiter ;call isi_transaksi_ketiga();--procedure isi_transaksi_keempat--delimiter $$createprocedure isi_transaksi_keempat()begindeclare p int;declare urut int;declare urut2 int;set p=0;
    • set urut=36;set urut2=9;label : loopset p=p+1;set urut = urut+1;set urut2 = urut2+1;insert into transaksi (id, id_biodata,tanggal_transaksi,jumlah_transaksi)values(urut, p,concat("2000","-",p,"-",p),concat(urut2));if p <12 then iterate label; end if;leave label;end loop label;end$$delimiter ;call isi_transaksi_keempat();--Nomor 3—catatan yang bisa dihapushanya isi tabel transaksi--Trigger untuk provinsi---CREATE TABLE transaksi_deleted LIKE transaksi;ALTER TABLE transaksi_deleted ADD(tgl_hapus DATETIME,nama_user VARCHAR(200));
    • DELIMITER $$CREATETRIGGER hapus_transaksi AFTER DELETE ON transaksi FOR EACH ROWBEGININSERT INTO transaksi_deleted(id,id_biodata,tanggal_transaksi,jumlah_transaksi,tgl_hapus,nama_user)VALUES(OLD.id,OLD.id_biodata,OLD.tanggal_transaksi,OLD.jumlah_transaksi,SYSDATE(),CURRENT_USER);END;$$DELIMITER ;SHOW TRIGGERS FROM arozisokhi_zebua_uts_bdl;--Triger untuk restore data yang sudah dihapus--cara kerja: Jika data yang ada pada transaksi_deleted dihapus maka data tersebut otomatisdikembalikan ke tabel transaksi
    • DELIMITER $$CREATETRIGGER restore_transaksi AFTER DELETE ON transaksi_deleted FOR EACH ROWBEGININSERT INTO transaksi(id,id_biodata,tanggal_transaksi,jumlah_transaksi)VALUES(OLD.id,OLD.id_biodata,OLD.tanggal_transaksi,OLD.jumlah_transaksi);END;$$DELIMITER ;
    • --Nomor 4--A. Untuk Menampilkan biodata beserta umur dan alamatSELECTa.id,nama,tgl_lahir,(YEAR(CURDATE())-YEAR(tgl_lahir)) AS umur,(CONCAT(nama_provinsi,",",nama_kabupaten)) AS alamatFROMbiodata aLEFT JOIN alamat b ON (b.id=a.id)LEFT JOIN provinsi c ON (c.id_provinsi=b.id_provinsi)LEFT JOIN kabupaten d ON (d.id_kabupaten=b.id_kabupaten);Hasilny :
    • B. Untuk Menampilkan biodata beserta nama dan jumlah transaksi perbulanSELECTid,nama,IFNULL (jml_jan,0) AS Januari,IFNULL (jml_feb,0) AS Februari,IFNULL (jml_mar,0) AS Maret,IFNULL (jml_apr,0) AS April,IFNULL (jml_mei,0) AS Mei,IFNULL (jml_jun,0) AS Juni,IFNULL (jml_jul,0) AS Juli,IFNULL (jml_agst,0) AS Agustus,IFNULL (jml_sep,0) AS September,IFNULL (jml_okt,0) AS Oktober,IFNULL (jml_nov,0) AS November,IFNULL (jml_des,0) AS DesemberFROMbiodata aLEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_janFROM transaksiWHERE MONTH(tanggal_transaksi)=1GROUP BY id_biodata) AS Januari ON(Januari.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_febFROM transaksiWHERE MONTH(tanggal_transaksi)=2GROUP BY id_biodata) AS Februari ON(Februari.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_marFROM transaksiWHERE MONTH(tanggal_transaksi)=3
    • GROUP BY id_biodata) AS Maret ON(Maret.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_aprFROM transaksiWHERE MONTH(tanggal_transaksi)=4GROUP BY id_biodata) AS April ON(April.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_meiFROM transaksiWHERE MONTH(tanggal_transaksi)=5GROUP BY id_biodata) AS Mei ON(Mei.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_junFROM transaksiWHERE MONTH(tanggal_transaksi)=6GROUP BY id_biodata) AS Juni ON(Juni.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_julFROM transaksiWHERE MONTH(tanggal_transaksi)=7
    • GROUP BY id_biodata) AS Juli ON(Juli.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_agstFROM transaksiWHERE MONTH(tanggal_transaksi)=8GROUP BY id_biodata) AS Agustus ON(Agustus.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_sepFROM transaksiWHERE MONTH(tanggal_transaksi)=9GROUP BY id_biodata) AS September ON(September.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_oktFROM transaksiWHERE MONTH(tanggal_transaksi)= 10GROUP BY id_biodata) AS Oktober ON(Oktober.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_novFROM transaksi
    • WHERE MONTH(tanggal_transaksi)=11GROUP BY id_biodata) AS November ON(November.id_biodata=a.id)LEFT JOIN(SELECT id_biodata, SUM(jumlah_transaksi) AS jml_desFROM transaksiWHERE MONTH(tanggal_transaksi)=12GROUP BY id_biodata) AS Desember ON(Desember.id_biodata=a.id);Hasilnya :---Sekian Terimaksih, Tuhan Memberkati---