Lecture 4: Relational algebra

Slides:



Advertisements
Similar presentations
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5A Relational Algebra.
Advertisements

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.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
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.
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.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
CS 4432query processing1 CS4432: Database Systems II.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
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. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. 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.
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.
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.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
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.
Relational Algebra.
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)
Relational Algebra p BIT DBMS II.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Jianping Fan.
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.
1 Relational Algebra. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational model supports.
Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Relational Algebra & Calculus
Database Systems (資料庫系統)
CS4432: Database Systems II
Relational Algebra Chapter 4 1.
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.
Relational Algebra 1.
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 1.
Relational Algebra Chapter 4 - part I.
Relational Algebra Chapter 4, Sections 4.1 – 4.2
CENG 351 File Structures and Data Managemnet
Relational Algebra & Calculus
Relational Algebra Chapter 4 - part I.
Relational Algebra Chpt 4a Xintao Wu Raghu Ramakrishnan
Presentation transcript:

Lecture 4: Relational algebra www.cl.cam.ac.uk/Teaching/current/Databases/

Today’s lecture What’s the (core) relational algebra? How can we write queries using the relational algebra? How powerful is the relational algebra?

Relational query languages Query languages allow the manipulation and retrieval of data from a database The relational model supports simple, powerful query languages Strong formal foundation Allows for much (provably correct) optimisation NOTE: Query languages are not (necessarily) programming languages

Formal relational query languages Two formal query languages Relational Algebra Simple ‘operational’ model, useful for expressing execution plans Relational Calculus Logical model (‘declarative’), useful for theoretical results Both languages were introduced by Codd in a series of papers They have equivalent expressive power They are the key to understanding SQL query processing!

Preliminaries A query is applied to relation instances, and the result of a query is also a relation instance Schema of relations are fixed (cf. types) The query will then execute over any valid instance The schema of the result can also be determined

Example relation instances A database of boats, sailors, and reservations S2 sid sname rating age 10 Myleene 6 23 22 Tim 8 26 99 Julia 100 20 88 Gavin 21 R1 sid bid day 22 101 101001 99 103 111201 S1 sid sname rating age 11 Sue 7 26 22 Tim 8 33 Bob 9 28 55 Kim 10 B1 bid colour 101 red 102 blue 103 green

Core relational algebra Five basic operator classes: Selection Selects a subset of rows Projection Picking certain columns Renaming Renaming attributes Set theoretic operations The familiar operations: union, intersection, difference, … Products and joins Combining relations in useful ways

Selection Selects rows that satisfy a condition, written R1 = c(R2) where c is a condition involving the attributes of R2, e.g. rating>8(S2) returns the relation instance sid sname rating age 99 Julia 100 20 88 Gavin 21

sname=“Julia”(rating>8(S2)) Selection cont. Note: The schema of the result is exactly the same as the schema of the input relation instance There are no duplicates in the resulting relation instance (why?) The resulting relation instance can be used as the input for another relational algebra operator, e.g. sname=“Julia”(rating>8(S2))

Projection Deletes fields that are not in the projection list R1=A(R2) where A is a list of attributes from the schema of R2, e.g. sname,rating(S2) returns the relation instance sname rating Myleene 6 Tim 8 Julia 100 Gavin

Projection cont. Note: Projection operator has to eliminate duplicates (why?) Aside: Real systems don’t normally perform duplicate elimination unless the user explicitly asks for it (why not?)

Renaming R1= A:=B(R2) Returns a relation instance identical to R2 except that field A is renamed B For example, sname:=nom(S1) sid nom rating age 11 Sue 7 26 22 Tim 8 33 Bob 9 28 55 Kim 10

Familiar set operations We have the familiar set-theoretic operators, e.g. , , - There is a restriction on their input relation instances: they must be union compatible Same number of fields Same field names and domains E.g. S1S2 is valid, but S1R1 is not

Cartesian products AB Concatenate every row of A with every row of B What do we do if A and B have some field names in common? Several choices, but we’ll simply assume that the resulting duplicate field names will have the suffix 1 and 2

Example S1R1 Note! sid.1 sname rating age sid.2 bid day 11 Sue 7 26 22 101 101001 99 103 111201 Tim 8 33 Bob 9 28 55 Kim 10

Theta join Theoretically, it is a derived operator R1 Vc R2 @ c(R1R2) E.g., S1 Vsid.1<=sid.2R1 sid.1 sname rating age sid.2 bid day 11 Sue 7 26 22 101 101001 99 103 111201 Tim 8 33 Bob 9 28 55 Kim 10

Theta join cont. The result schema is the same as for a cross-product Sometimes this operator is called a conditional join Most commonly the condition is an equality on field names, e.g. S1 Vsid.1=sid.2R1

Equi- and natural join Equi-join is a special case of theta join where the condition is equality of field names, e.g. S1 Vsid R1 Natural join is an equi-join on all common fields where the duplicate fields are removed. It is written simply A V B sid.1 sname rating age sid.2 bid day 22 Tim 8 26 101 101001

Natural join cont. Note that the common fields appear only once in the resulting relation instance This operator appears very frequently in real-life queries It is always implemented directly by the query engine (why?)

Find sailors who have reserved all the boats Division Not a primitive operator, but useful to express queries such as Find sailors who have reserved all the boats Consider the simple case, where relation A has fields x and y, and relation B has field y A/B is the set of xs (sailors) such that for every y (boat) in B, there is a row (x,y) in A

Division cont. Can you code this up in the relational algebra?

Division cont. Can you code this up in the relational algebra? x’s that are disqualified: x((x(A)  B) – A) Thus: x(A)-x((x(A)  B) – A)

Find names of sailors who’ve reserved boat 103 Example 1 Find names of sailors who’ve reserved boat 103 Solution 1: sname(bid=103(Reserves) V Sailors) Solution 2: sname(bid=103(Reserves V Sailors)) Which is more efficient? Query optimisation

Find names of sailors who’ve reserved a red boat Example 2 Find names of sailors who’ve reserved a red boat DO THIS ON SLIDE MYSELF

Find names of sailors who’ve reserved a red boat Example 2 Find names of sailors who’ve reserved a red boat sname(colour=“red”(Boats) V Reserves V Sailors) Better: sname(sid(bid(colour=“red”(Boats)) V Reserves) V Sailors) DO THIS ON SLIDE MYSELF

Find sailors who’ve reserved a red or a green boat Example 3 Find sailors who’ve reserved a red or a green boat MARS Bar challenge!!! Need to know that SQL has logical AND and OR operators

Find sailors who’ve reserved a red or a green boat Example 3 Find sailors who’ve reserved a red or a green boat let T = colour=“red”colour=“green”(Boats) in sname(T V Reserves V Sailors) MARS Bar challenge!!! Need to know that SQL has logical AND and OR operators

Find sailors who’ve reserved a red and a green boat Example 4 Find sailors who’ve reserved a red and a green boat Mars Bar challenge

Find sailors who’ve reserved a red and a green boat Example 4 Find sailors who’ve reserved a red and a green boat NOTE: Can’t just trivially modify last solution! let T1 = sid (colour=“red”(Boats) V Reserves) T2 = sid (colour=“green”(Boats) V Reserves) in sname((T1  T2) V Sailors) Mars Bar challenge

Find the names of sailors who’ve reserved at least two boats Example 5 Find the names of sailors who’ve reserved at least two boats let T = sid.1:=sid (sid.1,sname,bid (Sailors V Reserves)) in sname.1 (sid.1=sid.2bid.1bid.2(T  T)) Mars bar challenge

Find the names of sailors who’ve reserved all boats Example 6 Find the names of sailors who’ve reserved all boats let T = sid,bid (Reserves) / bid (Boats) in sname(T V Sailors) But this is pretty much given on the division slides

Computational limitations Suppose we have a relation SequelOf of movies and their immediate sequels We want to compute the relation ‘isFollowedBy’ … movie sequel Naked Gun Naked Gun 2½ Naked Gun 33 1/3 Rocky Rocky II Rocky III Rocky IV Rocky V

Computational limitations We could compute fst,thd(movie:=fst,sequel:=snd(SequelOf) V movie:=snd,sequel:=thd(SequelOf)) This provides us with sequels-of-sequels We could write three joins to get sequels-of- sequels-of-sequels and union the results What about Friday the 13th (9 sequels)?  In general we need to be able to write an arbitrarily large union… The relational algebra needs to be extended to handle these sorts of queries

Summary You should now understand: The core relational algebra Operations and semantics Union compatibility Computational limitations of the relational algebra Next lecture: Relational calculus