More Relation Operations 2015, Fall Pusan National University Ki-Joune Li.

Slides:



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

CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
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.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 227 Database Systems I Design Theory for Relational Databases.
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.
Query Compiler. The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical.
Subqueries Example Find the name of the producer of ‘Star Wars’.
The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical query plan into.
Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin.
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.
Closure The closure of {B 1 …B k } under the set of FDs S, denoted by {B 1 …B k } +, is defined as follows: {B 1 …B k } + = {B | any relation satisfies.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
CMSC424: Database Design Instructor: Amol Deshpande
The principal problem that we encounter is redundancy, where a fact is repeated in more than one tuple. Most common cause: attempts to group into one relation.
CMSC424: Database Design Instructor: Amol Deshpande
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.
Relational Operations on Bags Extended Operators of Relational Algebra.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
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 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.
Databases 1 Seventh lecture. Topics of the lecture Extended relational algebra Normalization Normal forms 2.
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,
Advanced Relational Algebra & SQL (Part1 )
Extended Operators in SQL and Relational Algebra Zaki Malik September 11, 2008.
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.
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.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Relational Algebra BASIC OPERATIONS 1 DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA.
Lu Chaojun, SJTU 1 Extended Relational Algebra. Bag Semantics A relation (in SQL, at least) is really a bag (or multiset). –It may contain the same tuple.
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.
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
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:
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Basic Operations Algebra of Bags
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Databases : More about SQL
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
Chapter 2: Intro to Relational Model
5.1 Relational Operations on Bags
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Relational Algebra Chapter 4 - part I.
Presentation transcript:

More Relation Operations 2015, Fall Pusan National University Ki-Joune Li

2 Relational Algebra on Bags A bag is like a set, but an element may appear more than once. o Multiset is another name for “ bag. ” o 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. o Example: {1,2,1} = {1,1,2} as bags, but [1,2,1] != [1,1,2] as lists.

3 Why Bags? SQL, the most important query language for relational databases is actually a bag language. o 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. o Projection: No more operation except deleting other attributes

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

Examples Given two relations R(A,B) and S(B,C) 5 AB R BC S AB A SELECT A+B<5 (R)Project A (R) AR.BS.BC R X S R R.B<S.B S AR.BS.BC

6 Bag Union, Intersection, Difference Union, intersection, and difference need new definitions for bags. Union: An element appears in the union of two bags the sum of the number of times it appears in each bag. o Example: {1,2,1} U {1,1,2,3,1} = {1,1,1,1,1,2,2,3} Intersection: An element appears in the union of two bags the minimum of the number of times it appears in either. o Example: {1,2,1} ∩ {1,2,3} = {1,2}. 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. o But never less than 0 times. o Example: {1,2,1} – {1,2,3} = {1}.

7 Beware: Bag Laws != Set Laws Not all algebraic laws that hold for sets also hold for bags. o 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. o 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

8 The Extended Algebra δ: eliminate duplicates from bags. τ L : sort tuples. o L is a list of attributes o Result of Sorting: a List (not set neither bag) Extended projection : arithmetic, duplication of columns. γ: grouping and aggregation. OUTERJOIN ( ):avoids “ dangling tuples ” = tuples that do not join with anything. ◦

9 Extended Projection Using the same Π L operator, we allow the list L to contain arbitrary expressions involving attributes, for example: o Arithmetic on attributes, e.g., A+B. o Duplicate occurrences of the same attribute. o Example: Π A+B,A,A (R)

10 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.

11 Grouping Operator R1 := γ L (R2). o L is a list of elements that are either:  Individual (grouping ) attributes.  AGG(A), where AGG is one of the aggregation operators and A is an attribute. Example o StarsIn(title, year, starName) o γ starName, MIN(year)  minYear,Count(title)  ctTitle (StarsIn) o Find the earliest year in which they appeared in movie for each star who has appeared at least three times  R1:= γ starName, MIN(year)  minYear, Count(title)  ctTitle (StarsIn)  R2 := SELECT ctTitle >= 3 (R1)  R3 := PROJECT minYear (R2)

12 Grouping Operator TitleYearStarName Star Wars1977Carrie Fisher Star Wars1977Mark Hamill Star Wars1977Harrison Ford Mighty Ducks1991Emilio Estevez Wayne’s World1992Dana Carvey My world1977Mark Hamill Raiders of the Lost Ark1981Harrison Ford Tiki's story1977Mark Hamill StarNameminYearctTitle Carrie Fisher19771 Harrison Ford19772 Emilio Estevez19911 Dana Carvey19921 Mark Hamill19773

δ (eliminating duplications) and γ (grouping) δ is a special case of γ Suppose R(A 1, A 2,..., A n ), o then δ(R)= γ A1, A2,..., An (R ) o but γ A1, A2,..., An (R ) = Π A1, A2,..., An (R ), only if R is a set (not a bag) 13

14 Outerjoin Suppose we join R S. A tuple of R that has no tuple of S with which it joins is said to be dangling. o Similarly for a tuple of S. Outerjoin preserves dangling tuples by padding them with a special NULL symbol in the result. o R S ABC R BCD S ABCD R S