More Relation Operations

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.
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.
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.
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
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.
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.
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 2015, Fall Pusan National University Ki-Joune Li.
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 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.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
1 Introduction to Database Systems, CS420 Relational Algebra.
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
Slides are reused by the approval of Jeffrey Ullman’s
Module 2: Intro to Relational Model
Outerjoins, Grouping/Aggregation Insert/Delete/Update
16.2.Algebraic Laws for Improving Query Plans
Databases : More about SQL
Chapter 2: Intro to Relational Model
Database Design and Programming
SQL Structured Query Language 11/9/2018 Introduction to Databases.
CS 405G: Introduction to Database Systems
IST 210: Organization of Data
Operators Expression Trees Bag Model of Data
Relational Algebra Chapter 4 - part I.
16.2.Algebraic Laws for Improving Query Plans
Basic Operations Algebra of Bags
More Complex Operators
Algebraic Laws.
Algebraic and Logical Query Languages pp.54 is added
Chapter 2: Intro to Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
5.1 Relational Operations on Bags
CPSC-608 Database Systems
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 2018, Fall Pusan National University Ki-Joune Li

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.

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. Projection: No more operation except deleting other attributes

Operations on Bags Selection: Projection Products and joins 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.

Examples Given two relations R(A,B) and S(B,C) R S SELECTA+B<5 (R) ProjectA(R) A B 1 2 5 6 B C 3 4 7 8 A B 1 2 A 1 5 R X S R R.B<S.B S A R.B S.B C 1 2 3 4 5 6 7 8 A R.B S.B C 1 2 3 4 7 8 5 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. 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. 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. But never less than 0 times. Example: {1,2,1} – {1,2,3} = {1}.

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

The Extended Algebra δ: eliminate duplicates from bags. τL: sort tuples. L is a list of attributes 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. ◦

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

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.

Grouping Operator R1 := γ L (R2). Example 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 StarsIn(title, year, starName) γ starName, MIN(year)minYear,Count(title)ctTitle (StarsIn) 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)

Grouping Operator Title Year StarName Star Wars 1977 Carrie Fisher Mark Hamill Harrison Ford Mighty Ducks 1991 Emilio Estevez Wayne’s World 1992 Dana Carvey My world Raiders of the Lost Ark 1981 Tiki's story StarName minYear ctTitle Carrie Fisher 1977 1 Harrison Ford 2 Emilio Estevez 1991 Dana Carvey 1992 Mark Hamill 3

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

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. Similarly for a tuple of S. Outerjoin preserves dangling tuples by padding them with a special NULL symbol in the result. R S R S R S A B C 1 2 3 4 5 6 7 8 9 B C D 2 3 10 11 6 7 12 A B C D 1 2 3 10 11 4 5 6 Ʇ 7 8 9 12