Download presentation
Presentation is loading. Please wait.
Published byAlison Williamson Modified over 9 years ago
1
Relational Model COP 4720 Lecture 9 Lecture Notes
2
Lecture 9COP 47202 Outline of Lecture Relational Algebra Queries Sec. 3.2
3
Lecture 9COP 47203 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)
4
Lecture 9COP 47204 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
5
Lecture 9COP 47205 Sample Schema for Exercises Student(ID, name, address, GPA, SAT) Campus(location, enrollment, rank) Apply(ID, location, date, major, decision)
6
Lecture 9COP 47206 Sample Queries PROJECT date,decision (Apply) Campus X Apply
7
Lecture 9COP 47207 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))
8
Lecture 9COP 47208 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)
9
Lecture 9COP 47209 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)
10
Lecture 9COP 472010 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) )
11
Lecture 9COP 472011 c) List names of employees who have a dependent with the same first name. Same (Employee) Fname=Dependent_Name (Dependent) Result Fname,Lname (Same) ???????
12
Lecture 9COP 472012 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)
13
Lecture 9COP 472013 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.