• No results found

Lecture 11 Revision. Dr. Osama Al-Haj Hassan. Mapping Example Relational Algebra & SQL Examples Find candidate Keys BCNF Test

N/A
N/A
Protected

Academic year: 2021

Share "Lecture 11 Revision. Dr. Osama Al-Haj Hassan. Mapping Example Relational Algebra & SQL Examples Find candidate Keys BCNF Test"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)

Lecture 11 Revision

• Mapping Example

• Relational Algebra & SQL Examples

Dr. Osama Al-Haj Hassan

• Relational Algebra & SQL Examples

• Find candidate Keys

• BCNF Test

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

(2)

Mapping Example

E1 1 R1 M E2 1 R2 1 E3

t1

t3 t4 t5 t6 t7

t2

tt t14 t15

E4 R3 E5 M R4 N

t8 t9 t10 t11

t13 t12

(3)

Solution

t1 t2

E1

t4 t1

E2

t5

t6 E3

t8 t9

E4

t14 t6 t15

t8 t9

t10 t11

E5

t10 t6

R4

t12

t1 t3

T3-R

t1 t8

R3

t10 t13

3

(4)

Company DB

(5)

Relational Algebra and SQL Examples

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

work for department number 1.

5

(6)

Solution

σ Employee

ππ

ππ(σ Employeesalary>400 and dno=1 )

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

ππ ππ(

Ssn,name

)

(7)

Relational Algebra and SQL Examples

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

for department “QA”.

7

(8)

Solution

Employee Department dno = dnumber

π π π

π(

σσ(Salary>400 and dname=‘QA’σσ )

))))

Select ssn,name from employee,department where dno = dnumber and

salary>400 and dname=‘QA’;

π π π π(

Ssn,name

Salary>400 and dname=‘QA’

))))

(9)

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.

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

9

(10)

Solution

essn

COUNT pno , SUM hours (works_on)

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

essn

COUNT pno , SUM hours (works_on)

(11)

Relational Algebra and SQL Examples

• Find ssn and name of employee and the number of projects

• 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

(12)

Solution

essn

COUNT pno , SUM hours (works_on) πemployee

ssn,name) ( )

(

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

group by essn;

essn

COUNT pno , SUM hours (works_on)

ssn,name) ( )

( ssn=essn

(13)

Relational Algebra and SQL Examples

• Find ssn and number of projects each employee is working on in each department

in each department

13

(14)

Solution

Works_on project pno = pnumber

(

ππππ(Essn,pno,dnum )

))))

Essn,dno COUNT pno

Select essn,dno,count(*) from works_on,project Where pno = pnumber

group by essn,dno;

(15)

Relational Algebra and SQL Examples

• Find ssn for employees who work on all projects

15

(16)

Solution

π π π

π works_on

essn,pno )

(pno) π π π

π project pnumber

÷ ρ(

Select essn , count(*) from works_on Group by essn

Having count(*)= (Select count(*) from project);

(17)

Relational Algebra and SQL Examples

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

17

(18)

Solution

)

ππ

ππ works_on

- ρ( essn

)

ππ

ππ employee

( ssn

Select ssn from employee where

Ssn not in (select essn from works_on);

)

(ssn)essn

- ρ(

ssn ) (

(19)

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 to department 1 and they work in projects that belong to department 2

19

(20)

Solution

Works_on project pnum = pnumber

π π π π(

essn

σ ) σ σ σ

dnum=1(

)))) π π π π(

Works_onpnum = pnumberproject

essn

σ ) σσ σ

dnum=2(

))))

(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);

(21)

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 belong to department 1 or they work in projects that belong to department 2 or both.

21

(22)

Solution

Works_on project pnum = pnumber

π π π π(

essn

σ ) σ σ σ

dnum=1(

)))) π π π π(

Works_onpnum = pnumberproject

essn

σ ) σσ σ

dnum=2(

))))

(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);

(23)

Find Candidate Keys of length 1 or 2 for the following Relation

R = {A, B, C, D, E}

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

Since C is part of the candidate keys, you do not have to try A+,B+,D+, and E+ Since C does not appear on the right hand side of any FD, you know C is part of candidate keys

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

(24)

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)

(25)

BCNF Test

R = A,B,C,D,E

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

R1 = A,B,D

F={AB --> D, D --> B}

R2 = A,B,C,E

F={C --> E, E --> A}

Keys: CB,CD

Key: AB Key: CB

Note: You always compute keys using attribute closure

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

R3 = E,A F={E --> A}

R4 = B,C,E F={C --> E}

Key: AB

Key: CB Key: E

R5 =C,E F={C --> E}

R6 = C,B

Key: CB Key: C

• This decomposition results in R1,R3,R5,R6

• It is dependency

preserving because No FD is lost during decomposition

25

References

Related documents

For example, SQL in case of relational database systems should be extended to be able to support spatial types and

The right to terminate a contract for breach of condition is modified in relation to non-consumer contracts for the sale of goods by the Sale and Supply of Goods Act 1994, s 4(1),

If 305 grams of silver nitrate are reacted in an excess of magnesium chloride producing 23.7 grams of magnesium nitrate, what is the percent

 Most of the data required for data warehouse analysis comes from multiple databases and these analysis are recurrent and predictable to be able to design specific software to

◼ For each weak entity type W in the ER schema with owner entity type E, create a relation R & include all simple attributes (or.. simple components of composite attributes) of

Copyright © 2011 Ramez Elmasri and Shamkant Navathe. Two-Tier Client/Server

 Raid level 3 (single parity disks relying on the disk controller to figure out which disk has failed) and level 5 (block-level data striping) are preferred for Large volume

  Nested queries, joined tables, outer joins, aggregate functions, and grouping... Copyright © 2011 Ramez Elmasri and