You are on page 1of 6

TUGAS PRAKTIKUM BASIS DATA

Oleh : Cathrine Nicea Folamauk

PROGRAM PRA S2 ILMU KOMPUTER FAKULTAS MIPA-UGM 2013

Pertemuan 4 : VIEW dan TRIGGER Syarat : Menggunakan tabel employee_join dan tabel job

1. Buatlah view yang berisi name (gabungan first_name dan last_name), salary, city, dan job_description dari kedua tabel dimaksud. Penyelesaian : Sintaks : create view view_1 (name, salary, city, job_desc) as select concat (e.first_name, e.last_name) as name, e.salary, e.city, j.title as job_desc from employee_join e join job j on e.job_id=job_id;

2. Buatlah view untuk menampilkan job_description dan jumlah employee untuk masing-masing job. Penyelesaian : Sintaks : create view view_2(job_desc, emp_count) as select j.title as job_desc, count(e.id) as emp_count from employee_join e join job j on e.job_id=j.job_id group by e.job_id order by j.title;

3. Buatlah sebuah trigger untuk menyimpan data yang dihapus dalam tabel employee_join. Data yang dihapus tersebut disimpan dalam tabel baru bernama employee_bak. Penyelesaian : Membuat tabel employee_bak : Sintaks : create table employee_bak(id int(11) not null, first_name varchar(15), last_name varchar(15), start_date date, end_date date, salary float(8,2), city varchar(10), job_id int(11));

Membuat trigger untuk menyimpan data yang dihapus dari tabel employee_join ke tabel employee_bak Sintaks : create trigger employee_hapus after delete on employee_join for each row begin insert into employee_bak values (old.id, old.first_name, old.last_name, old.start_date, old.end_date, old.salary, old.city, ,old.job_id);

Pertemuan 4 : FUNCTION dan PROCEDURE Syarat : Menggunakan tabel employee_join dan tabel job

1. Buatlah fuction untuk menampilkan gabungan first_name dan last_name dengan bentuk last_name, first_name. Penyelesaian : Sintaks : Mysql > delimiter // Mysql> create function revName(new_first_name varchar(15), new_last_name varchar(15)) return varchar(35) begin return concat(new_last_name,,,new_first_name); Mysql> end //

Hasilnya :

2. Buatlah procedure untuk menampilkan job_description dari masukan sebuah id employee. Penyelesaian : Sintaks : Mysql > delimiter // Mysql> create procedure empjob(in idCek int) begin select first_name, last_name, title from employee_join, job where employee_join.job_id=job.job_id and id=idCek; Mysql> end //

You might also like