Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

Tallahassee, Florida, 2014 COP4710 Database Systems Relational Algebra Fall 2014.
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
SQL CSET 3300.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
1 Lecture 12: Further relational algebra, further SQL
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
Midterm Review II. Redundancy. –Information may be repeated unnecessarily in several tuples. –E.g. length and filmType. Update anomalies. –We may change.
Relational Operations on Bags Extended Operators of Relational Algebra.
Query Execution Since our SQL queries are very high level the query processor does a lot of processing to supply all the details. An SQL query is translated.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
Fall 2001Arthur Keller – CS 1806–1 Schedule Today (TH) Bags and SQL Queries. u Read Sections Project Part 2 due. Oct. 16 (T) Duplicates, Aggregation,
Relational Operations on Bags Extended Operators of Relational Algebra.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
Chapter 5 Algebraic and Logical Query Languages pp.54 is added Pp 61 updated.
16.2 ALGEBRAIC LAWS FOR IMPROVING QUERY PLANS Ramya Karri ID: 206.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
1 Relational Algebra Basic operators Relational algebra expression tree.
1 Relational Algebra Operators Expression Trees Bag Model of Data.
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Relational Algebra Basic Operations Algebra of Bags.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
Databases 1 Seventh lecture. Topics of the lecture Extended relational algebra Normalization Normal forms 2.
Databases 1 First lecture. Informations Lecture: Monday 12:15-13:45 (3.716) Practice: Thursday 10:15-11:45 (2-519) Website of the course:
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
1 Relational Algebra Basic Operations Algebra of Bags.
Databases : Relational Algebra 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
From Professor Ullman, Relational Algebra.
Database Management Systems Chapter 5 The Relational Algebra Instructor: Li Ma Department of Computer Science Texas Southern University, Houston October,
1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which.
1 Lecture 2 Relational Algebra Based on
Chapter 5 Notes. P. 189: Sets, Bags, and Lists To understand the distinction between sets, bags, and lists, remember that a set has unordered elements,
Extended Operators in SQL and Relational Algebra Zaki Malik September 11, 2008.
More Relation Operations 2015, Fall Pusan National University Ki-Joune Li.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
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)
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Relational Algebra BASIC OPERATIONS 1 DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
1 Relational Algebra Basic Operations Algebra of Bags.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
1 Introduction to Database Systems, CS420 Relational Algebra.
1 Database Design: DBS CB, 2 nd Edition Relational Algebra: Basic Operations & Algebra of Bags Ch. 5.
1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:
Summary of Relational Algebra
Basic Operations Algebra of Bags
CPSC-310 Database Systems
Slides are reused by the approval of Jeffrey Ullman’s
COP4710 Database Systems Relational Algebra.
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Databases : More about SQL
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Database Design and Programming
IST 210: Organization of Data
Operators Expression Trees Bag Model of Data
More Relation Operations
Basic Operations Algebra of Bags
Algebraic and Logical Query Languages pp.54 is added
5.1 Relational Operations on Bags
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web page (

STEMPNU 2 Building Complex Expressions Algebras allow us to express sequences of operations in a natural way.  Example: in arithmetic --- (x + 4)*(y - 3). Relational algebra allows the same. Three notations, just as in arithmetic: w Sequences of assignment statements. w Expressions with several operators. w Expression trees.

STEMPNU 3 Sequences of Assignments Create temporary relation names. Renaming can be implied by giving relations a list of attributes. Example: R3 := R1 JOIN C R2 can be written: R4 := R1 X R2 R3 := SELECT C (R4)

STEMPNU 4 Expressions in a Single Assignment Example: the theta-join R3 := R1 JOIN C R2 can be written: R3 := SELECT C (R1 X R2) Precedence of relational operators: 1. Unary operators --- select, project, rename --- have highest precedence, bind first. 2. Then come products and joins. 3. Then intersection. 4. Finally, union and set difference bind last. w But you can always insert parentheses to force the order you desire.

STEMPNU 5 Expression Trees Leaves are operands --- either variables standing for relations or particular, constant relations. Interior nodes are operators, applied to their child or children.

STEMPNU 6 Example 1 Using the relations  Bars(name, addr) and  Sells(bar, beer, price),  find the names of all the bars that are either on Maple St. or sell Bud for less than $3.

STEMPNU 7 As a Tree: BarsSells SELECT addr = “Maple St.” SELECT price<3 AND beer=“Bud” PROJECT name RENAME R(name) PROJECT bar UNION

STEMPNU 8 Example 2 Using Sells(bar, beer, price),  find the bars that sell two different beers at the same price. Strategy:  by renaming, define a copy of Sells, S(bar, beer1, price). The natural join of Sells and S consists of quadruples (bar, beer, beer1, price) such that the bar sells both beers at this price.

STEMPNU 9 The Tree Sells RENAME S(bar, beer1, price) JOIN Sells.bar=S.bar and Sells.price=S.price PROJECT bar SELECT beer != beer1

STEMPNU 10 Relational Algebra on Bags A bag is like a set, but an element may appear more than once.  Multiset is another name for “ bag. ”  Example: {1,2,1,3} is a bag. {1,2,3} is also a bag that happens to be a set. Bags also resemble lists, but order in a bag is unimportant.  Example: {1,2,1} = {1,1,2} as bags, but [1,2,1] != [1,1,2] as lists.

STEMPNU 11 Why Bags? SQL, the most important query language for relational databases is actually a bag language.  SQL will eliminate duplicates, but usually only if you ask it to do so explicitly. Some operations, like projection, are much more efficient on bags than sets.

STEMPNU 12 Operations on Bags Selection:  Applies to each tuple,  so its effect on bags is like its effect on sets. Projection  As a bag operator, we do not eliminate duplicates. Products and joins  done on each pair of tuples, so duplicates in bags have no effect on how we operate.

STEMPNU 13 Example: Bag Selection R(A,B )S(B,C ) SELECT A+B<5 (R) =AB 12

STEMPNU 14 Example: Bag Projection R(A,B )S(B,C ) PROJECT A (R) =A 1 5 1

STEMPNU 15 Example: Bag Product R(A,B )S(B,C ) R X S =AR.BS.BC

STEMPNU 16 Example: Bag Theta-Join R(A,B )S(B,C ) R JOIN R.B<S.B S =AR.BS.BC

STEMPNU 17 Bag Union Union, intersection, and difference need new definitions for bags. An element appears in the union of two bags the sum of the number of times it appears in each bag. Example: {1,2,1} UNION {1,1,2,3,1} = {1,1,1,1,1,2,2,3}

STEMPNU 18 Bag Intersection An element appears in the intersection of two bags the minimum of the number of times it appears in either. Example: {1,2,1} INTER {1,2,3} = {1,2}.

STEMPNU 19 Bag Difference An element appears in the difference A – B of bags as many times as it appears in A, minus the number of times it appears in B.  But never less than 0 times. Example: {1,2,1} – {1,2,3} = {1}.

STEMPNU 20 Beware: Bag Laws != Set Laws Not all algebraic laws that hold for sets also hold for bags.  Example: Commutative law for union (R UNION S = S UNION R ) does hold for bags. Since addition is commutative, adding the number of times x appears in R and S doesn ’ t depend on the order of R and S.  Counter Example: Set union is idempotent, meaning that S UNION S = S. However, for bags, if x appears n times in S, then it appears 2n times in S UNION S. Thus S UNION S != S in general

STEMPNU 21 The Extended Algebra 1. DELTA = eliminate duplicates from bags. 2. TAU = sort tuples. 3. Extended projection : arithmetic, duplication of columns. 4. GAMMA = grouping and aggregation. 5. OUTERJOIN: avoids “ dangling tuples ” = tuples that do not join with anything.

STEMPNU 22 Duplicate Elimination R1 := DELTA(R2). R1 consists of one copy of each tuple that appears in R2 one or more times.

STEMPNU 23 Example: Duplicate Elimination R =AB DELTA(R) =AB 12 34

STEMPNU 24 Sorting R1 := TAU L (R2).  L is a list of some of the attributes of R2. R1 is the list of tuples of R2 sorted first on the value of the first attribute on L, then on the second attribute of L, and so on.  Break ties arbitrarily. TAU is the only operator whose result is neither a set nor a bag.

STEMPNU 25 Example: Sorting R =AB TAU B (R) =[(5,2), (1,2), (3,4)]

STEMPNU 26 Extended Projection Using the same PROJ L operator, we allow the list L to contain arbitrary expressions involving attributes, for example: 1. Arithmetic on attributes, e.g., A+B. 2. Duplicate occurrences of the same attribute.

STEMPNU 27 Example: Extended Projection R =AB PROJ A+B,A,A (R) =A+BA1A

STEMPNU 28 Aggregation Operators Aggregation operators are not operators of relational algebra. Rather, they apply to entire columns of a table and produce a single result. The most important examples: SUM, AVG, COUNT, MIN, and MAX.

STEMPNU 29 Example: Aggregation R =AB SUM(A) = 7 COUNT(A) = 3 MAX(B) = 4 AVG(B) = 3

STEMPNU 30 Grouping Operator R1 := GAMMA L (R2).  L is a list of elements that are either: w Individual (grouping ) attributes. w AGG(A ), where AGG is one of the aggregation operators and A is an attribute. Example: StarsIn (title, year, starName)  Find the earliest year in which they appeared in movie for each star who has appeared at least three times

STEMPNU 31 Example: Grouping/Aggregation R =ABC GAMMA A,B,AVG(C) (R) = ?? First, group R : ABC Then, average C within groups: ABAVG(C)

STEMPNU 32 Example: Grouping/Aggregation StarsIn (title, year, starName)  Find the earliest year in which they appeared in movie for each star who has appeared at least three times R1:=Gamma starName, MIN(year)  minYear, Count(title)  ctTitle (StarIN)  R2 := SELECT ctTitle >= 3 (R1)  R3 := PROJECT minYear (R2)

STEMPNU 33 Outerjoin Suppose we join R JOIN C S. A tuple of R that has no tuple of S with which it joins is said to be dangling.  Similarly for a tuple of S. Outerjoin preserves dangling tuples by padding them with a special NULL symbol in the result.

STEMPNU 34 Example: Outerjoin R = ABS =BC (1,2) joins with (2,3), but the other two tuples are dangling. R OUTERJOIN S =ABC NULL NULL67