Exercise 2 Relational Calculus Database System-dww.

13
Exercise 2 Relational Calculus Database System-dww

Transcript of Exercise 2 Relational Calculus Database System-dww.

Page 1: Exercise 2 Relational Calculus Database System-dww.

Exercise 2Relational Calculus

Database System-dww

Page 2: Exercise 2 Relational Calculus Database System-dww.

Database System-dww

6 Relations of Company

Page 3: Exercise 2 Relational Calculus Database System-dww.

Find the first and last names of all employees whose salary is above $50,000

Database System-dww

Rel Algebra FNAME,LNAME(SALARY>‘50000’ (EMPLOYEES))

Tuple Rel Calculus t.FNAME,t.LNAME|EMPLOYEE(x) AND t.SALARY>‘50000’))

SQL select FNAME, LNAMEfrom EMPLOYEESwhere SALARY>‘50000’

Domain Rel Calculus ac|(Ǝi) EMPLOYEE(abcdefghijkl) AND i>‘50000’))

Page 4: Exercise 2 Relational Calculus Database System-dww.

Database System-dww

Exercises

(a) Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project.(b) List the names of employees who have a dependent with the same first name as themselves.(c) Find the names of employees that are directly supervised by 'Franklin Wong'. (e) Retrieve the names of employees who work on every project.

Page 5: Exercise 2 Relational Calculus Database System-dww.

Database System-dww

Exercises(f) Retrieve the names of employees who do not work on any project.(i) Find the names and addresses of employees who work on at least one project located in Houston but whose department has no location in Houston.(j)List the last names of department managers who have no dependents.

Page 6: Exercise 2 Relational Calculus Database System-dww.

Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project

Graphical approach can be used to help the logical and prosedural approach

Example of above query, the graphical approach: clue from query the relations that (may) work on the query are in the red text

Database System-dww

P

ProductX

Pname=‘ProductX’

W E

> 10

Hours > 10

Pnumber=Pno

5

Dno = 5

ESSN=SSN Fname, Lname (?)

WORKS_ON_ProductX (Rel.A) (PNAME=‘ProductX’(PROJECT) PNUMBER=PNO(WORKS_ON))

EMPLOYEE_DEPT5_WORKS_MORE_10_onProductX (Rel.B) ((EMPLOYEE) SSN=ESSN(HOURS>10(Rel.A)))

RESULT FNAME,LNAME(DNO=‘5’ (Rel.B))

Page 7: Exercise 2 Relational Calculus Database System-dww.

Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project

Database System-dww

P

ProductX

Pname=‘ProductX’

W E

> 10

Hours > 10

Pnumber=Pno

5

Dno = 5

ESSN=SSN Fname, Lname (?)

{ e.LNAME, e.FNAME | EMPLOYEE(e) AND e.DNO=5 AND (Ǝp)(Ǝw)(WORKS_ON(w) AND PROJECT(p) AND e.SSN=w.ESSN ANDw.PNO=p.PNUMBER AND p.PNAME='ProductX' AND w.HOURS>10 ) }

{ qs | EMPLOYEE(qrstuvwxyz) AND z=5 AND (Ǝa) (Ǝb) (Ǝe)(Ǝf) (Ǝg) ( WORKS_ON(efg) AND PROJECT(abcd) AND t=e AND f=b AND a='ProductX' AND g>10 ) }

Page 8: Exercise 2 Relational Calculus Database System-dww.

List the names of employees who have a dependent with the same first name as themselves.

Database System-dww

Dp EDepend_name=Fname Fname, Lname (?)

ESSN=SSN

RESULT FNAME,LNAME(DEPENDENT (ESSN,DEPEND_NAME)=(SSN,FNAME)(EMPLOYEE))

{ e.LNAME, e.FNAME | EMPLOYEE(e) AND (Ǝd) ( DEPENDENT(d) AND e.SSN=d.ESSN AND e.FNAME=d.DEPENDENT_NAME ) }

{ qs | (Ǝt) (Ǝa) (Ǝb) ( EMPLOYEE(qrstuvwxyz) ANDDEPENDENT(abcde) AND a=t AND b=q ) }

Page 9: Exercise 2 Relational Calculus Database System-dww.

Find the names of employees that are directly supervised by 'Franklin Wong'.

Database System-dww

E_SSN_FRANKLIN_WONG(Rel.A) (SSN(FNAME=‘FRANKLIN’ LNAME=‘WONG’(EMPLOYEE)))

E_SUPERVISED_BY_FRANKLIN_WONG (Result) (FNAME, LNAMES(EMPLOYEE) SUPERSSN=SSN(Rel.A))

{ e.LNAME, e.FNAME | EMPLOYEE(e) AND (Ǝs) ( EMPLOYEE(s) ANDs.FNAME='Franklin' AND s.LNAME='Wong' AND e.SUPERSSN=s.SSN ) }

{ qs | (Ǝy) (Ǝa) (Ǝc) (Ǝd) ( EMPLOYEE(qrstuvwxyz) AND EMPLOYEE(abcdefghij) AND a='Franklin' AND c='Wong' AND y=d ) }

Page 10: Exercise 2 Relational Calculus Database System-dww.

Retrieve the names of employees who work on every project

Database System-dww

ALL_EMPLOYEE(SSN, PNO)(Rel.A) (SSN, PNO(WORKS_ON))

ALL_PROJECT(PNO)(Rel.B) (PNO(PROJECT))

E_ALL_PROJECT (Rel.A÷Rel.B)

RESULT (FNAME, LNAME (E_ALL_PROJECT * EMPLOYEE))

{ e.LNAME, e.FNAME | EMPLOYEE(e) AND (Ѵp) ( NOT(PROJECT(p)) OR (Ǝw) (WORKS_ON(w) AND p.PNUMBER=w.PNO AND w.ESSN=e.SSN ) ) }

{ qs | (Ǝt) ( EMPLOYEE(qrstuvwxyz) AND (Ѵb) (NOT(PROJECT(abcd)) OR (Ǝe) (Ǝf) (WORKS_ON(efg) AND e=t AND f=b) ) }

Page 11: Exercise 2 Relational Calculus Database System-dww.

Retrieve the names of employees who do not work on any project

Database System-dww

ALL_EMPLOYEE(SSN)(Rel.A) (SSN(EMPLOYEE))

ALL_EMPLOYEE_WORKING(SSN)(Rel.B) (ESSN(WORKS_ON))

E_NOT_WORKING (Rel.A - Rel.B)

RESULT (FNAME, LNAME ((EMPLOYEE) SSN=ESSN(E_NOT_WORKING))

{ e.LNAME, e.FNAME | EMPLOYEE(e) AND NOT(Ǝw) ( WORKS_ON(w)AND w.ESSN=e.SSN ) }

{ qs | (Ǝt) ( EMPLOYEE(qrstuvwxyz) AND NOT(Ǝa) (WORKS_ON(abc) AND a=t ))}

Page 12: Exercise 2 Relational Calculus Database System-dww.

Find the names and addresses of employees who work on at least one project located in Houston but whose department has no location in Houston

Database System-dww

{ e.LNAME, e.FNAME, e.ADDRESS | EMPLOYEE(e) AND (Ǝp) (Ǝw) (WORKS_ON(w) AND PROJECT(p) AND e.SSN=w.ESSN ANDw.PNO=p.PNUMBER AND p.PLOCATION='Houston' AND NOT(Ǝl) ( DEPT_LOCATIONS(l) ANDe.DNO=l.DNUMBER AND l.DLOCATION='Houston' ) ) }

{ qsv | (Ǝt) (Ǝz) ( EMPLOYEE(qrstuvwxyz) AND (Ǝb) (Ǝc) (Ǝe) (Ǝf) ( WORKS_ON(efg) AND PROJECT(abcd) AND t=e AND f=b AND c='Houston' ANDNOT(Ǝh) NOT(Ǝi) ( DEPT_LOCATIONS(hi) AND z=h AND i='Houston’ ) ) }

Page 13: Exercise 2 Relational Calculus Database System-dww.

List the last names of department managers who have no dependents

Database System-dww

D_MNG_SSN(Rel.A)(SSN(DEPARTMENT) E_WITH_DEPENDENT(Rel.A)(SSN(DPENDENT)

MNGR_NO_DEPENDENT(Rel.A – Rel.B) RESULT(LNAME(EMPLOYEE * MNGR_NO_DEPENDENT))

{ e.LNAME | EMPLOYEE(e) AND (Ǝd) ( DEPARTMENT(d) ANDe.SSN=d.MGRSSN ANDNOT(Ǝx) (DEPENDENT(x) AND e.SSN=x.ESSN) ) }

{ s | (Ǝt) ( EMPLOYEE(qrstuvwxyz) AND (Ǝc) ( DEPARTMENT(abcd)AND t=c AND NOT(Ǝe) (DEPENDENT(efghi) AND e=t) ) }