10/3/2017
Chapter 6 The Relational Algebra and Calculus 10/3/2017 Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Recap of Relational Algebra Operations
Database State for COMPANY All examples discussed below refer to the COMPANY database shown here.
Q4: For every project located in ‘stafford’, list the project no, the controlling depart no, and the department manager’s last name, address and birth date. STAFFORD_PROJS Plocation=’stafford’ (project) CONTR_DEPT (STAFFORD_PROJS DNUM= DNUMBER DEPARTMENT) PROJ_DEPT_MGR (CONTR_DEPT MGR-SSN= SSN EMPLOYEE) RESULT PNUMBER,DNUM, LNAME, ADDRESS,BDATE (PROJ_DEPT_MGR)
Examples of in Relational Algebra Q5: Find the names of employees who work on all the projects controlled by department no 5. ADEPT5_PROJS(PNO) PNUMBER( DNUM=’5’ PROJECT) EMP_PROJ(SSN,PNO) ESSN,PNO(WORKS_ON) RESULT_EMP_SSNS (EMP_PROJ ÷ DEPT5_PROJS) RESULT LNAME, FNAME (RESULT_EMP_SSNS * EMPLOYEE)
Examples of in Relational Algebra Q7. List the names of all employee with two or more dependants Here we use the aggregate function count to count the number of employees to be more than 2 T1(ssn,no_of_dependant) ESSN ʒ Count dependant_name(DEPENDANT) T2σ NO_of_dependant >= 2 (T1) RESULTLNAME,FNAME (T2*EMPLOYEE)
Examples of in Relational Algebra Q8. List the names of managers who have at least one dependant MGRS(ssn) MGR_SSN(DEPARTMENT) EMPS_WITH_DEPS(SSN) ESSN(DEPENDANT) MGRS_WITH_DEPS (MGRS∩EMPS_WITH_DEPS) RESULTLNAME,FNAME (MGRS_WITH_DEPS*EMPLOYEE)
Examples of Queries in Relational Algebra STUDENT Course STUDENT Consider the relations student ,course, and studentCourse as follows: Course
Examples of Queries in Relational Algebra Q1. Get student names for the students who have completed course c1. Here we can make natural join because both tables have common attribute we need in join condition RESULT S_name(Student *C_course=“c1” StudentCourse) Q2. Get student names for the students who have studied by at least book named “introduction to compilers” RESULT s_name( s_code( c_code((σ c_book=‘introduction to compilers’(Course))* StudentCourse)) * Student)
Examples of Queries in Relational Algebra Q3. Get student names for the students who have completed all courses RESULT s_name(( s_code,c_code(StudentCourse)÷ C_code(course))*(student) Q4. Get student codes for the students who have completed at least all those courses completed by student s2 RESULT s_code( s_code,c_code(studentCourse)÷ c_code(σ s_code=‘s2’(studentCourse)))
Examples of Queries in Relational Algebra Q5. Get student names for the students who have not completed course c2 Here we firstly get students who have completed the course c2 and make the difference between the result and all student in student realtion RESULT s_name(( s_code (Student) – s_code (σ c_code=‘c2’(StudentCourse)))* Student) Q6. get all pairs of student names such that the two students are in the same year. Here we take acopy from table student and rename it as stud1 And then we make join between both relaions RESULT stud1.s_name,stud1.s_code( σ((stud1.s_Code=student.s_code)&(stud1.s_year=student.s_year)) (ρ stud1(student)) * (student))