Chapter 5 Algebraic and Logical Query Languages pp.54 is added Pp 61 updated.

Slides:



Advertisements
Similar presentations
1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.
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
Relational Algebra.
Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.
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.
16.2.Algebraic Laws for Improving Query Plans Algebraic Laws for Improving Query Plans Commutative and Associative Laws Laws Involving.
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.
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.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
Instructor: Mohamed Eltabakh
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.
The Relational Database Model
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Algebra (Chapter 7)
From Professor Ullman, Relational Algebra.
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,
Database Systems Relational Algebra assoc. prof., dr. Vladimir Dimitrov web: is.fmi.uni-sofia.bg.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
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.
Relational Algebra Instructor: Mohamed Eltabakh 1 Part II.
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.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
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.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
1 CSE 480: Database Systems Lecture 16: 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:
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Basic Operations Algebra of Bags
Module 2: Intro to Relational Model
16.2.Algebraic Laws for Improving Query Plans
Chapter 3: Relational Model III
Chapter 2: Intro to Relational Model
Operators Expression Trees Bag Model of Data
The Relational Algebra and Relational Calculus
Instructor: Mohamed Eltabakh
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More Relation Operations
16.2.Algebraic Laws for Improving Query Plans
Basic Operations Algebra of Bags
More Complex Operators
Algebraic and Logical Query Languages pp.54 is added
One-Pass Algorithms for Database Operations (15.2)
Chapter 2: Intro to Relational Model
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
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Presentation transcript:

Chapter 5 Algebraic and Logical Query Languages pp.54 is added Pp 61 updated

5.1 Relational Operations on Bags What is a bag?

Bags What is a bag? Bag is a relation that may( or may not ) have duplicate tuples. Example: AB

Bags continue Since the tuple (1,2) appear three times this is a bag

5.1.1 Why Bags? Speed Example: Suppose I want the projection of A and B from the following relation. ABC

I simply cut attribute C and get the result AB I created a table and copy A and B to it. Simple and fast!

Now suppose I wanted a set with no duplication I will have to take the first tuple and put it in AB 12 I will then have to read the second tuple and compare it against the first. AB 12 34

Since they are different I will include this tuple in the result and get AB Now I will read the third tuple and compare it to the first. AB

Since they are the same I will not include this tuple. The point is that I had to do a lot of work. Each new tuple has to be compared with all other tuples before I could add it to the set. Hence time consuming.

Another reason to use bags Suppose I would like to calculate the average Of attribute A. Suppose farther that A = revenue in million of dollars. ABC Then the average of a set will be 2 and the actual average is 1.5. this is substantial difference.

5.1 Relational Operations on Bags Union intersection and Difference of bags

RUS that same as regular union only welcomes duplications. Suppose we have two grocery bags. One has two boxes of Oreos the second has five boxes or Oreos. I consolidate the two bags into one bag with 2+5=7 Oreo boxes. Union of bags

Intersection of Bags R ∩ S If the tuple t appears n times in R and m times in S then the tuple t appear min(m, n) times in the intersection. That is because the intersection it the common element in R and S and the relations has exactly min(m, n) in common.

The difference of R and S Each occurrence of t in S will cancel one occurrence of t in R. Then output is the “left over” of t.

Examples of union, intersection and difference on bags

Let R be the relation (bag) AB * Let S be the relation bellow.(bag) AB

Then R U S in a bag is simply the two tables written together. AB

Intersection of bags R ∩ S AB 12 34

The difference of bags R and S, R-S AB 12 12

5.1 Relational Operations on Bags Projection of Bags It has been explained previously (simply cut)

5.1 Relational Operations on Bags Selection on Bags

Selection on bags Let R be the bag ABC σ C>=6 (R)

ABC Since it is a bag we allow duplication

5.1 Relational Operations on Bags Product of Bags

Product on bags R X S AB Bag R Bag S BC

Product on bags As we learned earlier each row from R has to be paired with ALL rows in S. AR.BS.BC

Product on bags As we learned earlier each row from R has to be paired with ALL rows in S. AR.BS.BC

Product on bags continue Notice that in the above bag we again used the convention for the attribute name. B appear twice so we call it R.B and S.B. Equivalent Relation

5.1 Relational Operations on Bags Joins of Bags

Joins of bags ∞ We compare each tuple of one relation with each tuple of the other, decide whether or not this pair of tuples joins successfully, and if so we put the resulting tuple in the answer. When constructing the answer we permit duplication.

Example of Joins in bags ∞ AB Relation R Relation S AB

Result of R ∞ S ABC Please notice that unlike the product we do not write B for each relation. We are “naturally” joining the relations. Think of it as the transitive rule.

Theta-join in bags 1.Find the product of the two relations 2. select only these tuples that comply with the condition. 3.Allow duplications It is the symbol ∞ C with condition beneath it.

Example theta join AB Relation R Relation S AB

The theta-join of R and S with the condition R.B <S.B AR.BS.BC < 4 hence selected

5.1 Relational Operations on Bags 1. Exercise from previous section (Team 3/7) P52: upload Fig into your oracle (submit the source codes: create and insert statements to grader Exercises for Section 5.1 Ex (3/7) Ex Assigned in the Class List them into the algebraic law Table

5.2 Extended Operators of Relational Algebra Duplicate Elimination

The duplicate-elimination operator δ Turns a bag into set. Eliminate all but one copy of each tuple. Relation R AB

Apply the duplication eliminator to R δ(R) AB ( δ is the Greek letter Delta)

5.2 Extended Operators of Relational Algebra Aggregation Operators

Aggregation operators Aggregation operators apply to attributes (columns ) of relations. Example of aggregation operators are sums and averages.

Example of aggregation operators Relation R AB SUM(B)= =10 2.AVG(A)=( )/4 3.MIN(A)=1 4.MAX(B)=4 5.COUNT(A)=4  number of elements in A

5.2 Extended Operators of Relational Algebra Grouping

Grouping Grouping of tuples according to their value in one or more attributes has the effect of partitioning the tuples of a relation into groups.

Example of grouping Studio nameLength Disney123 MGM345 Century fox678 Century fox900 MGM23 Suppose we use the aggregation, sum(length). This aggregation will give us the sum of the whole column.

Example of grouping continue but suppose we want to know the total umber of minutes of movies produced by each studio. Then we must have sub tables within the table. Each sub table represent a studio. We will do that by grouping by studio name. Now we can apply the aggregation operator sum( length) to each group.

Example of grouping continue Studio nameLength MGM23 MGM345 Century fox678 Century fox900 Disney123 Now the table is grouped by studio name and we can apply the aggregation operator sum(length)

5.2 Extended Operators of Relational Algebra The Grouping Operator

The grouping operator ϒ Given the schema StarsIn(title, year, StarName) We would like to find the starName of each star who appeared in at least three movies and earliest year in which they appear. How can we approach this problem?

Grouping operator continue We must first group by StarName. It is very intuitive. We want to partition the table into stars and then we can do all the tests for each star In relational algebra we write ϒ StarName Bellow is the table grouped by starName Group by

MOVIETITLEMOVIEYEARSTARNAME Blood Diamond2006Leonardo Dicaprio The Quick and the Dead1995Leonardo Dicaprio Titanic1997Leonardo Dicaprio The Departed2006 Leonardo Dicaprio Body of lies2008Leonardo Dicaprio Inception2010Leonardo Dicaprio Somersault2004Samuel Henry Macbeth2006Samuel Henry Love my Way2006Samuel Henry The Great Raid2005Samuel Henry Terminator Salvation2009Samuel Henry Avatar2009Samuel Henry Perseus2010Samuel Henry Autumn in2000Vera A Farmiga Dust2001Vera A Farmiga Mind the Gap2004 Vera A Farmiga

Notice that in the above table there are three groups one for each Star. Now for each group we are interested in the first year in which the Star appeared, and we would like to know if he played in 3 or more movies. We will use the aggregations min(year) and count(title)>=3

The grouping operator continue How these aggregations works? In each group separately we look for the min year In each group we look for the number of titles in this group. If the number of titles in a group is grate then 3, then this will be sent to the output otherwise this group is eliminated.

Final statement in the grouping operator ϒ starName, min(year)->minYear, count(title)->ctTitle (StarsIn) Group by Then Find the minimum of each group Count the number of title in each group

Final statement in the grouping operator  starName (  ctTitle>3 (ϒ starName, min(year)->minYear, count(title)->ctTitle (StarsIn))) See Fig 5.5 for tree expresion

Final statement in the grouping operator  starName (  ctTitle>3 (ϒ starName, min(year)->minYear, count(title)->ctTitle (StarsIn)))

5.2 Extended Operators of Relational Algebra Extending the Projection Operator

Extending the projection operator We can include, renaming and arithmetic operators in projection. Example: π A, B+C-->X Projection Of attribute A AndAdd the value in B and C Rename it to X

Extending the projection operator continue Relation R ABC Relation S AX B+C=X

5.2 Extended Operators of Relational Algebra The Sorting Operator τ The sorting operator τ turns a relation into a list of tuples, sorted according to one or more attributes.

External Merge Sort How do you sorting 4000 students using only one class room (can hold only 40 students) 1.Fill in the class room with 40 students, let them line up alphabetically 2.So we have 100 sorted group 3.Line up two groups in front of class room 4.One of the two “head” students will walk into class room and sit at the first seat.

External Merge Sort 1.Once the 40 seats are full, let them go out. 2.Student continue to walk int until all sets are occupied. 3.Move then out, now we have a group of sorted students. 4.Continue...

5.2 Extended Operators of Relational Algebra Outerjoins

Outer join Youtube link 7M 7M

Outerjoins ABC BCD

Simple outer join First find all tuple that agree and pair them. Notice that unlike product the tuples that matches do not repeat. Next we deal with tuples that do not agree. We call these dangling tuples. Add the dangling tuples but what ever is messing add null. example:

ABC BCD If I was doing natural join I would be done here. These are the only matching tuples

ABC BCD But what about the dangling tuples. In the outer join we have to account for them too

Outer joins ABCD Null

Left outer join The easier way of thinking of it is that we must keep all the tuples from the left relation.

ABC BCD

Example of left outer join Step one do normal join. That is write all the tuples that pair correctly. ABCD

Left outer join Next look at the left relation and see that second and third tuples were not used. We must use them ABCD NULL 789 I have use the left relation fully

ABC BCD

Example of right outer join Step one do natural join. That is write all the tuples that pair correctly. Same exact step as the left outer join. I actually copied and paste it. ABCD

Example right outer join continue Now look for the tuple that were not used in the right relation. That is the third tuple. Add this tuple to complete the right outer join. ABCD NULL678

5.2 Extended Operators of Relational Algebra Exercises for Section Show the commutate law for Cartesian Product by example in pp Exercise (a), (b)

5.3 A Logic for Relations Predicates and Atoms

Relational Algebra Thanks

5.3 A Logic for Relations Arithmetic Atoms

5.3 A Logic for Relations Datalog Rules and Queries

5.3 A Logic for Relations Meaning of Datalog Rules

5.3.5 Extensional and Intensional Predicates

5.3 A Logic for Relations Datalog Rules Applied to Bags

5.3 A Logic for Relations Exercises for Section 5.3

5.4 Relational Algebra and Datalog Boolean Operations

5.4 Relational Algebra and Datalog Projection

5.4 Relational Algebra and Datalog Selection

5.4 Relational Algebra and Datalog Product

5.4 Relational Algebra and Datalog Joins

5.4 Relational Algebra and Datalog Simulating Multiple Operations with Datalog

5.4 Relational Algebra and Datalog Comparison Between Datalog and Relational Algebra

5.4 Relational Algebra and Datalog Exercises for Section 5.4

5.5 Summary of Chapter

5 5.6 References for Chapter 5