1 Relational Algebra Operators Expression Trees Bag Model of Data Source: Slides by Jeffrey Ullman.

Slides:



Advertisements
Similar presentations
1 Relational Algebra Basic Operations Algebra of Bags.
Advertisements

IS698: Database Management Min Song IS NJIT. The Relational Data Model.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Database Systems.
1 Relational Algebra 1 Basic Operations. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which new values.
1 Relational Algebra* and Tuple Calculus * The slides in this lecture are adapted from slides used in Standford's CS145 course.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Algebra Fall 2014.
Chapter 3 : Relational Model
1 Relational Algebra Operators Expression Trees. 2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which.
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Relational Algebra Jermaine Rodney. What is an “Algebra”  Mathematical system consisting of: Operands --- Variables or values from which new values can.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
1 More SQL Database Modification Defining a Database Schema Views Source: slides by Jeffrey Ullman.
1 Introduction to SQL Select-From-Where Statements Subqueries Grouping and Aggregation Source: slides by Jeffrey Ullman.
1 Functional Dependencies Meaning of FD’s Keys and Superkeys Inferring FD’s Source: slides by Jeffrey Ullman.
1 Relational Algebra Basic operators Relational algebra expression tree.
1 Relational Algebra Operators Expression Trees Bag Model of Data.
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
Fundamentals/ICY: Databases 2012/13 WEEK 11 (relational operators & relational algebra) John Barnden Professor of Artificial Intelligence School of Computer.
Relational Algebra Basic Operations Algebra of Bags.
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:
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
RELATIONAL DATA MODEL 1. 2 What is a Data Model? 1.Mathematical representation of data. wExamples: relational model = tables; semistructured model = trees/graphs.
Relational Algebra A presentation for CS 457 By Dawn Haddan.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Algebra (Chapter 7)
The Relational Algebra and Calculus
From Relational Algebra to SQL CS 157B Enrique Tang.
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
SCUHolliday - coen 1785–1 Schedule Today: u Relational Algebra. u Read Chapter 5 to page 199. Next u SQL Queries. u Read Sections And then u Subqueries,
Advanced Relational Algebra & SQL (Part1 )
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
1 Lecture 6 Introduction to SQL part 4 Slides from
Relational Algebra BASIC OPERATIONS 1 DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA.
Relational Algebra Database Systems.
1 Relational Algebra Basic Operations Algebra of Bags.
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:
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Basic Operations Algebra of Bags
CPSC-310 Database Systems
Module 2: Intro to Relational Model
COP4710 Database Systems Relational Algebra.
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
Relational Algebra - Part 1
Relational Algebra at a Glance
Chapter 2: Intro to Relational Model
Relational Databases The Relational Model.
Operators Expression Trees Bag Model of Data
Relational Databases The Relational Model.
Basic Operations Algebra of Bags
Chapter 2: Intro to Relational Model
Schedule Today: Next And then Relational Algebra.
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
Presentation transcript:

1 Relational Algebra Operators Expression Trees Bag Model of Data Source: Slides by Jeffrey Ullman

2 What is an “Algebra” uMathematical system consisting of: wOperands --- variables or values from which new values can be constructed. wOperators --- symbols denoting procedures that construct new values from given values.

3 What is Relational Algebra? uAn algebra whose operands are relations or variables that represent relations. uOperators are designed to do the most common things that we need to do with relations in a database. wThe result is an algebra that can be used as a query language for relations.

4 Roadmap uThere is a core relational algebra that has traditionally been thought of as the relational algebra. uBut there are several other operators we shall add to the core in order to model better the language SQL --- the principal language used in relational database systems.

5 Core Relational Algebra uUnion, intersection, and difference. wUsual set operations, but require both operands have the same relation schema. uSelection: picking certain rows. uProjection: picking certain columns. uProducts and joins: compositions of relations. uRenaming of relations and attributes.

6 Selection uR1 := SELECT C (R2) wC is a condition (as in “if” statements) that refers to attributes of R2. wR1 is all those tuples of R2 that satisfy C.

7 Example Relation Sells: storecandyprice 7-11Twizzlers Kitkat2.75 KrogerTwizzlers2.50 KrogerKitkat Menu := SELECT store=“7-11” (Sells): storecandyprice 7-11Twizzlers Kitkat2.75

8 Projection uR1 := PROJ L (R2) wL is a list of attributes from the schema of R2. wR1 is constructed by looking at each tuple of R2, extracting the attributes on list L, in the order specified, and creating from those components a tuple for R1. wEliminate duplicate tuples, if any.

9 Example Relation Sells: storecandyprice 7-11Twizzlers Kitkat2.75 KrogerTwizzlers2.50 KrogerKitkat3.00 Prices := PROJ candy,price (Sells): candyprice Twizzlers2.50 Kitkat2.75 Kitkat3.00

10 Product uR3 := R1 * R2 wPair each tuple t1 of R1 with each tuple t2 of R2. wConcatenation t1t2 is a tuple of R3. wSchema of R3 is the attributes of R1 and then R2, in order. wBut beware attribute A of the same name in R1 and R2: use R1.A and R2.A.

11 Example: R3 := R1 * R2 R1(A,B ) R2(B,C ) R3(A,R1.B,R2.B,C )

12 Theta-Join uR3 := R1 JOIN C R2 wTake the product R1 * R2. wThen apply SELECT C to the result. uAs for SELECT, C can be any boolean- valued condition. wHistoric versions of this operator allowed only A  B, where  is =, <, etc.; hence the name “theta-join.”

13 Example Sells(store,candy,price ) Stores(name, addr ) 7-11Twiz Maple St. 7-11Kitkat2.75Kroger River Rd. KrogerTwiz.2.50 KrogerPez3.00 StoreInfo := Sells JOIN Sells.store = Stores.name Stores StoreInfo(store,candy,price,name,addr ) 7-11Twiz Maple St. 7-11Kitkat Maple St. KrogerTwiz.2.50KrogerRiver Rd. KrogerPez3.00KrogerRiver Rd.

14 Natural Join uA frequent type of join connects two relations by: wEquating attributes of the same name, and wProjecting out one copy of each pair of equated attributes. uCalled natural join. uDenoted R3 := R1 JOIN R2.

15 Example Sells(store,candy,price )Stores(store,addr ) 7-11Twiz Maple St. 7-11Kitkat2.75KrogerRiver Rd. KrogerTwiz.2.50 KrogerPez3.00 StoreInfo := Sells JOIN Stores Note Stores.name has become Stores.store to make the natural join “work.” StoreInfo(store,candy,price,addr ) 7-11Twiz.2.50Maple St. 7-11Kitkat2.75Maple St. KrogerTwiz.2.50River Rd. KrogerPez3.00River Rd.

16 Renaming uThe RENAME operator gives a new schema to a relation. uR1 := RENAME R1(A1,…,An) (R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2. uSimplified notation: R1(A1,…,An) := R2.

17 Example Stores(name, addr ) 7-11Maple St. KrogerRiver Rd. R(store, addr ) 7-11Maple St. KrogerRiver Rd. R(store, addr) := Stores

18 Building Complex Expressions uCombine operators with parentheses and precedence rules. uThree notations, just as in arithmetic: wSequences of assignment statements. wExpressions with several operators. wExpression trees.

19 Sequences of Assignments uCreate temporary relation names. uRenaming can be implied by giving relations a list of attributes. uExample: R3 := R1 JOIN C R2 can be written: R4 := R1 * R2 R3 := SELECT C (R4)

20 Expressions in a Single Assignment uExample: the theta-join R3 := R1 JOIN C R2 can be written R3 := SELECT C (R1 * R2) uPrecedence of relational operators: w[SELECT, PROJECT, RENAME] (highest). w[PRODUCT, JOIN]. wINTERSECTION. w[UNION, --]

21 Expression Trees uLeaves are operands --- either variables standing for relations or particular, constant relations. uInterior nodes are operators, applied to their child or children.

22 Example uUsing the relations Stores(name, addr) and Sells(store, candy, price), find the names of all the stores that are either on Maple St. or sell Twizzlers for less than $3.

23 As a Tree: Stores(name,addr)Sells(store,candy,price) SELECT addr = “Maple St.” SELECT price<3 AND candy=“Twiz.” PROJECT name RENAME R(name) PROJECT store UNION

24 As a Sequence of Assignments R1(name) := PROJECT name (SELECT addr="Maple" (Stores)) R2(name) := PROJECT store (SELECT price<3 AND candy="Twiz." (Sells)) R3 := R1 UNION R2

25 Example uUsing Sells(store, candy, price), find the stores that sell two different candies at the same price. uStrategy: by renaming, define a copy of Sells, called S(store, candy1, price). The natural join of Sells and S consists of quadruples (store, candy, candy1, price) such that the store sells both candies at this price.

26 The Tree Sells(store,candy,price) RENAME S(store, candy1, price) JOIN PROJECT store SELECT candy != candy1

27 Schemas for Results uUnion, intersection, and difference: the schemas of the two operands must be the same, so use that schema for the result. uSelection: schema of the result is the same as the schema of the operand. uProjection: list of attributes tells us the schema.

28 Schemas for Results --- (2) uProduct: schema is the attributes of both relations. wUse R.A, etc., to distinguish two attributes named A. uTheta-join: same as product. uNatural join: union of the attributes of the two relations. uRenaming: the operator tells the schema.

29 Relational Algebra on Bags uA bag (or multiset ) is like a set, but an element may appear more than once. uExample: {1,2,1,3} is a bag. uExample: {1,2,3} is also a bag that happens to be a set.

30 Why Bags? uSQL, the most important query language for relational databases, is actually a bag language. uSome operations, like projection, are much more efficient on bags than sets.

31 Operations on Bags uSelection applies to each tuple, so its effect on bags is like its effect on sets. uProjection also applies to each tuple, but as a bag operator, we do not eliminate duplicates. uProducts and joins are done on each pair of tuples, so duplicates in bags have no effect on how we operate.

32 Example: Bag Selection R(A,B ) SELECT A+B<5 (R) =AB 12

33 Example: Bag Projection R(A,B ) PROJECT A (R) =A 1 5 1

34 Example: Bag Product R(A,B )S(B,C ) R * S =AR.BS.BC

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

36 Bag Union uAn element appears in the union of two bags the sum of the number of times it appears in each bag. uExample: {1,2,1} UNION {1,1,2,3,1} = {1,1,1,1,1,2,2,3}

37 Bag Intersection uAn element appears in the intersection of two bags the minimum of the number of times it appears in either. uExample: {1,2,1,1} INTER {1,2,1,3} = {1,1,2}.

38 Bag Difference uAn 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. wBut never less than 0 times. uExample: {1,2,1,1} – {1,2,3} = {1,1}.

39 Beware: Bag Laws != Set Laws uSome, but not all algebraic laws that hold for sets also hold for bags. uExample: the commutative law for union (R UNION S = S UNION R ) does hold for bags. wSince addition is commutative, adding the number of times x appears in R and S doesn’t depend on the order of R and S.

40 Example of the Difference uSet union is idempotent, meaning that S UNION S = S. uHowever, for bags, if x appears n times in S, then it appears 2n times in S UNION S. uThus S UNION S != S in general.

41 The Extended Algebra uDELTA = eliminate duplicates from bags. uTAU = sort tuples. uExtended projection : arithmetic, duplication of columns. uGAMMA = grouping and aggregation. uOuterjoin : avoids “dangling tuples” = tuples that do not join with anything.

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

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

44 Sorting uR1 := TAU L (R2). wL is a list of some of the attributes of R2. uR1 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. wBreak ties arbitrarily. uTAU is the only operator whose result is neither a set nor a bag.

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

46 Extended Projection uUsing the same PROJ L operator, we allow the list L to contain arbitrary expressions involving attributes, for example: wArithmetic on attributes, e.g., A+B. wDuplicate occurrences of the same attribute.

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

48 Aggregation Operators uAggregation operators are not operators of relational algebra. uRather, they apply to entire columns of a table and produce a single result. uThe most important examples: SUM, AVG, COUNT, MIN, and MAX.

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

50 Grouping uSometimes we want to apply an aggregation operator to a subset (or group) of tuples, instead of all of them. uSo we need a way to "group" them, e.g. ABC group by A: ABC group by A and B:

51 Grouping Operator uR1 := GAMMA L (R2). L is a list of elements, each of which is either: wan individual (grouping ) attribute or wAGG(A ), where AGG is one of the aggregation operators and A is an attribute.

52 Applying GAMMA L (R) uGroup R according to all the grouping attributes on list L. wThat is: form one group for each distinct list of values for those attributes in R. uWithin each group, compute AGG(A ) for each aggregation on list L. uResulting relation has one tuple for each group; this tuple contains wthe values of the grouping attributes for the group wthe group’s aggregation values.

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

54 Example: Grouping/Aggregation Sells( store, candy, price ) 7-11 Kroger HEB Twiz. Kitkat Twiz. Pez Kitkat compute GAMMA candy,AVG(price) (Sells) : storecandyprice 7-11 Kroger Twiz HEB Kitkat KrogerPez3.00 candyAVG(price) Twiz. Kitkat Pez

55 Outerjoin uSuppose we join R JOIN C S. uA tuple of R that has no tuple of S with which it joins is said to be dangling. wSimilarly for a tuple of S. uOuterjoin preserves dangling tuples by padding them with a special NULL symbol in the result.

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