|
|
After studying SQL, you can now fully appreciate the Logical Data Independence concept....
|
|
|
|
FName LName ProjectName #HoursWorked |
SELECT fname, lname, pname, hours
FROM employee, works_on, project
WHERE ssn = essn
AND pno = pnumber
Output:
FNAME LNAME PNAME HOURS
------ -------- --------------- ----------
John Smith ProductX 32.5
John Smith ProductY 7.5
Frank Wong ProductY 10
Frank Wong Reorganization 10
Frank Wong ProductZ 10
Frank Wong Computerization 10
Joyce English ProductX 20
Joyce English ProductY 20
John Doe ProductZ 40
James Borg Reorganization 0
Jack Wallace Reorganization 15
Jack Wallace Newbenefits 20
Jake Jones Computerization 35
Jake Jones Newbenefits 5
Alice Miller Computerization 10
Alice Miller Newbenefits 30
|
SELECT *
FROM ( SELECT fname, lname, pname, hours
FROM employee, works_on, project
WHERE ssn = essn
AND pno = pnumber
) EmpActivity
WHERE fname = 'John'
AND lname = 'Smith'
Output:
FNAME LNAME PNAME HOURS
------ -------- --------------- ----------
John Smith ProductX 32.5
John Smith ProductY 7.5
|
The temporal relation EmpActivity is created on the fly !!!!
Example:
CREATE VIEW EmpActivity
AS
( SELECT fname, lname, pname, hours
FROM employee, works_on, project
WHERE ssn = essn
AND pno = pnumber
)
|
|
Example: Find all activities of John Smith
SELECT *
FROM EmpActivity
WHERE fname = 'John'
AND lname = 'Smith'
FNAME LNAME PNAME HOURS
---------- ---------- --------------- ----------
John Smith ProductX 32.5
John Smith ProductY 7.5
|
CREATE VIEW Dept_Info(dname, no_emps, total_sal)
AS
( SELECT dname, count(ssn), sum(salary)
FROM department, employee
WHERE dnumber = dno
GROUP BY dname
)
|
| Find all department with >2 employees: |
Solution:
SELECT *
FROM Dept_Info
WHERE no_emps > 2
DNAME NO_EMPS TOTAL_SAL
--------------- ---------- ----------
Administration 3 93000
Research 4 133000
|
UPDATE employee SET salary = salary + 10000 SELECT * FROM Dept_Info WHERE no_emps > 2 DNAME NO_EMPS TOTAL_SAL --------------- ---------- ---------- Research 4 173000 Administration 3 123000 |
| Logical data independence is the property where the Users' applications that uses the database do not depend on the conceptual database schema |
|
Suppose we need to
change the conceptual schema
What do we need to do to continue to present the data in the SAME format as before ??? |
Answer:
|
change the SELECT query
used to
construct the view !!!
NO changes are needed to the users applications !!! |
Comment: use "cheung-sql" use companyDB |