Lecture 15: Relational Algebra

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 Tim Kaddoura CS157A. Introduction  Relational query languages are languages for describing queries on a relational database  Three.
The Relational Algebra
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
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.
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.
The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
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.
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.
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 )
Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
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.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
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.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 9 A First Course in 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.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
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.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Lecture 8 Relational Algebra April 26, 2015.
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
The Relational Algebra and Calculus
COMP3017 Advanced Databases
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
The Relational Algebra and Relational Calculus
Chapter 2: Intro to Relational Model
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Chapter 4 The Relational Algebra and Calculus
The Relational Algebra and Calculus
LECTURE 3: Relational Algebra
The Relational Algebra and Relational Calculus
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Presentation transcript:

Lecture 15: Relational Algebra CSE 480: Database Systems Lecture 15: Relational Algebra Reference: Read Chapter 6.1-6.3.1 of the textbook

What is Relational Algebra? A formal way to express a query in relational model A query consists of relational expressions describing the sequence of operators applied to obtain the query result Example :  LName, Sex ( DNo=4 OR Salary>100000 (EMPLOYEE) )  and  are the operators

Why Study Relational Algebra? Query processing in DBMS SELECT A.Name, B.Grade FROM A, B WHERE A.Id = B.Id  Name, Grade (Id,Name(A  B))

Relational Algebra Operator Output of a relational algebra expression is a relation Types of operators in a relational algebra expression Unary: op(Relation) Binary: Relation op Relation Relational algebra operators select, project, union, set difference, Cartesian product

Select Operator () Id Name Address Status condition (R) (NOT THE SAME AS SELECT in SQL) Resulting relation contains only tuples in R that satisfy the given condition has the same attributes (columns) as the original relation Example: Student Status=‘Junior’(Student) Id Name Address Status Id Name Address Status 1123 John 123 Main Junior 1234 Lee 123 Main Senior 5556 Mary 7 Lake Dr Freshman 9876 Bart 5 Pine St Junior 1123 John 123 Main Junior 9876 Bart 5 Pine St Junior

Selection Condition select_condition (relation) Selection condition acts as a filter to the rows in relation Selection condition is a Boolean expression Simple selection condition: <attribute> operator <constant> <attribute> operator <attribute> where operator: <, , , >, =,  <condition> AND <condition> <condition> OR <condition> NOT <condition>

Selection Condition - Examples Student 1123 John 123 Main Junior 1234 Lee 123 Main Senior 5556 Mary 7 Lake Dr Freshman 9876 Bart 5 Pine St Junior Id Name Address Status Selectivity Fraction of tuples selected by a selection condition  Id>3000 OR Status=‘Freshman’ (Student) selectivity = 2/4  Id>3000 AND Id <3999 (Student) selectivity = 0/4  NOT(Status=‘Senior’) (Student) selectivity = 3/4  Status‘Senior’ (Student) selectivity = 3/4

Selection Condition – Incorrect Queries Find professors who taught both CS315 and CS305  CrsCode=‘CS315’ AND CrsCode=‘CS305’ (TEACHING) X Find courses taught by CS professors  PROFESSOR.Id=Teaching.ProfId AND PROFESSOR.DeptId=‘CS’ (TEACHING) X

Properties of SELECT operator Commutative:  <condition1>( < condition2> (R)) =  <condition2> ( < condition1> (R)) Cascade of SELECT operations may be applied in any order: <cond1>(<cond2> (<cond3> (R)) = <cond2> (<cond3> (<cond1> ( R))) Cascade of SELECT operations may be replaced by a single selection with a conjunction of all the conditions: <cond1>(< cond2> (<cond3>(R)) =  <cond1> AND < cond2> AND < cond3>(R)))

Project Operator Produces a relation containing a subset of columns of its input relation attribute list(R) Example: Student Name,Status(Student) Id Name Address Status Name Status 1123 John 123 Main Junior 1234 Lee 123 Main Freshman 5556 Mary 7 Lake Dr Senior 9876 Bart 5 Pine St Junior John Junior Lee Freshman Mary Senior Bart Junior

Project Operator Resulting relation has no duplicates; therefore it can have fewer tuples than the input relation Example: Student Address(Student) Id Name Address Status Address 123 Main 7 Lake Dr 5 Pine St 1123 John 123 Main Junior 1234 Lee 123 Main Freshman 5556 Mary 7 Lake Dr Senior 9876 Bart 5 Pine St Junior

Properties of PROJECT Operator Number of tuples in the result of projection <list>(R) is always less or equal to the number of tuples in R If list of projected attributes includes a superkey, the resulting relation has the same number of tuples as the input relation PROJECT operator is not commutative  <list1> ( <list2> (R) )   <list2> ( <list1> (R) )

Relational Algebra Expressions Find the Ids and names of junior undergraduate students Student 1123 John 123 Main Junior 1234 Lee 123 Main Freshman 5556 Mary 7 Lake Dr Senior 9876 Bart 5 Pine St Junior Id Name Address Status 1123 John 9876 Bart Result Id Name  Id, Name ( Status=’Junior’ (Student) )

Relational Algebra Expressions We may want to apply several relational algebra operations one after the other We can write the operations as a single relational algebra expression by nesting the operations, or We can apply one operation at a time and create intermediate result relations. In the latter case, we must give names to the relations that hold the intermediate results.

Single versus Sequence of operations Query: Find the first name, last name, and salary of all employees who work in department number 5 We can write a single relational algebra expression: FNAME, LNAME, SALARY( DNO=5(EMPLOYEE)) OR We can explicitly show the sequence of operations, giving a name to each intermediate relation: DEP5_EMPS   DNO=5(EMPLOYEE) RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)

RENAME operator The RENAME operator is denoted by  We may rename the attributes of a relation or the relation name or both S (B1, B2, …, Bn )(R) Change the relation name from R to S, and Change column (attribute) names to B1, B2, …..Bn S(R) : Change the relation name only to S (B1, B2, …, Bn )(R) : Change the column (attribute) names only to B1, B2, …..Bn

Example Change relation name to Emp and attributes to First_name, Last_name, and Salary Emp(First_name, Last_name, Salary ) (  FNAME,LNAME,SALARY (EMPLOYEE)) or Emp(First_name, Last_name, Salary)   FNAME,LNAME,SALARY (EMPLOYEE)

Set Operators A relation is a set of tuples; therefore set operations are applicable: Intersection:  Union:  Set difference (Minus):  Set operators are binary operators Operator: Relation  Relation  Relation Result of set operation is a relation that has the same schema as the combining relations

Examples: Set Operations Y A B x1 x2 x3 x4 A B x1 x2 x5 x6 X  Y X  Y X – Y A B x1 x2 A B x1 x2 x3 x4 x5 x6 A B x3 x4

Example STUDENT  INSTRUCTOR STUDENT – INSTRUCTOR STUDENT  INSTRUCTOR

Union Compatible Relations Set operations are limited to union compatible relations Two relations are union compatible if: they have the same number of attributes, and the domains of corresponding attributes are type compatible (i.e. dom(Ai)=dom(Bi) for i=1, 2, ..., n). The resulting relation has the same attribute names as the first operand relation

Example Tables: Student (SSN, Name, Address, Status) Professor (Id, Name, Office, Phone) are not union compatible. But  Name (Student) and  Name (Professor) are union compatible

Exercise Relations: Find the SSN of student employees Employee (SSN, Name, Address) Professor (SSN, Office, Phone) Student (SSN, Status) Find the SSN of student employees  SSN (Employee)   SSN (Student) Find the SSN of employees who are not professors  SSN (Employee) –  SSN (Professor) Find the SSN of employees who are neither professors nor students  SSN (Employee) – ( SSN (Student)   SSN (Professor))

Cartesian Product A B C D A B C D x1 x2 y1 y2 x1 x2 y1 y2 R  S is expensive to compute: Number of columns = degree(R) + degree(S) Number of rows = number of rows (R) × number of rows (S) A B C D A B C D x1 x2 y1 y2 x1 x2 y1 y2 x3 x4 y3 y4 x1 x2 y3 y4 x3 x4 y1 y2 R S x3 x4 y3 y4 R S

Example List all the possible Broker-Client pairs Broker (BrokerId, BrokerName) Client (ClientId, ClientName) List all the possible Broker-Client pairs Broker  Client BrokerID BrokerName 1 Merrill Lynch 2 Morgan Stanley 3 Salomon Smith Barney ClientId ClientName A11 Bill Gates B11 Steve Jobs BrokerId BrokerName ClientId ClientName 1 Merrill Lynch A11 Bill Gates B11 Steve Jobs 2 Morgan Stanley 3 Salomon Smith Barney

More Complex Examples Find the names of employees who are not supervisors

Example Find the names of employees who earn more than their supervisors

Summary (Relational Algebra Operators) Unary operators SELECT condition(R) PROJECT Attribute-List(R) RENAME S(A1,A2,…Ak)(R) Set operators R  S R  S R – S or S – R Cartesian product R  S