Dasar Database

316 views

Published on

Dasar dasar Database

Published in: Education, Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
316
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dasar Database

  1. 1. 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)
  2. 2. );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));
  3. 3. --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)
  4. 4. 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
  5. 5. 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;
  6. 6. 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
  7. 7. 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;
  8. 8. 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));
  9. 9. 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
  10. 10. 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 ;
  11. 11. --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 :
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. 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---

×