1 - Ir. Hasanuddin Sirait, MT., MS

advertisement
TUGAS 1
MANAJEMEN BASIS DATA
Oleh :
SITI DARA FADILLA
P2700206007
SOAL
1. Explain the following terms briefly: attribute, domain, entity,
relationship,entity set, relationship set, one-to-many relationship, many-tomany relationship, participation constraint, overlap constraint, covering
constraint, weak entity set, aggregation, and role indicator.
2. Consider the following information about a university database:
 Professors have an SSN, a name, an age, a rank, and a research specialty.
 Projects have a project number, a sponsor name (e.g., NSF), a starting date,
an ending date, and a budget.
 Graduate students have an SSN, a name, an age, and a degree program
(e.g., M.S. or Ph.D.).
 Each project is managed by one professor (known as the project’s principal
investigator).
 Each project is worked on by one or more professors (known as the project’s
co-investigators).
 Professors can manage and/or work on multiple projects.
 Each project is worked on by one or more graduate students (known as the
project’s research assistants).
 When graduate students work on a project, a professor must supervise their
work on the project. Graduate students can work on multiple projects, in
which case they will have a (potentially different) supervisor for each one.
 Departments have a department number, a department name, and a main
office.
 Departments have a professor (known as the chairman) who runs the
department.
 Professors work in one or more departments, and for each department that
they work in, a time percentage is associated with their job.
 Graduate students have one major department in which they are working on
their degree.
 Each graduate student has another, more senior graduate student (known as
a student advisor) who advises him or her on what courses to take.
Design and draw an ER diagram that captures the information about the
university. Use only the basic ER model here; that is, entities, relationships,
and attributes. Be sure to indicate any key and participation constraints.
3. Consider the university database from Exercise 2 and the ER diagram you
designed. Write SQL statements to create the corresponding relations and
capture as many of the constraints as possible.
4. Consider the following schema:
Suppliers(sid: integer, sname: string, address: string)
Parts(pid: integer, pname: string, color: string)
Catalog(sid: integer, pid: integer, cost: real)
The key fields are underlined, and the domain of each field is listed after the field
name. Therefore sid is the key for Suppliers, pid is the key for Parts, and sid
and pid together form the key for Catalog. The Catalog relation lists the prices
charged for parts by Suppliers. Write the following queries in relational
algebra, tuple relational calculus, or domain relational calculus, and SQL:
1. Find the names of suppliers who supply some red part.
2. Find the sids of suppliers who supply some red or green part.
3. Find the sids of suppliers who supply some red part or are at 221 Packer
Street.
4. Find the sids of suppliers who supply some red part and some green part.
5. Find the sids of suppliers who supply every part.
6. Find the sids of suppliers who supply every red part.
7. Find the sids of suppliers who supply every red or green part.
8. Find the sids of suppliers who supply every red part or supply every green
part.
9. Find pairs of sids such that the supplier with the first sid charges more for
some part than the supplier with the second sid.
10. Find the pids of parts supplied by at least two different suppliers.
11. Find the pids of the most expensive parts supplied by suppliers named
Yosemite Sham.
Jawaban :
1. Pengertian Istilah – istilah berikut adalah :
a. Attribute (atribut) yaitu sebuah properti atau deskripsi untuk mewakili
suatu entitas, misalnya seorang mahasiswa dapat dilihat dari atributnya
seperti nama,nomor stambuk,alamat.
b. Domain yaitu sekumpulan nilai yang memungkinkan untuk atribut.
c. Entity (entitas) yaitu suatu objek dalam dunia nyata yang dapat dibedakan
dari objek lain, sebagai contoh pada bidang kesehatan entitasnya dapat
berupa pasien, dokter, obat, kamar.
d. Relationship yaitu hubungan (asosiasi) di antara dua atau lebih entitas
e. entity set yaitu sekumpulan entitas yang sama, misalnya kumpulan
mahasiswa teknik dalam jurusan teknik informatika.
f. Relationship set yaitu sekumpulan relationship yang sama.
g. One-to-many relationship, yaitu hubungan yang menunjukkan satu
entitas dapat diasosiasikan ke banyak entitas lain. Hubungan ini dilakukan
melalui sebuah field kunci. Sebagai contoh dalam sebuah perusahaan
dimana sebuah divisi dapat memiliki banyak karyawan.
h. Many-to-many relationship yaitu hubungan yang menunjukkan banyak
entitas dapat diasosiasikan ke banyak entitas lain. Hubungan ini dilakukan
melalui sebuah field kunci. Sebagai contoh hubungan beberapa orang
dapat memiliki hoby yang sama.
i. Participation constraint, menentukan apakah relasi antara dua atau
lebih entitas melibatkan entitas nyata. Sebagai contoh setiap divisi
memiliki sebuah entitas manager, participation constraint dapat berupa
total atau parsial, participation constraint total mengatakan setiap divisi
memiliki seorang manager, sedangkan participation constraint parsial
mengatakan setiap karyawan tidak semuanya dapat menjadi seorang
manager.
j. Overlap
constraint
terjadi
pada
hubungan
pewarisan
dari
superkelas(kelas induk) ke subkelas, overlap
constraint menentukan
apakah dua subkelas dapat memiliki entitas yang sama.
k. Covering constraint menentukan dimana entitas dalam subkelas berisi
seluruh entitas yang ada pada superkelas.
l. Weak entity set yaitu dimana entitas tidak dapat diidentifikasi sebagai
entitas unik
tanpa menganggap beberapa atribut kunci utama
diidentifikasi lain oleh pembuat entitas.
m. Aggregation yaitu keistimewaan dari model relasi entitas (entity
relationship) yang memungkinkan kumpulan relasi ikut serta di dalam
kumpulan relasi yang lain.
n. Role indicator. Jika kumpulan entitas digunakan dengan lebih dari satu
tugas , maka role indicator menguraikan bagaimana perbedaan pemakaian
relasi.
2. Bentuk Diagram ER
age
rank
spesiality
pid
sponsor
worked
ssn
startdate
Profesor
Project
manager
enddate
budget
supervises
Work_dep
ssn
Work project
Runs
senior
Graduate
Student
advisor
major
Time
ssngrad
program
departement
name
age
depname
depnumber
mainoffice
3. SQL Statement
Buat Tabel Professor
CREATE TABLE Professor (
ssnprof CHAR(9)
name CHAR(50)
age INTEGER
rank INTEGER
speciality CHAR(70)
PRIMARY KEY (ssnprof));
Buat Tabel Departement
CREATE TABLE Departement (depnumber INTEGER
depname CHAR(50)
mainoffice CHAR(30)
PRIMARY KEY (dnumber));
Buat Tabel Runs
CREATE TABLE Runs (
depnumber INTEGER
ssnprof CHAR(9)
PRIMARY KEY (depnumber, ssnprof),
FOREIGN KEY (ssnprof)REFERENCES Professor,
FOREIGN KEY (depnumber)REFERENCES Departement );
Buat Tabel Work_Dep
CREATE TABLE WorkDep (
depnumber INTEGER
ssnprof CHAR(9)
time INTEGER
PRIMARY KEY (depnumber,ssnprof),
FOREIGN KEY (ssnprof)REFERENCES Professor,
FOREIGN KEY (depnumber)REFERENCES Departement);
Buat Tabel Project
CREATE TABLE Project (
pid INTEGER
sponsor CHAR(50)
startdate DATE
enddate DATE
budget DOUBLE
PRIMARY KEY (pid));
Buat Tabel Graduate
CREATE TABLE Graduates (
ssngrad CHAR(9)
age INTEGER
name CHAR(50)
program CHAR(40));
4. Statement in Relational Algebra, Tuple Relational Calculus, or
domain relational calculus and SQL
1.Find the name s of suppliers who supply some red part.
Relational Algebra
π sname (π sid ((π pid δcolor=’red’ Parts )∞C a t a l o g )∞Suppliers )
SQL
SELECT S.sname FROM Suppliers S,Parts P,Catalog C WHERE P.color=’ red ’ AND
C.pid=P.pid AND C.sid=S.sid
2.Find the sid s of suppliers who supply some red or green part.
Relational Algebra
π sid (π pid ((π color=’red’ v color=’green’ Parts )∞C a t a l o g )
SQL
SELECT C.sid FROM Catalog C, Parts P WHERE (P.color =‘ red ’ OR P.color =‘
green’) AND P.pid =C.pid
3.Find the sid s of suppliers who supply some red part or are at 221 Packer
Street.
Relational Algebra
ρ(R 1, π sid ((π pid δcolor=’red’ Parts ) ∞C a t a l o g ))
SQL
SELECT S.sid FROM Suppliers S WHERE S.address =‘ 221 Packer street ’
OR S.sid IN (SELECT C.sid FROM Parts P,Catalog C WHERE P.color=’ red ’ AND P.pid
=C.pid )
4.Find the sid s of suppliers who supply some red part and some green part.
Relational Algebra
ρ(R1, π sid ((π pid δcolor=’red’ Parts ) ∞C a t a l o g ))
ρ(R2, π sid ((π pid δcolor=’green’ Parts ) ∞C a t a l o g ))
R1 ∩ R2
SQL
SELECT C.sid FROM Parts P,Catalog C WHERE P.color =‘ red ’ AND P.pid =C.pid
AND EXISTS (SELECT P2.pid FROM Parts P2,Catalog C2 WHERE P2.color =‘ green ’ AND
C2.sid =C.sid AND P2.pid =C2.pid )
5.Find the sid s of suppliers who supply every part.
Relational Algebra
(π sid,pid Catalog) / (π pid Parts )
SQL
SELECT C.sid FROM Catalog C WHERE NOT EXISTS (SELECT P.pid
FROM Parts P WHERE NOT EXISTS (SELECT C1.sid FROM Catalog C1 WHERE C1.sid =C.sid
AND C1.pid =P.pid))
6.Find the sid s of suppliers who supply every red part.
Relational Algebra
(π sid,pid Catalog) / (πpid δcolor=’red’ Parts)
SQL
SELECT C.sid FROM Catalog C WHERE NOT EXISTS (SELECT P.pid FROM Parts P
WHERE P.color =‘ red ’ AND (NOT EXISTS (SELECT C1.sid FROM Catalog C1
WHERE C1.sid=C.sid AND C1.pid =P.pid)))
7.Find the sid s of suppliers who supply every red or green part.
Relational Algebra
(π sid,pid Catalog) / (πpid δcolor=’red’ v color=’green’ Parts )
SQL
SELECT C.sid FROM Catalog C WHERE NOT EXISTS (SELECT P.pid
FROM Parts P WHERE (P.color =‘ red ’ OR P.color =‘ green ’ ) AND (NOT EXISTS (SELECT
C1.sid FROM Catalog C1 WHERE C1.sid=C.sid AND C1.pid =P.pid)))
8.Find the sid s of suppliers who supply every red part or supply every green
part.
Relational Algebra
ρ(R1, ((π sid,pidCatalog) / (π pid δcolor=’red’ Parts )))
ρ(R2, ((π sid,pidCatalog) / (π pid δcolor=’green’ Parts )))
R1UR2
SQL
SELECT C.sid FROM Catalog C WHERE (NOT EXISTS (SELECT P.pid
FROM Parts P WHERE P.color =‘ red ’ AND (NOT EXISTS (SELECT C1.sid
FROM Catalog C1 WHERE C1.sid =C.sid AND C1.pid =P.pid))))
OR (NOT EXISTS (SELECT P1.pid FROM Parts P1 WHERE P1.color =‘ green ’ AND
(NOT EXISTS (SELECT C2.sid FROM Catalog C2 WHERE C2.sid =C.sid AND
C2.pid =P1.pid))))
9.Find pairs of sid s such that the supplier with the . rst sid charges more for
some
part than the supplier with the second sid .
Relational Algebra
ρ(R1, Catalog)
ρ(R2, Catalog)
πR1.sid,R2.sid(δR1.pid=R2.pid٨R1.sid ≠R2.sid٨R1.cost>(R1xR2))
SQL
SELECT C1.sid,C2.sid FROM Catalog C1,Catalog C2 WHERE C1.pid =C2.pid AND
C1.sid <>C2.sid AND C1.cost > C2.cost
10.Find the pid s of parts supplied by at least two di . erent suppliers.
Relational Algebra
ρ(R1, Catalog)
ρ(R2, Catalog)
πR1.sidδR1.pid=R2.pid ٨R1.sid ≠ R2.sid (R1xR2))
SQL
SELECT C.pid FROM Catalog C WHERE EXISTS (SELECT C1.sid
FROM Catalog C1 WHERE C1.pid =C.pid AND C1.sid =C.sid )
11.Find the pid s of the most expensive parts supplied by suppliers named Yosemite
Sham.
Relational Algebra
ρ(R1, π sid δsname=’Yosemite Sham’Suppliers)
ρ(R2,R1 ∞ Catalog)
ρ(R3,R2)
ρ(R4(1→sid,2→pid,3→cost), δR3.cost<R2.cost(R3xR2))
πpid(R2→ π sid,pid,cost,R4)
SQL
SELECT C.pid FROM Catalog C,Suppliers S WHERE S.sname =‘ Yosemite Sham ’ AND
C.sid =S.sid AND C.cost ¡ÝALL (Select C2.cost FROM Catalog C2,Suppliers S2
WHERE S2.sname =‘ Yosemite Sham’ AND C2.sid =S2.sid)
Download