Lecture 1 Relational Algebra and Relational Calculus.

Slides:



Advertisements
Similar presentations
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Advertisements

COMPANY schema EMPLOYEE
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
Chapter 6 Additional Relational Operations Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
The Relational Algebra
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
LAB 4 FIGURES.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5.
Chapter 4 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
CS 380 Introduction to Database Systems Chapter 7: The Relational Algebra and Relational Calculus.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
Retrieve the names of all employees in department 5 who work more than 10 hours per week on the ‘ProductX’ project. p10ssn ← (Π essn (σ hours > 10 (works-on.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
October 9, Announcements Exam Tuesday in class –Closed book Program 2 due Friday –Turn in using D2L dropbox –Only turn in the 4 files specified.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Slide 6- 1 CARTESIAN (or cross) Product Operation Defines a relation Q that is the concatenation of every tuple of relation R with every tuple of relation.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 11 A First Course in Database Systems.
7-1 Chapter 7 The Relational Data Model, Relational Constraints, and the Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 16 A First Course in Database Systems.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض.
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
Database Systems 主講人 : 陳建源 日期 :99/11/30 研究室 : 法 Chapter 6 The Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Lecture 8 Relational Algebra April 26, 2015.
10/3/2017.
The Relational Algebra and Calculus
The Relational Algebra
The Relational Algebra and Relational Calculus
The Relational Algebra and Relational Calculus
The Relational Algebra and Calculus
CS580 Advanced Database Topics
6/22/2018.
Chapter 6: Relational Algebra Fall 2015 Dr. Abdullah Almutairi
Chapter 4 The Relational Algebra and Calculus
CS4222 Principles of Database System
Company Requirements.
The Relational Algebra and Calculus
Session - 6 Sequence - 2 SQL: The Structured Query Language:
Introduction to Database Systems
The Relational Algebra and The Relational Calculus
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

Lecture 1 Relational Algebra and Relational Calculus

Unary Relational Operations ► SELECT Operation  To select the EMPLOYEE tuples whose department number is four  DNO = 4 (EMPLOYEE)  To select the EMPLOYEE tuples whose salary is greater than $30,000  SALARY > 30,000 (EMPLOYEE)

Unary Relational Operations ► PROJECT Operation  To list each employee’s first and last name and salary, the following is used   LNAME, FNAME,SALARY (EMPLOYEE)

Select employees in dep. 4 with salaries exceeding or in dep. 5 with salaries exceeding Show the full name and salary of every employee. Show the gender and salary of every employee. Select the gender and salary of all employees in dep. 4 with salaries exceeding or in dep. 5 with salaries exceeding 30000

Unary Relational Operations ► To retrieve the first name, last name, and salary of all employees who work in department 5, we must apply select and project operations.  We can write a single relational algebra expression as follows: ►  FNAME, LNAME, SALARY (  DNO=5 (EMPLOYEE))  OR We can explicitly show the sequence of operations, giving a name to each intermediate relation: ► TEMP   DNO=5 (EMPLOYEE) ► R   FNAME, LNAME, SALARY (TEMP)

Slide 6- 9

Operations From Set Theorys ► CARTESIAN (or cross product) Operation  Find the cross product between female employees (Fname, LName and SSN) and dependents ► FEMALE_EMPS   SEX=’F’ (EMPLOYEE) ► EMPNAMES   FNAME, LNAME,SSN (FEMALE_EMPS) ► EMP_DEPENDENTS  EMPNAMES x DEPENDENT

To get employees’ dependant s, SSNs must match The rest are spurious

► Retrieve the manager of each department  we need to combine each DEPARTMENT tuple with the EMPLOYEE tuple whose SSN value matches the MGRSSN value in the department tuple  We do this by using the join operation ► DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE Binary Relational Operations

► Get the locations of every department  DEPT_LOCS  DEPARTMENT DEPT_LOCATIONS DNUMBER=DNUMBER

Examples of Relational Algebra Queries Q1: Retrieve the names and addresses of all employees who work for the ‘Research’ department RESEARCH_DEPT   DNAME=‘Research’ (DEPARTMENT) RESEARCH_EMPS  (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE EMPLOYEE) RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS) Q2: For every project located in ‘Stafford’, list the project number and the controlling department name STAFFORD_PROJS   PLOCATION=‘STAFFORD’ (PROJECT) CONTR_DEPT  (STAFFORD_PROJS DNUM=DNUMBER DEPARTMENT) RESULT   PNUMBER, DNAME (RESEARCH_EMPS)

Examples of Relational Algebra Queries Q3: For every project located in ‘Stafford’, list the project number, the controlling department name 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 MGRSSN=SSN EMPLOYEE) RESULT   PNUMBER, DNAME,LNAME, ADDRESS, BDATE (RESEARCH_EMPS)

Examples of Relational Algebra Queries Q4: Find the names of employees who work on ALL projects controlled by department number 5 DEPT5_PROJS(PNO)   PNUMBER (  DNUM=5 (PROJECT)) EMP_PROJ(SSN, PNO)   ESSN, PNO (WORKS_ON) RESULT_EMP_SSNS  EMP_PROJ ÷ DEPT5_PROJS RESULT   FNAME, LNAME (RESULT_EMP_SSNS ESSN = SSN EMPLOYEE) Q5:Make a list of project numbers for projects that involve an employee whose last name is ‘Smith’ either as a worker or as a manager of the department that controls the project SMITHS   SSN (  LNAME=‘Smith’ (EMPLOYEE)) SMITH_WORKER_PROJ   PNO (WORKS_ON ESSN = SSN SMITHS) MGRS   LNAME, DNUMBER (EMPLOYEE SSN=MGRSSN DEPARTMENT) SMITH_MANAGED_DEPTS (DNUM)   DNUMBER (  LNAME=‘Smith’ (MGRS)) SMITH_MANAGED_PROJS (PNO)   PNUMBER (SMITH_MANAGED_DEPTS DNUMBER =DNUM PROJECT) RESULT  (SMITH_WORKER_PROJS  SMITH_MGR_PROJS)

► Use of the Functional operator ℱ (script f)  ℱ FUNCTION ATTRIBUTE (R) ► ℱ MAX Salary (Employee) retrieves the maximum salary value from Employee ► ℱ MIN Salary (Employee) retrieves the minimum Salary value from Employee ► ℱ SUM Salary (Employee) retrieves the sum of the Salary from Employee  Sometimes we want to get aggregate functions over groups of tuples (e.g. for all employees in every department separately) ► GROUPING_ATTRIBUTES ℱ FUNCTION ATTRIBUTE (R)  DNO ℱ COUNT SSN, AVERAGE Salary (Employee) groups employees by DNO (department number) and computes the count of employees and average salary per department Additional Relational Operations

R (DNO, NO_OF_EMPLOYEES, AVERAGE_SAL )  ( DNO ℱ COUNT SSN, AVERAGE Salary (Employee)) DNO ℱ COUNT SSN, AVERAGE Salary (Employee) If no names are specified for aggregates: FUNCTION_ATTRIBUTE ℱ COUNT SSN, AVERAGE Salary (Employee) Get the number of employees in the company and the average salary of an employee Get the number of employees and the average salary in each department Default Names

Examples of Relational Algebra Queries ► Q6: List the names of all employees with two or more dependents  T1(SSN, NO_OF_DEPTS)  ESSN ℱ Count DEPENDENT_NAME (DEPENDENT)  T2   NO_OF_DEPS >=2 (T1)  RESULT   LNAME, FNAME (T2 SSN=SSN EMPLOYEE) ► Q7: Retrieve the names of employees who have no dependents  ALL_EMPS   SSN (EMPLOYEE)  EMPS_WITH_DEPS ( SSN )   ESSN ( DEPENDENT )  EMPS_WITHOUT_DEPS  (ALL_EMPS - EMPS_WITH_DEPS)  RESULT   LNAME, FNAME (EMPS_WITHOUT_DEPS SSN=SSN EMPLOYEE)

EMPLOYEE SSN = MGRSNN DEPARTMENT

SSNSUPERSSN