Relational Algebra. Manipulating Databases To access information in a database we use a query Ex: How many customers have the first name = `John’? Good.

Slides:



Advertisements
Similar presentations
Chapter 6 The Relational Algebra
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
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
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Greg Riccardi Florida State University. Using SQL to Manipulate Database Content and Structure How to create queries in SQL –Simple select statements.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
SQL SQL (Structured Query Language) is used to define, query, and modify relational databases Every relational database system understands SQL SQL is standard:
Your name here The lecture notes are based on using Microsoft Access interactively as part of the lecture.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
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.
Chapter 3 Section 3.4 Relational Database Operators
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
The Relational Algebra and Calculus
Relational Algebra. 2 Outline  Relational Algebra Unary Relational Operations Relational Algebra Operations from Set Theory Binary Relational Operations.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
1 Principles of Database Systems With Internet and Java Applications Today’s Topic Chapter 7: SQL, the Structured Query Language Instructor’s name and.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
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.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
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.
1 CSE 480: Database Systems Lecture 16: Relational Algebra.
April 20022CS3X1 Database Design Relational algebra John Wordsworth Department of Computer Science The University of Reading Room.
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Database Systems Chapter 6
The Relational Algebra and Relational Calculus
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
The Relational Algebra and Calculus
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
The Relational Algebra and Relational Calculus
Chapter 4 The Relational Algebra and Calculus
The Relational Algebra and Calculus
LECTURE 3: Relational Algebra
The Relational Algebra and Relational Calculus
376a. Database Design Dept. of Computer Science Vassar College
Presentation transcript:

Relational Algebra

Manipulating Databases To access information in a database we use a query Ex: How many customers have the first name = `John’? Good query writing follows a formal model called relational algebra

Relational Algebra Relational algebra: a collection of mathematical operations that manipulate tables Familiarity with relational algebra helps understanding the logic behind complex queries and ease the way for writing them

Tables and Queries Recall: a table is a set of rows/records having the same number and types of attributes When you send a query to the database, it Finds the appropriate rows of information in the stored tables Performs the requested operations on the data Represents the results in a new temporary table Delivers the table of results to the user

Example Ex: How many customers have the first name = `John’? The database creates a table containing all customers whose first name is `John’ and returns the table to the user

Basic types of queries There are 4 basic types of queries A projection operation produces a result table with Only some of the columns of its input table. A selection operation produces a result table with All of the columns of the input table Only those rows of its input table that satisfy some criteria. A join or product operation produces a result table by Combining the columns of two input tables. A set operation produces a result table by Combining rows from one or the other of its input tables

Projection operation:  A projection query selects some of the columns of the input table project T onto (attribute1, attribute2, …) Relational algebra form:  attribute1, attribute2,... (T)

Example  firstName,lastName (Customer)

Example … Notice that the result table has fewer rows Duplicate rows have been removed because the attributes do not contain a key

Storing the temporary results We can store the result of a query in a table T’ as follows: T’   attribute1, attribute2,... (T) This will create a table T’ with attributes: attribute1, attribute2, … containing the result of the query

Selection queries:  A selection query selects rows that match a selection criteria from a table Relational algebra form  (T) Each row is checked to see if it satisfies the condition and selected accordingly

Example  lastName=‘Doe’ (Customer)

Complex selection criteria The selection criterion can be any boolean expression containing operators like: and, or, =, ,, , , etc …

Example T   ssn=’ ’ and date < ’01-mar-2002’ (TimeCard)

Product queries: X The product query takes two tables and produce a table which is the cross product of the two, i.e., combines every row of one table with every row of other table R(A1, A2, …, An) × S(B1, B2, …, Bm)= Q(A1, A2, …, An, B1, B2, …, Bm) Relational algebra form: R  S

Example Employee  TimeCard

Product queries … If two attributes in two tables T and R have the same name, we prefix them with the relation name: T. Ex: Employee.ssn, TimeCard.ssn Remark. Many of the resulting rows in the previous example don’t make sense

Join queries: ⋈ In the previous table we are only interested in the rows that match: rows with Employee.ssn = TimeCard.ssn We are interested in the query:  Employee.ssn=TimeCrad.ssn (Employee x TimeCard )

Join operations … A join query is a cross product with a restriction on the result rows The join condition determines which rows match Only matching rows are in the result table Typical join condition is equality of attributes It is called equi-join Relational algebra form: R ⋈ S

Example Some rows from the table Employee ⋈ Employee.ssn=TimeCard.ssn TimeCard

Natural join: * Frequently, when doing an equi-join, the attributes have the same name A natural join is an equi-join with an equality condition on the common attributes: Employee ⋈ ssn TimeCard Employee * TimeCard In natural join the common attributes appear once

Queries with multiple joins Consider the Video-Rental schema, and suppose we want to retrieve for every currently-rented video, the renter’s account number, video number, rental date, due date, title of the movie, and cost

Solution  accountId, videoId, dateRented, dateDue, title, cost (( Rental ⋈ videoId Video) ⋈ movieId Movie)

Combining operations Suppose we want to find the following info. For customer with account = 113, find all the videos that he is renting: For each video, find the video number, the title of the movie, and the due date

Solution  videoId, title, dateDue ((  accountId=113 ( Rental ) ⋈ videoId Video) ⋈ movieId Movie) Or: T1 =  accountId=113 ( Rental ) T2=T1 ⋈ videoId Video T3 = T2 ⋈ movieId Movie T4 =  videoId, title, dateDue ( T3)

More examples List all comedy movies that were rented on December 21, For every movie list the customer’s name, movie title, and date returned

Solution T1 =  daterented=‘December ’ (Previous Rental ) T2=T1 ⋈ videoId Video T3 =  genre=‘comedy’ (Movie) T4 = T2 ⋈ movieId T3 T5 = T4 ⋈ accountId Customer T6 =  firstName, lastName, title, dateReturned ( T5)

Set operations Set operations include: Union, intersection, and difference Relational algebra form: , ,  Set operations can be applied to any tables with the same shape (compatible) The same order and type of attributes Attribute names do not have to agree

Set operations If R and S are two compatible tables: R  S is the table that contains the set of rows that are either in R or in S R  S is the table that contains the set of rows that are both in R and S R - S is the table that contains the set of rows that are in R but not in S

Example of  Retrieve all the videos that are currently or were previously rented EverRented= Rental  PreviousRental

Example of  Retrieve the video id of all the videos that are currently rented and have been rented at least once before Veterans=  videoId, ( Rental)   videoId, ( PreviousRental )

Example of - Retrieve the video id of all the videos that are currently rented and have never been rented before FirstTime=  videoId, ( Rental) -  videoId, ( PreviousRental )

Aggregate functions Not all queries can be expressed using the basic operations described previously. What if we want to compute the average salary of all employees?

Aggregate functions What if we want to count the number of employees in each department? For such queries, we use aggregate functions. Relational algebra form  (T)

Aggregate functions … The function list includes: average, sum, count, maximum, minimum The result of the query will be a table containing the results The attributes consist of the grouping attributes + function parameters

Examples Ex1: compute the average salary of all the employees  Average(salary) (Employee) The resulting table contains one attribute: Average_Salary and one value Ex2: compute the number of employees in each department DNO  Count(ssn) (Employee) The resulting table contains two attributes: DNO and Count_ssn. There is a row for every dept. containing the DNO value and the number of employees

Renaming attributes It is sometimes convenient to rename the attributes in the resulting relation: R( DEPTNUM, NUM_EMPL )  DNO  Count(ssn) (Employee)

Recursive operations Compute all the employees supervised by ``Pinochio’’ Compute all the emplyees supervised by ``Pinochio’’ at level two Compute all the employees supervisod by ``Pinochio’’ at any level!!!

Answers A1: Pinochio_ssn <-  ssn (  fname=`pinochio’ ( Employee )) Result1   ssn ( Pinochio ⋈ ssn=superssn Employee) A2: Result2   ssn ( Result1 ⋈ ssn=superssn Employee) Result  Result1  Result2 A3: is not supported by standard relational algebra

Outer Join Left Outer Join Ex: list the employee names and also the name of the department they manage in case it exists Right Outer Join Full Outer Join

Examples from (Emp-Dept-Proj schema) List everybody who makes more than $ List names of everybody working for the research department. List employees with a dependent. List employees that have a daughter. List employees without dependents. List employees working on a project in Houston. List all supervisors. List names of all managers. List names of managers with at least one dependent

Examples from (Emp-Dept-Proj schema …) For every project located in ‘Chicago’, list the project number, the controlling department number, the department manager’s last name, address, and birthdate. Make a list of project numbers for projects involving an employee whose first name is `Pinochio’ either as a worker on the project, or as a manager of the department that controls the project. Find the names of all employees who are directly supervised by `Isaac Newton’ For each department, retrieve the department name and average salary of its employees. Retrieve the average salary of all female employees For each project, list the project name and the total number of hours spent on the project.