Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra:

Slides:



Advertisements
Similar presentations
Relational Database Operators
Advertisements

Relational Algebra and Relational Calculus
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Copyright © C. J. Date 2005page 97 S#Y S1DURINGS3DURING [d04:d10][d08:d10] S2DURINGS4DURING [d02:d04][d04:d10] [d08:d10] WITH ( EXTEND T2 ADD ( COLLAPSE.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
Domains, Relations & Base RelVars (Ch
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Algebra. Relational algebra Consists of collection of operators –Restrict, project, join, … Takes relations as operands Returns relations as.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Chapter 6 Relations. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Topics in this Chapter Tuples Relation Types Relation Values Relation.
Chapter 6 Relations. Topics in this Chapter Tuples Relation Types Relation Values Relation Variables SQL Facilities.
Databases Illuminated
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Chapter 3 Section 3.4 Relational Database Operators
Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with.
Chapter 9 Integrity. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Topics in this Chapter Predicates and Propositions Internal vs.
The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.
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 Database Model
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
RELATIONAL ALGEBRA CHAPTER 6 1. LECTURE OUTLINE  Unary Relational Operations: SELECT and PROJECT  Relational Algebra Operations from Set Theory  Binary.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter 5 Relational Algebra Pearson Education © 2014.
Relational Algebra Operators
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 )
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
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.
Relational Algebra Prof. Yin-Fu Huang CSIE, NYUST Chapter 7.
Chapter 10 Views. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.10-2 Topics in this Chapter What are Views For? View Retrievals View Updates.
Views Prof. Yin-Fu Huang CSIE, NYUST Chapter 10. Advanced Database System Yin-Fu Huang 10.1Introduction Example: Var Good_Supplier View (S Where Status.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Ritu CHaturvedi Some figures are adapted from T. COnnolly
STRUCTURE OF PRESENTATION :
COMP3017 Advanced Databases
Chapter 10 Views.
The Relational Database Model
Lecture 2 The Relational Model
Theory behind the relational engine
Theory behind the relational engine
Chapter 3 The Relational Database Model
The Relational Algebra and Relational Calculus
The Relational Algebra
The Relational Model L2DB.
Chapter 4 Relational Algebra
Relational Database Operators
Presentation transcript:

Chapter 7 Relational Algebra

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points and Additional Operators Grouping and Ungrouping

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-3 Relational Algebra The relational algebra is a collection of operators that take relations as their operands and return a relation as their result Eight operators, in two groups of four Union, intersect, difference, Cartesian product Restrict, project, join, divide The set of possible relational operators is essentially unlimited The operators are “read only”

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-4 Closure Revisited The output from any relational operator is another relation: the closure property Relational expressions can be nested (analogously to arithmetic expressions) Every relation has a head and a body; relational algebra must address both Attribute type inference must be supported RENAME changes the name of an attribute without changing its type or content

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-5 The Syntax of the Original Algebra Each operator returns a relation, and operates on a relation Each operator assigns a relation value to the new relation, based on alterations to the tables being operated upon Generically: := RELATION { }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-6 The Syntax of the Original Algebra – General Format := RELATION { } | | -- “relation operator invoked” | -- “with expression” | | ( )

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-7 Semantics of the Original Algebra – Union Union operates on two sets and returns a set that contains all elements belonging to either Both sets must be of the same type - formerly known as union compatibility Relations cannot have duplicate tuples; we say loosely that UNION “eliminates duplicates”

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-8 Semantics of the Original Algebra – Intersect and Difference Intersect operates on two sets and returns a set that contains all tuples belonging to both Difference operates on two sets and returns a set containing all tuples occuring in one but not the other, using MINUS For both Intersect and Difference, the sets operated upon must be of the same type - formerly known as union compatibility

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-9 Semantics of the Original Algebra – Cartesian Product A Cartesian Product is the set of all ordered pairs such that in each pair, the first element comes from the first set, and the second element comes from the second set However, since the result of a relational operator is a relation, the result of each pair is a single tuple containing all the elements of both of the source tuples Uses keyword TIMES

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-10 Semantics of the Original Algebra – Restrict Yields a horizontal subset – a/k/a “SELECT” a WHERE p p is called the restriction condition p is a predicate, and returns boolean If it can be evaluated by examining a single tuple it is simple; otherwise it is nonsimple

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-11 Semantics of the Original Algebra – Project Yields a vertical subset The general form is a commalist of attributes to be kept in the result For all attributes kept, all tuples are kept An alternative specification is to name the attributes to be excluded: P { ALL BUT WEIGHT}

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-12 Semantics of the Original Algebra – Join – Natural Join When unqualified, join means “natural join” For any two relations with at least one matching attribute, the join operator returns a relation with a single tuple of all the attributes for each match Attributes that do not match from each source relation are retained If no attributes match, result is a Cartesian product If all attributes match, result is an Intersect

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-13 Semantics of the Original Algebra – Join – Theta Join Used to join relations based on matching attributes, where the values are not equal Given relations a and b, and attributes X and Y, this can be expressed as follows: (a TIMES b) WHERE X theta Y When theta is set to = the result can be made to be that of natural join (project away the duplicate attribute, and rename the kept one)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-14 Semantics of the Original Algebra – Divide Used to “divide one relation into another” Small Divide uses one relation expression as divisor, Great Divide uses two For small divide: a DIVIDEDBY b PER c where a is the dividend, b is the divisor, and c is the mediator Used to determine who in a relates to the complete set in b

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-15 Semantics of the Original Algebra – Divide - Example Let S be a relation of suppliers, P one of parts, and SP the mediator S JOIN ( S {S#} DIVIDEDBY P {p#} PER SP {S#, P#} ) Will return a relation with suppliers who supply all parts, only This represents an improvement in clarity over the original formulation of DIVIDE

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-16 What is the Algebra for? The purpose of the algebra is to allow the writing of relational expressions Applications of the algebra: retrieval, update, defining integrity constraints, derived relvars, stability and security An implemented language can be said to be relationally complete if it is at least as powerful as the algebra

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-17 The Original Algebra Many operators are associative: Union, intersect, times, join, but not minus Many operators are commutative: Union, intersect, times, join, but not minus Join, union, intersect were originally defined as dyadic, but are now seen to operate on any number of relations, including DEE and DUM

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-18 Additional Relational Operators Semijoin is used to perform a partial join based on restrictions (Join for a specific part number, for example) Semidifference is similar (Obtain suppliers who do not supply a particular part, e.g.) Extend adds an attribute dynamically, but does not alter the underlying relvar Summarize performs vertical or attribute-wise computations

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-19 Grouping… Required because relations can have attributes that are themselves relations Provides a map between such relations and “flat” relations SP GROUP {P#, QTY} AS PQ Will return quantities of parts by supplier, which is the unnamed co-conspirator

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-20 …and Ungrouping Returns the original relation In the example, the original SP relation If you group, you can always ungroup, but the converse is not necessarily true This occurs when the relations being ungrouped were not validly grouped in the first place