Relational Model COP 4720 Lecture 9 Lecture Notes
Lecture 9COP Outline of Lecture Relational Algebra Queries Sec. 3.2
Lecture 9COP Summary of Relational Algebra (1) E ::= R | SELECT C (R) or c (R) | PROJECT A 1,A 2,...,A n (R) or A 1,A 2,...,A n (R) | E1 X E2 | E1 U E2 | E1 - E2 | RENAME R(A 1,A 2,...,A m ) (E) or R(A 1,A 2,...,A m ) (E)
Lecture 9COP Summary of Relational Algebra (2) E ::= R | E1 JOIN E2 or E1 E2 | E1 JOIN C E2 or E1 C E2 | E1 INTERSECT E2 or E1 E2
Lecture 9COP Sample Schema for Exercises Student(ID, name, address, GPA, SAT) Campus(location, enrollment, rank) Apply(ID, location, date, major, decision)
Lecture 9COP Sample Queries PROJECT date,decision (Apply) Campus X Apply
Lecture 9COP Sample Queries Find Names and addresses of all students with GPA > 3.7 who applied to CS major and were rejected. List name and address of all students who didn’t apply anywhere. name, address ((Students) Students.ID= Not_Apply.ID ( Not_Apply ( ID (Students) - ID (Apply)))) name, address ( GPA>3.7 decision=‘No’ major=‘CS’ (Student Student.ID=Apply.ID Apply))
Lecture 9COP Schema for examples Employee(fname,lname,ssn,bdate,address,sex,salary,superssn,dno) Department(dname, dnumber,mgrssn,mgrstartdate) Dept_Locations(dnumber, dlocation) Project(pname,pnumber,plocation,dnum) Works_on(essn,pno,hours) Dependent(essn,dependent_name,sex,bdate,relationship)
Lecture 9COP Examples a)Retrieve the name and address of all employees who work for the ‘Research’ department. research dname = ‘Research’ (Department) research_emps research dnumber = dno (Employee) Result fname,lname,address (research_emps) or research Select dname = ‘Research’ (Department) research_emps research join dnumber=dno Employee result project fname,lname,address (research_emps)
Lecture 9COP Examples (refer to schema on previous slide) b) Retrieve the names of all employees in dept. 5 who work more than 10 hrs./week on project X. Option 1: Fname,Lname (Employee SSN=ESSN ( Pname=‘Project X’ hours>10 Dnum=5 (Project Pnumber=Pno Works_On) ) ) Option 2: Emp_Work_X Pname=‘Project X’ (Project Pnumber=Pno Works_On) Emp-Work_10 Employee SSN=ESSN ( hours>10 (Emp_Work_X) ) Result Fname,Lname ( Dnum=5 (Emp_Work_10) )
Lecture 9COP c) List names of employees who have a dependent with the same first name. Same (Employee) Fname=Dependent_Name (Dependent) Result Fname,Lname (Same) ???????
Lecture 9COP c) List names of employees who have a dependent with the same first name. Same (Employee) Fname=Dependent_Name ssn=essn (Dependent) Result Fname,Lname (Same)
Lecture 9COP d) Retrieve the names of employees who work on every project. Emp_proj Project essn,pno ( Works_on ) T1 Project essn (Emp_proj) T2 Project pno (Works_on) T3 T2 x T1 T4 T3 – Emp_proj T5 Project essn T4 Ans T1 – T5