Relational Algebra - Chapter 6.1-6.5 (7th ed. 8.1-8.5)

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)
Lecture 1 Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
The Relational Algebra
CM036: Advanced Database Lecture 3 Relational Algebra and SQL.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
- relation schema, relations - database schema, database state
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.
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.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
CS609 Introduction. Databases Current state? Future?
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
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.
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) 
The Relational Algebra and Calculus
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.
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.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
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.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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.
Query Processing – Implementing Set Operations and Joins Chap. 19.
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.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
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.
Database Systems Chapter 6
The Relational Algebra and Relational Calculus
Chapter (6) The Relational Algebra and Relational Calculus Objectives
COMP3017 Advanced Databases
CS580 Advanced Database Topics
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
6/22/2018.
The Relational Algebra and Relational Calculus
Retrieval Queries in SQL(DML)
Chapter 6: Relational Algebra Fall 2015 Dr. Abdullah Almutairi
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Chapter 4 The Relational Algebra and Calculus
CS4222 Principles of Database System
Outline: Relational Data Model
The Relational Algebra and Calculus
Lecture 3 Relational Algebra and SQL
Presentation transcript:

Relational Algebra - Chapter (7th ed )

Relational Algebra Theoretical basis for SQL (E.F. Codd) Relational algebra (algebraic notation) and relational calculus (logical notation) Created to demonstrate the potential for a query language of the relational model Algebra and calculus are equivalent in expressive power Can represent complex queries compactly, but too mathematical for the average person

What is an “Algebra”? Mathematical system consisting of: – Operands --- variables or values from which new values can be constructed. – Operators --- symbols denoting procedures that construct new values from given values.

What does it do? Provide DML and DDL In relational algebra, a series of operations are combined to form a relational algebra expression (query)

Set Theoretic Operations

Set theoretic operations Union, Intersection, Difference - Binary – Applied to 2 sets (relations) - no duplicates in result - mathematical set – Must be same type of tuples - Compatibility same degree n dom(Ai) = dom(Bi) – Fig – Resulting relation - same attribute names as first relation – Which operations are: Commutative ? – R U S = S U R, R ∩ S = S ∩ R – R - S = S - R ? Associative ? – R U (S U T) = (R U S) U T, R ∩ (S ∩ T) = (R ∩ S) ∩ T – (R - S) – T ?

Cartesian Product X - Binary Also binary, but does not require union compatibility – R(A1, A2, … An) X S(B1, B2, …, Bm) Creates a tuple with the combined attributes of 2 tables – Q(A1, A2, …, An, B1, B2, …, Bm) Fig Degree of resulting relation? – n+m

Relational Algebra Operations

Select Operation   - unary operation (Where in SQL) A subset of tuples satisfying a selection condition Selects rows Equivalent to select condition in WHERE clause  ( )  dno=4 (Employee)  salary>30000 (Employee)

Select Operation Select condition is a Boolean expression comparison op - =, <, <=, etc. You can use boolean conditions to connect clauses Can combine cascade of selects into single select with ANDs  (dno=4 and salary>25000) or (dno=5 and salary>30000) (Employee) Fig

Select Operation σc1 (σc2(R)) = σc2 (σc1(R))

Project Operation  or   unary operation Equivalent to the SELECT clause in SQL(Select) Keeps only certain attributes (columns) from a relation Selects columns Form of operation:  ( )  fname, lname, salary Employee Fig Resulting relation has only those attributes specified Degree of relation ? – attributes in attr_list

Project Operation The project operation eliminates duplicate tuples in the resulting relation so that it remains a mathematical set  sex, salary Employee Fig If several male employees have salary $30,000 only single tuple is kept in the resulting relation. Is the project operation commutative? – No, why?

Sequences of operations Several relation algebra operations can be combined to form a relational algebra expression (query). Retrieve the names and salaries of employees who work in department 5. Q ←  fname, lname, salary (  dno=5 Employee) Alternately, explicit intermediate relations can be specified for each step: Dept5 ←  dno=5 Employee R ←  fname,lname,salary Dept5

Write the following in Relational Algebra Select * from Employee Select bdate from Employee Select * from Employee where sex='F'

Write the following in Relational Algebra List SSN of employees who do not have dependents (Select ssn from employee) minus (Select essn from dependent)

Renaming Attributes can optionally be renamed in the resulting relation: Dept5   dno=5 Employee T(firstname,lastname,salary)   fname,lname,salary Dept5 Fig Alternative notation in textbook, can rename attributes and/or table  R(firstname, lastname, salary)  fname,lname,salary Dept5

Operations DML Operations: – set theory operations Union Intersection Difference – relational DB operations Select Project Anything else? – Now write SSN, lname of employees who do not have dependents Join

The Join operation |X| Similar to a Cartesian Product followed by a select Form of operation: R |X| S Result is: Q (A 1, A 2, …, A n, B 1, B 2, …, B m ) A 1, A 2 … are the attributes of R B 1, B 2,.. are the attributes of S For all tuples that satisfy the join condition join condition: and and … Fig Resulting number of tuples? Different types of joins - theta join, natural join, equijoin

Equijoin R |X| Ai=Bi S requires identical values in every tuple for each pair of join attributes (one or more equality comparisons) Join conditions are all of the form A i = B i and A j = B j … Retrieve each department’s name and manager’s name. T  Department |X| mgrssn=ssn Employee Result   dname,fname,lname (T)

Order of precedence Unary: – Select, project (highest precedence) Binary: – Joins, Cartesian product – Intersection – Union, minus Use lots of parenthesis!

Write the following in Relational Algebra Select * from Employee, Department where dno=dnumber List employee SSNs who are female and work for the research department Select * From Employee, dept_locations Where dno = dnumber and dlocation = 'Houston'

List lname, essn of employees who work on pno=10 List lname, essn of employees who work on project ‘Computerization’

Theta Join R |X| Ai  Bi S where the join condition is of the form: Ai  Bi  is =, , , etc. Example: Scholarship(SName GPA_Req Desc) Student (Name CWID GPA Major) Select Name, SName From Student, Scholarship Where GPA >= GPA_Req

Natural join We will use the * notation (some others use |X| without subscript) Like an equijoin, except attributes for the equijoin in the second relation are deleted from result (Why have 2 columns with the same value?) Q  R * ( ),( ) S Fig Equivalent to equijoin but keep only list1 If attributes have the same name in both relations, list1 and list2 are not needed. In the original definition of natural join, the join attributes required to have the same names in both relations.

Write the following in Relational Algebra List locations of ‘Research’ department (use natural join) List SSN, lname of employees who do not have dependents

When renaming is needed A relation can have a set of join attributes with itself List all employee names and their supervisor names S(soc, first, last)   ssn,fname,lname Employee Temp  Employee |X| superssn=soc S Result   fname,lname,first,last (Temp) //alternatively Result  fname,lname,first,last  S(soc, first, last)  ssn,fname,lname Employee) |X| superssn=soc Employee) Usually, don't see qualification of attributes in relational algebra

Complete Set of Relational Algebra Operations { , , , , × } All other relational algebra operations can be expressed as a sequence of operations from this set. Other operations are for convenience. R |X| S =  (R X S) R  S = (R  S)  ((R  S)  (S  R))

Do we need anymore relational algebra operations to satisfy queries?

How about? Select COUNT(*) From Project Select pname, COUNT(ssn) From Project, Works_on Where pnumber=pno Group by pname

Additional relation algebra Operations Aggregate function - SUM, COUNT, AVG, MIN, MAX [ ]  ( ) R   count ssn, avg salary (Employee) The following uses the optional grouping attribute R  dno  count ssn, avg salary (Employee) Fig The attributes returned from an aggregate function are the attributes in the function list and any grouping attributes listed

Find out how many projects there are

Write in Relational Algebra Retrieve the number of projects For each project, retrieve the project number, project name, and the number of employees who work on that project. Select pnumber, pname, COUNT(*) From Project, Works_on Where pnumber =pno Group By pnumber, pname

Outer Join Extension of join and union In a regular equijoin or natural join, tuples in R1 or R2 that do not have matching tuples in the other relation do not appear in the result. Some queries require all tuples in R1 (or R2 or both) to appear in the result When no matching tuples are found, nulls are placed for the missing attributes.

Outer Join Left outer join: R1 ]X| R2 keeps every tuple in R1 in result. List all employees and if they are a manager, list dname Temp <- (Employee ]X| ssn=mgrssn Department) R <-  fname, minit, lname, dname (Temp) Fig Right outer join: R1 |X[ R2 keeps every tuple in R2 in result. Full outer join: R1 ]X[ R2 keeps every tuple in R1 and R2 in result. Think about how this is different from R1 X R2.

Division operation Part of original relational algebra T(Y) = R(Z)  S(X) tuple t is in result if t is in R for every tuple in S More generally, result is a relation T(Y) that includes t if t appears in R with the value of X for every tuple in S. Fig The attributes Y in table T = attributes of R in Z - attributes S in X, where Y is the set of attributes in R not in S. Result <- R  S

Division operation For example, Retrieve ssn of employees who work on all projects John smith works on. smith_pnos <-  pno Works_on |X| ssn=essn  fname='John' and lname='Smith' Employee ssn_pnos <-  essn, pno Works_on ssns <- ssn_pnos  smith_pnos //if wanted names would have to do another join results <-  fname, lname (ssns |X| ssn=essn Employee)

// Using minus Select ssn from Employee where lname <> ‘Smith’ and not exists ((Select pno from Works_on, Employee where ssn=essn and lname = ‘Smith’) minus (select pno from Works_on where ssn=essn)); / / if result of minus is empty, work on same projects

Write the following in Relational Algebra For every project located in 'Stafford' list the project number, the controlling department number and department manager's last name, address and birthdate. Select pnumber, dnum, lname, bdate, address From Project, Department, Employee Where dnum = dnumber and mgrssn = ssn and plocation = 'Stafford'

Write in Relational Algebra For each project on which more than two employees work, retrieve the project number, project name, and the number of employees who work on that project. Select pnumber, pname, COUNT(*) From Project, Works_on Where pnumber =pno Group By pnumber, pname Having COUNT(*) > 2

Write the following in Relational Algebra Compute the average number of dependents over employees with dependents Think about how you would write this: Select * From Employee Where salary > all (Select salary From Employee Where sex = 'F')

DDL - Also provided Declare Schema for database Declare Relation for Schema Insert into Relation Delete Relation tuple with specified condition Modify col. of Relation tuple with specified condition