relational model cop 4720 lecture 9 lecture notes
TRANSCRIPT
Relational Model
COP 4720Lecture 9
Lecture Notes
Lecture 9COP 4720 2
Outline of Lecture
• Relational Algebra• Queries
• Sec. 3.2
Lecture 9COP 4720 3
Summary of Relational Algebra (1)
E ::= R
| SELECTC(R) or c(R)
| PROJECTA1,A2,...,An(R) or A1,A2,...,An
(R)
| E1 X E2
| E1 U E2
| E1 - E2
| RENAMER(A1,A2,...,Am)(E) or
R(A1,A2,...,Am)(E)
Lecture 9COP 4720 4
Summary of Relational Algebra (2)
E ::= R
| E1 JOIN E2 or E1 E2
| E1 JOINC E2 or E1 C E2
| E1 INTERSECT E2 or E1 E2
Lecture 9COP 4720 5
Sample Schema for Exercises
Student(ID, name, address, GPA, SAT)
Campus(location, enrollment, rank)
Apply(ID, location, date, major, decision)
Lecture 9COP 4720 6
Sample Queries
PROJECTdate,decision(Apply)
Campus X Apply
Lecture 9COP 4720 7
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 4720 8
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 4720 9
Examplesa) 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 Selectdname = ‘Research’(Department)
research_emps research joindnumber=dno Employeeresult projectfname,lname,address(research_emps)
Lecture 9COP 4720 10
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 4720 11
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 4720 12
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 4720 13
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 T1T4 T3 – Emp_projT5 Project essn T4Ans T1 – T5