Revision
Mapping Example
Relational Algebra & SQL Examples
Find candidate Keys
BCNF Test
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
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
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
8
Relational Algebra and SQL
Examples
9
Solution
10
Relational Algebra and SQL
Examples
11
Solution
ssn,name ) (
(employee )
essn COUNT pno , SUM hours (works_on)
ssn=essn
12
Relational Algebra and SQL
Examples
13
Solution
( (Works_on
pno =
project
pnumber ) )
Essn,pno,dnum
Essn,dno COUNT pno
14
Relational Algebra and SQL
Examples
15
Solution
works_on project
essn,pno ( pnumber )
(pno)
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)
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
20
Relational Algebra and SQL
Examples
21
Solution
( ( Works_on project
pnum = pnumber )) ( pnum = pnumber ) )
( Works_on project
dnum=1 dnum=2
essn essn
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
24
BCNF Test
Note: You always compute keys using attribute closure
R1 = A,B,D R2 = A,B,C,E
Key: AB Key: CB
F={AB --> D, D --> B} F={C --> E, E --> A}