Anda di halaman 1dari 6

create table BARANG (

KODE_BARANG CHAR(6) not null,


NAMA_BARANG VARCHAR2(25) not null,
SATUAN_BARANG VARCHAR2(20) not null,
STOK_BARANG NUMBER(4) not null,
constraint PK_BARANG primary key (KODE_BARANG)
);
create table CUSTOMER (
KODE_CUSTOMER CHAR(6) not null,
NAMA_CUSTOMER VARCHAR2(30) not null,
ALAMAT_CUSTOMER VARCHAR2(30) not null,
KOTA_CUSTOMER VARCHAR2(15) not null,
TELEPON_CUSTOMER VARCHAR2(15),
constraint PK_CUSTOMER primary key (KODE_CUSTOMER)
);
create table PASOK (
KODE_BARANG CHAR(6) not null,
KODE_SUPLIER CHAR(5) not null,
KODE_PASOK CHAR(10) not null,
TANGGAL_PASOK DATE,
JUMLAH_PASOK NUMBER(4),
constraint PK_PASOK primary key (KODE_BARANG, KODE_SUPLIER, KODE_PASOK)
);
create index PASOK_FK on PASOK (
KODE_BARANG ASC
);
create index PASOK2_FK on PASOK (
KODE_SUPLIER ASC
);
create table PEMBELIAN (
KODE_BARANG CHAR(6) not null,
KODE_CUSTOMER CHAR(6) not null,
KODE_PEMBELIAN CHAR(10) not null,
TANGGAL_PEMBELIAN DATE,
JUMLAH_PEMBELIAN NUMBER(4),
constraint PK_PEMBELIAN primary key (KODE_BARANG, KODE_CUSTOMER, KODE_PEMBELI
AN)
);
create index PEMBELIAN_FK on PEMBELIAN (
KODE_BARANG ASC
);
create index PEMBELIAN2_FK on PEMBELIAN (
KODE_CUSTOMER ASC
);
create table SUPLIER (
KODE_SUPLIER CHAR(5) not null,
NAMA_SUPLIER VARCHAR2(30) not null,
ALAMAT_SUPLIER VARCHAR2(30) not null,
KOTA_SUPLIER VARCHAR2(15) not null,
TELEPON_SUPLIER VARCHAR2(15),
constraint PK_SUPLIER primary key (KODE_SUPLIER)
);
alter table barang
modify SATUAN_BARANG varchar2(5);
alter table barang
modify STOK_BARANG number(3);
alter table customer
modify ALAMAT_CUSTOMER varchar2(50);
alter table pasok
modify JUMLAH_PASOK number(3);
alter table pembelian
modify JUMLAH_PEMBELIAN number(3);
alter table suplier
modify ALAMAT_SUPLLIER varchar2(50);
alter table pasok
modify KODE_PASOK char(8);
alter table pembelian
modify KODE_PEMBELIAN char(8);
insert into barang values('ELK001', 'Televisi', 'Unit', '10');
insert into barang values('ELK002', 'Lemari Es', 'Unit', '5');
insert into barang values('ELK003', 'Rice Cooker', 'Buah', '20');
insert into barang values('ELK004', 'Air Conditioner', 'Buah', '10');
insert into barang values('ELK005', 'Radio', 'Buah', '15');
insert into customer values('CST001', 'Toko Karisma', 'Jl.Laksana B.26', 'Bandun
g', '085722034995');
insert into customer values('CST002', 'Toko Surya', 'Jl.Jenderal Ahmad Yani No.6
30', 'Bandung', '0222787449');
insert into customer values('CST003', 'Toko Bungsu', 'Jl.Jendral Achmad Yani No.
34', 'Bekasi', '08529908876');
insert into customer values('CST004', 'Toko Tunggal', 'Jl.Haji Zainal Mustofa No
.148', 'Tasikmalaya', '081534587699');
insert into customer values('CST005', 'Toko Suara', 'Jl.Karang Anyar No.2', 'Cir
ebon', '0856875648478');
insert into suplier values('SPL01', 'PT.ACTRON', 'Jl.Cimanggis 534', 'Jakarta',
'0217225678');
insert into suplier values('SPL02', 'PT.MULYA ELEKTRONIK', 'Jl.ABC 309', 'Bandun
g', '0225223305');
insert into suplier values('SPL03', 'PT.SUPERTRON', 'Jl.Thamrin 12', 'Jakarta',
'0216604092');
insert into suplier values('SPL04', 'PT.COOLEST', 'Jl.Sudirman 45', 'Jakarta', '
0218554262');
insert into suplier values('SPL05', 'PT.SOUND ELEKTRONIK', 'Jl.Industri 56', 'Ba
ndung', '0214326024');
insert into pasok values('ELK001', 'SPL01', 'PSK01001', to_date('22-09-2010','dd
-mm-yyyy'), '5');
insert into pasok values('ELK001', 'SPL05', 'PSK01002', to_date('18-10-2010','dd
-mm-yyyy'), '3');
insert into pasok values('ELK002', 'SPL04', 'PSK02003', to_date('22-09-2010','dd
-mm-yyyy'), '6');
insert into pasok values('ELK003', 'SPL02', 'PSK03004', to_date('10-11-2010','dd
-mm-yyyy'), '10');
insert into pasok values('ELK003', 'SPL03', 'PSK03005', to_date('22-10-2010','dd
-mm-yyyy'), '6');
insert into pasok values('ELK003', 'SPL01', 'PSK03006', to_date('22-09-2010','dd
-mm-yyyy'), '2');
insert into pasok values('ELK004', 'SPL03', 'PSK04007', to_date('10-11-2010','dd
-mm-yyyy'), '3');
insert into pasok values('ELK004', 'SPL04', 'PSK04008', to_date('12-12-2010','dd
-mm-yyyy'), '3');
insert into pasok values('ELK005', 'SPL01', 'PSK05009', to_date('10-11-2010','dd
-mm-yyyy'), '4');
insert into pasok values('ELK005', 'SPL05', 'PSK05010', to_date('18-10-2010','dd
-mm-yyyy'), '6');
insert into pembelian values('ELK001', 'CST001', 'PMB01006', to_date('22-10-2010
','dd-mm-yyyy'), '2');
insert into pembelian values('ELK001', 'CST001', 'PMB01011', to_date('18-11-2010
','dd-mm-yyyy'), '2');
insert into pembelian values('ELK002', 'CST002', 'PMB01007', to_date('28-09-2010
','dd-mm-yyyy'), '3');
insert into pembelian values('ELK003', 'CST002', 'PMB01012', to_date('30-11-2010
','dd-mm-yyyy'), '6');
insert into pembelian values('ELK003', 'CST003', 'PMB01008', to_date('22-12-2010
','dd-mm-yyyy'), '5');
insert into pembelian values('ELK003', 'CST003', 'PMB01013', to_date('28-09-2010
','dd-mm-yyyy'), '2');
insert into pembelian values('ELK004', 'CST004', 'PMB01009', to_date('01-12-2010
','dd-mm-yyyy'), '2');
insert into pembelian values('ELK004', 'CST004', 'PMB01014', to_date('25-12-2010
','dd-mm-yyyy'), '2');
insert into pembelian values('ELK005', 'CST005', 'PMB01010', to_date('15-11-2010
','dd-mm-yyyy'), '3');
insert into pembelian values('ELK005', 'CST005', 'PMB01015', to_date('18-12-2010
','dd-mm-yyyy'), '5');

update barang
set STOK_BARANG = 8
where KODE_BARANG = 'ELK002';
create or replace trigger tr_update_barang
after update on barang
for each row
begin
dbms_output.put_line('Tabel barang telah di-update');
end;
/
create or replace trigger tr_insert_barang
after insert on barang
for each row
begin
dbms_output.put_line('Tabel barang telah di-insert');
end;
/
create or replace trigger tr_delete_barang
after delete on barang
for each row
begin
dbms_output.put_line('Tabel barang telah di-delete');
end;
/
create or replace trigger tr_cekjmlbeli
before insert on pembelian
for each row
declare
stokbrg barang.stok_barang%type;
nmbrg barang.nama_barang%type;
begin
select stok_barang, nama_barang
into stokbrg, nmbrg
from barang
where kode_barang = :new.kode_barang;
if(:new.jumlah_pembelian > stokbrg) then
dbms_output.put_line('Transaksi Gagal');
dbms_output.put_line('Stok Barang Tidak Mencukupi');
dbms_output.put_line('Nama Barang' || nmbrg);
dbms_output.put_line('Stok Barang' || stokbrg);
:new.jumlah_pembelian := 0;
end if;
end;
/
set serveroutput on
declare
cursor cur_customer is
select kode_customer, nama_customer from customer
order by kode_customer;
vRec cur_customer%ROWTYPE;
begin
open cur_customer;
loop
fetch cur_customer into vRec;
exit when cur_customer%NOTFOUND;
dbms_output.put_line(vRec.kode_customer || ' ' || vRec. nama_customer);
end loop;
close cur_customer;
end;
/
set serveroutput on
declare
cursor cur_pembelian is
select kode_barang, kode_pembelian, jumlah_pembelian
from pembelian
order by jumlah_pembelian;
vRec cur_pembelian%ROWTYPE;
begin
open cur_pembelian;
loop
fetch cur_pembelian into vRec;
exit when cur_pembelian%NOTFOUND or cur_pembelian%ROWCOUNT>5;
dbms_output.put_line(vRec.kode_barang || ' ' || vRec.kode_pembelian || ' ' || vR
ec.jumlah_pembelian);
end loop;
close cur_pembelian;
end;
/
Create or replace function rata_pembelian(kdbrg pembelian.kode_barang%TYPE)
return number IS
rata number;
begin
SELECT AVG(jumlah_pembelian) into rata
FROM pembelian
where kode_barang = kdbrg;
return rata;
end;
/
select kode_barang, rata_pembelian(kode_barang)
from pembelian;
create or replace procedure cari_nama_suplier is
vNamaSuplier suplier.nama_suplier%TYPE;
begin
select nama_suplier into vNamaSuplier from suplier
where kode_suplier = 'SPL01';
dbms_output.put_line('Nama Suplier :' || vNamaSuplier);
end;
/
exec cari_nama_suplier;
create or replace procedure
cari_nama_sup(vParam in out char) is
vHasil suplier.nama_suplier%TYPE;
begin
select nama_suplier into vHasil from suplier
where kode_suplier = vParam;
vParam := vHasil;
end;
/
declare
vlocal char(30) := 'SPL01';
begin
dbms_output.put_line('Nilai vlocal sebelum pemanggilan ' || 'prosedur : ' || vlo
cal);
cari_nama_sup(vlocal);
dbms_output.put_line('Nila vlocal setelah pemanggilan ' || 'prosedur : ' || vloc
al);
end;
/
declare
function JumlahOrder(kodecustomer in char) return long is
vHasil long;
begin
select count(*) into vHasil from pembelian
where kode_customer = kodecustomer;
return vHasil;
end;
begin dbms_output.put_line('Total order yang dilakukan ' || to_char(JumlahOrder(
'CST002')));
end;
/
select kode_barang as kd_brg, min(jumlah_pasok) as
minimum pasok
from pasok
group by kode_barang;
select kode_customer as kd_cus, max(jumlah_pembelian) as
max_pembelian
from pembelian
group by kode_customer;
select kode_suplier as kd_sup, trunc(avg(jumlah_pasok),0) as
rata2_pasok
from pasok
group by kode_suplier;
select kode_barang as kd_brg, sum(jumlah_pembelian) as
jmlh_pembelian
from pembelian
group by kode_barang;
select barang.nama_barang, pasok.tanggal_pasok, pasok.jumlah_pasok
from barang, pasok
where barang.kode_barang = pasok.kode_barang;
select tanggal_pembelian, jumlah_pembelian
from pembelian
where jumlah_pembelian > (select avg(jumlah_pembelian)
from pembelian);
select initcap(nama_customer), length(nama_customer)
from customer
where nama_customer like 'T%';

alter trigger
tr_checkNilaiPasok
disable;
alter table barang
disable all triggers;