Anda di halaman 1dari 25

Lecture 11

Revision
Mapping Example
Relational Algebra & SQL Examples
Find candidate Keys
BCNF Test

Dr. Osama Al-Haj Hassan

Reference to: Fundamentals of Database Systems , Ramez Elmasri and Shamkant B. Navathe, Fifth Edition. 1
Mapping Example
t1 t2

tt t3 t14 t4 t5 t15 t6 t7

1 M E2 1 1 E3
E1 R1 R2

t13 t12

M N
E4 R3 E5 R4

t8 t9 t10 t11

Map the above ER diagram into relational model 2


E1 Solution
t1 t2
E2
t4 t1 t5 t14 t6 t15
E3
t6
E4
t8 t9
E5
t10 t11
R4
t10 t6 t12
T3-R
t1 t3
R3
t1 t8 t10 t13
3
Company DB

4
Relational Algebra and SQL
Examples

Find ssn and name of employees with salary > 200 and they
work for department number 1.

5
Solution

( Employee
salary>400 and dno=1 )
Ssn,name

Select ssn,name from employee where salary>400 and dno=1;

6
Relational Algebra and SQL
Examples

Find ssn,name of employees with salary > 200 and they work
for department QA.

7
Solution

( (Employee
dno =
Department
dnumber ))
Salary>400 and dname=QA
Ssn,name

Select ssn,name from employee,department where


dno = dnumber and
salary>400 and dname=QA;

8
Relational Algebra and SQL
Examples

Find ssn of employee and the number of projects he is


working on and the total number of hours he is working.

9
Solution

essn COUNT pno , SUM hours (works_on)

Select essn,count(*), sum(hours) from works_on group by essn;

10
Relational Algebra and SQL
Examples

Find ssn and name of employee and the number of projects


he is working on and the total number of hours he is working.

11
Solution

ssn,name ) (
(employee )
essn COUNT pno , SUM hours (works_on)
ssn=essn

Select essn,count(*), sum(hours) from works_on,employee


Where ssn=essn
group by essn;

12
Relational Algebra and SQL
Examples

Find ssn and number of projects each employee is working on


in each department

13
Solution

( (Works_on
pno =
project
pnumber ) )
Essn,pno,dnum
Essn,dno COUNT pno

Select essn,dno,count(*) from works_on,project


Where pno = pnumber
group by essn,dno;

14
Relational Algebra and SQL
Examples

Find ssn for employees who work on all projects

15
Solution

works_on project
essn,pno ( pnumber )
(pno)

Select essn , count(*) from works_on


Group by essn
Having count(*)= (Select count(*) from project);

16
Relational Algebra and SQL
Examples

Find the SSN for employees who do not work on any project

17
Solution

(
employee
ssn )- ( works_on
essn )
(ssn)

Select ssn from employee where


Ssn not in (select essn from works_on);

18
Relational Algebra and SQL
Examples

Find the ssn for employees who work in projects that belong
to department 1 and they work in projects that belong to
department 2

19
Solution

( ( Works_on project
pnum = pnumber )) ( pnum = pnumber ) )
( Works_on project

dnum=1 dnum=2
essn essn

(Select ssn from works_on,project where


works_on.pno=project.pnumber and project.dnum=1)
intersect
(Select ssn from works_on,project where
works_on.pno=project.pnumber and project.dnum=2);

20
Relational Algebra and SQL
Examples

Find SSN for employees who either work in projects that


belong to department 1 or they work in projects that belong
to department 2 or both.

21
Solution

( ( Works_on project
pnum = pnumber )) ( pnum = pnumber ) )
( Works_on project

dnum=1 dnum=2
essn essn

(Select ssn from works_on,project where


works_on.pno=project.pnumber and project.dnum=1)
union
(Select ssn from works_on,project where
works_on.pno=project.pnumber and project.dnum=2);

22
Find Candidate Keys of length 1 or
2 for the following Relation
R = {A, B, C, D, E}
F = { AB --> D,
C --> E, Since C does not appear on the right hand side of
E --> A, any FD, you know C is part of candidate keys
D --> B }
Since C is part of the candidate keys, you do not have to try A +,B +,D +, and E +
C+ = {CEA}, so C is not a candidate key
CA+ = {CAE}, so CA is not a candidate key
CB+ = {CBEAD}, so CB is a candidate key
CD+ = {CDEAB}, so CD is a candidate key
CE+ = {CEA}, so CE is not a candidate key

So, our candidate keys of length 1 are: nothing


So, our candidate keys of length 2 are: CB,CD
23
BCNF Test
Is the previous relation in BCNF?

If not, then decompose it until each sub relation becomes in


BCNF.

R is not in BCNF (why?)


Because the following FDs violate BCNF constraints:
AB --> D, C --> E, E --> A, D --> B
The left hand side of each FD is not a super key (not CB nor CD)

24
BCNF Test
Note: You always compute keys using attribute closure

R = A,B,C,D,E Keys: CB,CD


F={AB --> D, C --> E, E --> A, D --> B}

R1 = A,B,D R2 = A,B,C,E
Key: AB Key: CB
F={AB --> D, D --> B} F={C --> E, E --> A}

Key: E R3 = E,A R4 = B,C,E


F={E --> A} F={C --> E} Key: CB
This decomposition
results in R1,R3,R5,R6
It is dependency
preserving because No FD
is lost during decomposition R5 =C,E R6 = C,B
F={C --> E} Key: CB
Key: C
25

Anda mungkin juga menyukai