RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science.

Slides:



Advertisements
Similar presentations
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra) Query Formulation Exercise.
Advertisements

COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5A Relational Algebra.
Relational Algebra Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
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.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
SQL and Relational Algebra Zaki Malik September 02, 2008.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
FALL 2004CENG 351 File Structures and Data Managemnet1 Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Lecture 4: Relational algebra
CS 4432query processing1 CS4432: Database Systems II.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
RELATIONAL ALGEBRA (III) Prof. Sin-Min LEE Department of Computer Science.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Introduction to Database Systems 1 Relational Algebra Relational Model: Topic 3.
RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra.
Relational Algebra, R. Ramakrishnan and J. Gehrke (with additions by Ch. Eick) 1 Relational Algebra.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
CS 370 Database Systems Lecture 11 Relational Algebra.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Relational Algebra  Souhad M. Daraghma. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational.
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 CS122A: Introduction to Data Management Lecture #7 Relational Algebra I Instructor: Chen Li.
Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
RELATIONAL ALGEBRA (II)
Database Systems (資料庫系統)
CS4432: Database Systems II
Relational Algebra Chapter 4, Part A
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Database Systems 10/13/2010 Lecture #4.
Relational Algebra.
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 - part I.
Relational Algebra Chapter 4, Sections 4.1 – 4.2
CENG 351 File Structures and Data Managemnet
Relational Algebra Chapter 4 - part I.
Presentation transcript:

RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science

Unary Relational Operations: SELECT and PROJECT zThe PROJECT Operation zSequences of Operations and the RENAME Operation zThe SELECT Operation

Relational Algebra Operations from Set Theory zThe UNION, INTERSECTION, and MINUS Operations

Binary Relational Operations: JOIN and DIVISION zThe JOIN Operation zThe EQUIJOIN and NATURAL JOIN Variations of JOIN zA Complete Set of Relational Algebra Operations zThe DIVISION Operation

Additional Relational Operations zAggregate Functions and Grouping zRecursive Closure Operations zOUTER JOIN Operations zThe OUTER JOIN Operation

SPECIAL RELATIONAL OPERATORS The following operators are peculiar to relations: - Join operators There are several kind of join operators. We only consider three of these here (others will be considered when we discuss null values): - (1) Condition Joins - (2) Equijoins - (3) Natural Joins - Division

JOIN OPERATORS Condition Joins: - Defined as a cross-product followed by a selection: R ⋈ c S = σ c (R  S) ( ⋈ is called the bow-tie) where c is the condition. - Example: Given the sample relational instances S1 and R1 The condition join S ⋈ S1.sid<R1.sid R1 yields

Equijoin: Special case of the condition join where the join condition consists solely of equalities between two fields in R and S connected by the logical AND operator ( ∧ ). Example: Given the two sample relational instances S1 and R1 The operator S1 R.sid=Ssid R1 yields

Natural Join - Special case of equijoin where equalities are implicitly specified on all fields having the same name in R and S. - The condition c is now left out, so that the “bow tie” operator by itself signifies a natural join. - N. B. If the two relations have no attributes in common, the natural join is simply the cross-product.

DIVISION - The division operator is used for queries which involve the ‘all’ qualifier such as “Find the names of sailors who have reserved all boats”. - The division operator is a bit tricky to explain.

EXAMPLES OF DIVISION

DIVISION Example: Find the names of sailors who have reserved all boats: (1) A =  sid,bid (Reserves). A1 =  sid (Reserves) A2 =  bid (Reserves) (2) B2 =  bid (Boats) B3 is the rest of B. Thus, B2 ={101, 102, 103, 104} (3)Find the rows of A such that their A.sid is the same and their combined A.bid is the set B2. Thus we find A1 = {22} (4) Get the set of A2 corresponding to A1: A2 = {Dustin}

FORMAL DEFINITION OF DIVISION The formal definition of division is as follows: A/B =  x (A) -  x ((  x (A)  B) – A)

EXAMPLES OF ALGEBRA QUERIES In the rest of this chapter we shall illustrate queries using the following new instances S3 of sailors, R2 of Reserves and B1 of boats.

QUERY Q1 Given the relational instances: (Q1) Find the names of sailors who have reserved boat 103  sname ((σ bid=103 Reserves) ⋈ Sailors) The answer is thus the following relational instance {,, }

QUERY Q1 (cont’d) There are of course several ways to express Q1 in relational algebra. Here is another:  sname (σ bid=103 (Reserves ⋈ Sailors)) Which of these expressions should we use? That is a question of optimization. Indeed, when we describe how to state queries in SQL, we can leave it to the optimizer in the DBMS to select the nest approach.

QUERY Q2 (Q2) Find the names of sailors who have reserved a red boat.  sname ((σ color=‘red’ Boats) ⋈ Reserves ⋈ Sailors)

QUERY Q3 (Q3) Find the colors of boats reserved by Lubber.  color ((σ sname=‘Lubber’Sailors )Sailors ⋈ Reserves ⋈ Boats)

QUERY Q4 (Q4) Find the names of Sailors who have reserved at least one boat  sname (Sailors ⋈ Reserves)

QUERY Q5 (Q5) Find the names of sailors who have reserved a red or a green boat.  (Tempboats, (σ color=‘red’ Boats) ∪ (σ color=‘green’ Boats))  sname (Tempboats ⋈ Reserves ⋈ Sailors)

QUERY Q6 (Q6) Find the names of Sailors who have reserved a red and a green boat. It seems tempting to use the expression used in Q5, replacing simply ∪ by ∩. However, this won’t work, for such an expression is requesting the names of sailors who have requested a boat that is both red and green! The correct expression is as follows:  (Tempred,  sid ((σ color=‘red’ Boats) ⋈ Reserves))  (Tempgreen,  sid ((σ color=‘green’ Boats) ⋈ Reserves))  sname ((Tempred ∩ Tempgreen) ⋈ Sailors)

QUERY Q7 (Q7) Find the names of sailors who have reserved at least two boats.  (Reservations,  sid,sname,bid (Sailors ⋈ Reserves))  (Reservationpairs(1  sid1, 2  sname, 3  bid1, 4  sid2, 5  sname, 6  bid2), Reservations  Reservations)  sname1 σ (sid1=sid2)  (bid1  bid2) Reservationpairs)

QUERY 8 (Q8) Find the sids of sailors with age over 20 who have not reserved a red boat.  sid (σ age>20 Sailors) -  sid ((σ color=‘red’ Boats) ⋈ Reserves ⋈ Sailors)

QUERY 9 (Q) Find the names of sailors who have reserved all boats.  (Tempsids, (  sid,bid Reserves) / (  bidBoats ))  sname (Tempsids ⋈ Sailors

QUERY Q10 (Q10) Find the names of sailors who have reserved all boats called Interlake.  (Tempsids, (  sid,bid Reserves)/(  bid (σ bname=‘Interlake’ Boats)))  sname (Tempsids ⋈ Sailors)

Cartesian Product z R(A 1, A 2,..., A m ) and S(B 1, B 2,..., B n ) z T(A 1, A 2,..., A m, B 1, B 2,..., B n ) = R(A 1, A 2,..., A m ) X S(B 1, B 2,..., B n ) z A tuple t is in T if and only if t[A 1,A 2,...,A m ] is in R and t[B 1, B 2,..., B n ] is in S. -If R has N 1 tuples and S has N 2 tuples, then T will have N 1 *N 2 tuples.

Cartesian Product AB a12 a24 R S Rx S CDE 4d1e1 3d2e1 5d3e2 ABCDE a124d1e1 a123d2e1 a125d3e2 a244d1e1 a243d2e1 a245d3e2

Examples of Queries in Relational Algebra

Natural-Join zDenoted by |x|. zBinary operation zCreates a Cartesian-product of the arguments then performs selection to force equality on attributes that appear in both relations

Division zDenoted by  zBinary Operation zUsed in queries that include the phrase “for all”.

Division (Cont’d) zDivision is an operation on schema R – S zA tuple t is in r  s if and only if: zt is in Π R – S (r) and zFor every tuple t s in s, there is a tuple t r in r satisfying both of the following: a. t r [S] = t s [R] b. t r [R – S] = t

Relational Algebra Fundamental operators  select   project  zcartesian product  zunion  zset difference - Other operators znatural join JOIN (butterfly symbol) zset intersection  zdivision 

A Simple DB account ac# owner ss# balance 1 bob sue jane transaction t# ac# type amount outcome date 1 1 W 1500 bounced 5/1/ D 1000 ok 5/2/ W 100 ok 5/4/ D 500 ok 5/7/ W 200 ok 5/9/98 account had transaction

Select eg:  balance>=1500 account result : ac# owner ss# balance 2 sue jane Project eg: π owner, ss# account result: owner ss# bob 123 sue 456 jane 789

Cartesian product eg: account  transaction ythis will have 15 rows like the ones shown below: ac# owner ss# balance t# type amount outcome date 1 bob W 1500 bounced 5/1/98 2 sue D 1000 ok 5/2/98 …………… Composing operations eg: “show all transactions done by account owner Bob”. σ account.ano= transaction.ano ((  owner=“Bob” account)  transaction)

zNatural Join - combines σ, π,  - very commonly used Natural Join forms the cross product of its two arguments, does a selection to enforce equality of columns with the same name and removes duplicate columns. Eg: “show all transactions done by account owner Bob” σ owner=“Bob” (account JOIN transaction)

Rename operation What if you need to access the same relation twice in a query? eg. person(ss#, name, mother_ss#, father_ss#) “Find the name of Bob’s mother” needs the “person” table to be accessed twice. The operation ρ x (r) evaluates to a second logical copy of relation r renamed to x.

Rename operation (contd) eg: π mother.name ( (ρ mother (person)) JOIN mother.ss# = person.mother_ss# (  name=“Bob ” (person)))