Upload
others
View
37
Download
0
Embed Size (px)
Citation preview
CIS611 Lab Assignment 1 Part 4: Solution SS Chung
4. Update the following new changes into the database:
1) Joyce English with Ssn = 453453453 got married with John.
2) Jenifer Wallace with Ssn = 987654321 just had a new daughter named Erica.
3) Jenifer Wallace with Ssn = 987654321 is just assigned to a new project number ‘10’ to work
on.
Add these new entries into Dependent, Works_On tables in your database then Select * from
Dependent and Select * from Works_On to show the updated table contents.
5. Write SQL Select statements to retrieve data in the followings:
Q1:
Make a list of all project numbers that Research department employees are working on,
either as a worker or as a manager of the department that controls the projects.
(SELECT W.Pno FROM DEPARTMENT D, WORKS_ON W
WHERE D.Dname = 'research' and w.essn = D.mgrssn)
UNION /*as employee */
(SELECT W.Pno
FROM WORKS_ON W, department D, EMPLOYEE E
WHERE E.Dno = D.Dnumber AND
W.Essn = E.Ssn AND D.Dname = 'research');
/*Left Q: for projects as a manager of Research dept -- It returns 2,3,10,20*/ SELECT Distinct W.Pno
FROM DEPARTMENT D, WORKS_ON W
WHERE D.Dname = 'research' and w.essn = D.mgrssn;
/*Right Q: for projects as employees in Research dept -- It returns 1,2,3,10,20*/
SELECT Distinct W.Pno FROM WORKS_ON W, department D,
EMPLOYEE E
WHERE E.Dno = D.Dnumber AND
W.Essn = E.Ssn AND D.Dname = 'research';
Q1 in Relational Algebra:
MGRSSN_RESEARCH_DEPT ← MGRSSN ( DNAME=' Research' (DEPARTMENT))
RESEARCH_MGR_PROJ ← PNO (WORKS_ON ESSN=MGRSSN MGRSSN_RESEARCH_DEPT)
DNUMBER_RESEARCH_DEPT ← DNUMBER ( DNAME=' Research' (DEPARTMENT))
SSN_EMP_RESEARCH_DEPT ← SSN (EMPLOYEE DNO=DNUMBER DNUMBER_RESEARCH_DEPT)
RESEARCH_EMP_PROJ ← PNO (WORKS_ON ESSN=SSN SSN_EMP_RESEARCH_DEPT)
RESULT ← (RESEARCH_MGR_PROJ ∪ RESEARCH_EMP_PROJ)
/*another version because of LeftQ for Projects as a manager of Research dept. It
returns 1,2,3,10,20 */
(SELECT P.Pnumber
FROM PROJECT P, DEPARTMENT D
WHERE P.Dnum = D.Dnumber AND
D.Dname = 'research')
UNION /*as employee */ (SELECT W.Pno
FROM WORKS_ON W, department D,
EMPLOYEE E WHERE E.Dno = D.Dnumber AND
W.Essn = E.Ssn AND
D.Dname = 'research');
/*LeftQ --for projects as a manager -- It returns 1,2,3 for research dept projects
*/
SELECT Distinct P.Pnumber
FROM PROJECT P, DEPARTMENT D
WHERE P.Dnum = D.Dnumber AND
D.Dname = 'research';
/*Right Q: projects as Research department employees: 1,2,3,10,20*/
SELECT Distinct W.Pno FROM WORKS_ON W, department D,
EMPLOYEE E
WHERE E.Dno = D.Dnumber AND
W.Essn = E.Ssn AND
D.Dname = 'research';
DNUMBER_RESEARCH_DEPT ← DNUMBER ( DNAME=' Research' (DEPARTMENT))
RESEARCH_MGR_PROJ ← PNO (PROJECT DNUM=DNUMBER DNUMBER_RESEARCH_DEPT)
SSN_EMP_RESEARCH_DEPT ← SSN (EMPLOYEE DNO=DNUMBER DNUMBER_RESEARCH_DEPT)
RESEARCH_EMP_PROJ ← PNO (WORKS_ON ESSN=SSN SSN_EMP_RESEARCH_DEPT)
RESULT ← (RESEARCH_MGR_PROJ ∪ RESEARCH_EMP_PROJ)
Q2:
Get SSN and the last name of married female employees who work on three or more
projects --Q1
SELECT E.SSN, E.LNAME FROM EMPLOYEE E
WHERE E.Sex = ‘F’ and
E.SSN IN (SELECT Dp.ESSN FROM Dependent Dp
WHERE Dp.relationship = 'Spouse')
AND E.SSN IN (SELECT W.ESSN
FROM WORKS_ON W
GROUP BY W.ESSN
HAVING COUNT(W.Pno) >= 3);
-- OR Q2
SELECT E.SSN, E.LNAME
FROM EMPLOYEE E, dependent Dp
WHERE E.Sex = ‘F’ and E.ssn = Dp.essn and Dp.relationship = 'Spouse' AND E.SSN IN (SELECT W.ESSN
FROM WORKS_ON W
GROUP BY W.ESSN
HAVING COUNT(W.Pno) >= 3);
-- Q2
SELECT E.SSN, E.LNAME FROM EMPLOYEE E, Dependent Dp
WHERE E.Sex = ‘F’ and E.ssn = Dp.essn and Dp.relationship = ‘spouse’ AND
(SELECT COUNT(*) FROM WORKS_ON W
WHERE E.SSN = W.ESSN ) >= 3;
-- OR
-- Q3
SELECT E.SSN, E.LNAME FROM EMPLOYEE E, Dependent Dp,
(SELECT ESSN
FROM WORKS_ON
GROUP BY ESSN
HAVING COUNT(PNO) >= 3) AS THREEMOREPROJECT(ESSN) WHERE E.Sex = ‘F’ and E.ssn = Dp.essn and Dp.relationship = 'Spouse' AND
E.SSN = THREEMOREPROJECT.ESSN;
-- OR
-- Q4 select e.ssn, e.Lname
from EMPLOYEE e, Dependent Dp
where E.Sex = ‘F’ and E.ssn = Dp.essn and Dp.relationship = 'Spouse' AND e.Ssn IN (select wo.Essn
from WORKS_ON wo
group by wo.Essn having count(wo.Pno) >= 3);
Q2 in Relational Algebra:
SSN_Female_Emp � SSN ( ( SEX = ‘F’ (EMPLOYEE) ))
ESSN_Married_Emp � ESSN ( ( Relationship = ‘Spouse’ (DEPENDENT) ))
SSN_Married_Female_Emp � SSN (Female_Emp SSN=ESSN Married_Emp)
ESSN_3MoreProjects �( Count(pno) >= 3 (ESSN F COUNT(pno)WORKS_ON))
SSN_MarriedFemaleEmp_3MoreProject �
SSN(SSN_Married_Female_Emp SSN=ESSN ESSN_3MoreProjects)
Name_MarriedFemaleEmp_3MoreProject �
SSN,LNAME (EMPLOYEE * SSN_MarriedFemaleEmp_3MoreProject)
Q3. Write SQL using View instead of Subquery to express the query Q2.
Create View ThreeMOREPROJECT As
SELECT ESSN FROM WORKS_ON
GROUP BY ESSN
HAVING COUNT(PNO) >= 3;
Q3 Using View: SELECT E.SSN, E.LNAME
FROM EMPLOYEE E, ThreeMOREPROJECT
WHERE E.Sex = 'F' and E.SSN = ThreeMOREPROJECT.ESSN;
Or
Q3 Using View:
select e.ssn, e.Lname
from EMPLOYEE e, Dependent Dp where E.Sex = ‘F’ and E.ssn = Dp.essn and Dp.relationship = 'Spouse' AND
e.Ssn IN (select Essn
from ThreeMOREPROJECT);
Q4:
Get the Last and the First name of married employees who only have daughters.
(Hint: Indentify the following three sets in SQL first then write a main SQL using them.
Married = Select ESSN From Dependent Where relationship = ‘spouse’;
Girls = Select ESSN From Dependent Where relationship = ‘daughter’;
Boys = Select ESSN From Dependent Where relationship = ‘son’;
SELECT E.fname, E.lname FROM Employee E, Dependent D
WHERE D.essn = E.ssn AND D. relationship = 'spouse' AND
D.essn IN (SELECT ESSN FROM Dependent D2
WHERE D. essn =D2.essn and D2.relationship = 'daughter')
AND
D.essn NOT IN (SELECT D3.ESSN FROM Dependent D3
WHERE D. essn =D3.essn and D3.relationship = 'son');
OR
Q4:
Select E.fname, E.lname
From EMPLOYEE E, DEPENDENT D Where E.ssn = D.essn and D.relationship = 'spouse' AND
EXISTS (Select D2.ESSN
From Dependent D2 Where D. essn =D2.essn and relationship = 'daughter')
AND
NOT EXISTS (Select D3.ESSN
From Dependent D3 Where D. essn =D3.essn and D3.relationship = 'son');
Q4 in Relational Algebra:
MARRIED_EMP (ESSN) ← ESSN ((EMPLOYEE) SSN=ESSN Relationship=’Married’ (DEPENDENT))
MARRIED_W_DAUGHTER_EMP (ESSN) ←
ESSN (MARRIED_EMP ESSN=ESSN ( Relationship=’Daughter’(DEPENDENT)) )
MARRIED_W_SON_EMP (ESSN) ←
ESSN (MARRIED_EMP ESSN=ESSN ( Relationship=’Son’(DEPENDENT)) )
DAUGHTER_ONLY_EMP_SSNS ← (MARRIED_W_DAUGHTER_EMP - MARRIED_W_SON_EMP )
RESULT ← FNAME, LNAME (EMPLOYEE * DAUGHTER_ONLY_EMP_SSNS)) )
Q4 in Relational Tuple Calculus:
{e.LNAME, e.FNAME | EMPLOYEE (e) AND
(∃ (d)) (DEPENDENT(d) AND e.ssn = d.essn AND d.relationship = ‘spouse’ AND
(∃ (d1)) (DEPENDENT(d1) AND e..ssn = d1.essn AND d1.relationship = ‘daughter’ )AND
(NOT ∃ (d2)) (DEPENDENT(d2) AND e.ssn = d2.essn AND d2.relationship = ‘son’))}