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

Slides:



Advertisements
Similar presentations
Relational Algebra Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Advertisements

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.
RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science.
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.
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.
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.
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.
RELATIONAL ALGEBRA Prof. Sin-Min LEE Department of Computer Science.
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.
Translation of ERD to Relational Scheme, Relational Algebra Prof. Sin-Min LEE Department of Computer Science.
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.
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.
Advanced Relational Algebra & SQL (Part1 )
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).
Relational Algebra p BIT DBMS II.
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.
Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
RELATIONAL ALGEBRA (II)
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Database Systems (資料庫系統)
CS4432: Database Systems II
Relational Algebra Chapter 4, Part A
Database Systems 10/13/2010 Lecture #4.
Relational Algebra.
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4, Sections 4.1 – 4.2
CENG 351 File Structures and Data Managemnet
Presentation transcript:

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

Database Scheme A relational database scheme, or schema, corresponds to a set of table definitions. Eg: product(p_id, name, category, description) supply(p_id, s_id, qnty_per_month) supplier(s_id, name, address, ph#) * remember the difference between a DB instance and a DB scheme.

SAMPLE SCHEMAS AND INSTANCES The Schemas: Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date) The Instances:

What is Relational Algebra? zRelational algebra is a procedural query language. zIt consists of the select, project, union, set difference, Cartesian product, and rename operations. zSet intersection, division, natural join, and assignment combine the fundamental operations. zSQL is based on relational algebra

zIt is an abstract language. We use it to express the set of operations that any relational query language must perform. zTwo types of operations: z 1.set-theoretic operations: tables are essentially sets of rows  2.native relational operations: focus on the structure of the rows Query languages are specialized languages for asking questions,or queries,that involve the data in database. What are the query languages ?

Query languages zprocedural vs. non-procedural zcommercial languages have some of both zwe will study: yrelational algebra ( which is procedural, i.e. tells you how to process a query ) yrelational calculus ( which is non-procedural i.e. tells what you want )

SEMANTICS OF THE SAMPLE RELATIONS zSailors: Entity set; lists the relevant properties of sailors. zBoats zBoats: Entity set; lists the relevant properties of boats. zReserves: Relationship set: links sailors and boats by describing the boat number and date for which a sailor made a reservation. Example of the declarative sentences for which rows stand: Row 1: “Sailor ’22’ reserved boat number ‘101’ on 10/10/98”.

Selection and Projection zSelection Operator : σ rating>8 (S2) Retrieves from the current instance of relation named S2 those rows where the value of the attribute ‘rating’ is greater than 8. Applying the above selection operator to the sample instance of S2 shown in figure 4.2 yields the relational instance on figure 4.4 as shown below: z π condition

Projection Operator π sname,rating (S2) Retrieves from the current instance of the relation named S2 those columns whose names are ‘sname’ and ‘rating’. Applying the above operator to the sample instance of S2 shown in figure 4.2 yields the relational instance on figure 4.5 as shown below: N. B.: Note that the projection operator can produce duplicate rows in the resulting instance.

- Projection Operator (cont’d) Similarly π age (S2) yields the following relational instance Note here the elimination of duplicates SQL would yield For π age (S2): age

Introduction z one of the two formal query languages of the relational model z collection of operators for manipulating relations z Operators: two types of operators y Set Operators: Union(  ),Intersection(  ), Difference(-), Cartesian Product (x)  New Operators: Select (  ), Project (  ), Join ( ⋈ )

Introduction – cont’d z A Relational Algebra Expression: a sequence of relational algebra operators and operands (relations), formed according to a set of rules. z The result of evaluating a relational algebra expression is a relation.

Selection z Denoted by  c ( R ) z Selects the tuples (rows) from a relation R that satisfy a certain selection condition c. z It is a unary operator z The resulting relation has the same attributes as those in R.

Example 1: SNOSNAMEAGESTATE S1MIKE21IL S2STEVE20LA S3MARY18CA S4MING19NY S5OLGA21NY S:  state=‘IL’ (S)

Example 2: CNOCNAMECREDITDEPT C1 Databas e 3CS C2 Statistic s 3MATH C3Tennis1SPORTS C4Violin4MUSIC C5Golf2SPORTS C6Piano5MUSIC C:  CREDIT  3 (C)

Example 3 SNOCNOGrade S1C190 S1C280 S1C375 S1C470 S1C5100 S1C660 S2C190 S2C280 S3C290 S4C280 S4C485 S4C5100 E:  SNO=‘S1’and CNO=‘C1’ (E)

Selection - Properties z Selection Operator is commutative  C1 (  C2 (R)) =  C2 (  C1 (R)) z The Selection is an unary operator, it cannot be used to select tuples from more than one relations.

Projection z Denoted by  L (R), where L is list of attribute names and R is a relation name or some other relational algebra expression. z The resulting relation has only those attributes of R specified in L. z The projection is also an unary operation.  Duplicate rows are not permitted in relational algebra. Duplication is removed from the result. zDuplicate rows can occur in SQL, though they may be controlled by explicit keywords.

Projection - Example z Example 1:  STATE (S) SNOSNAMEAGESTATE S1MIKE21IL S2STEVE20LA S3MARY18CA S4MING19NY S5OLGA21NY STATE IL LA CA NY

Projection - Example Example 2:  CNAME, DEPT (C) CNOCNAMECREDITDEPT C1 Databas e 3CS C2 Statistic s 3MATH C3Tennis1SPORTS C4Violin4MUSIC C5Golf2SPORTS C6Piano5MUSIC CNAMEDEPT Databas e CS Statistic s MATH TennisSPORTS ViolinMUSIC GolfSPORTS PianoMUSIC

Projection - Example Example 3:  S# (  STATE=‘NY' (S)) SNOSNAMEAGESTATE S1MIKE21IL S2STEVE20LA S3MARY18CA S4MING19NY S5OLGA21NY SNO S4 S5

SET Operations z UNION: R 1  R 2 z INTERSECTION: R 1  R 2 z DIFFERENCE: R 1 - R 2 z CARTESIAN PRODUCT: R 1  R 2

Union Compatibility z For operators , , -, the operand relations R1(A1, A2,..., An) and R2(B1, B2,..., Bn) must have the same number of attributes, and the domains of the corresponding attributes must be compatible; that is, dom(Ai)=dom(Bi) for i=1,2,...,n. z The resulting relation for , , or - has the same attribute names as the first operand relation R1 (by convention).

Union Compatibility - Examples z Are S(SNO, SNAME, AGE, STATE) and C(CNO, CNAME, CREDIT, DEPT) union compatible? z Are S(S#, SNAME, AGE, STATE) and C(CNO, CNAME, CREDIT_HOURS, DEPT_NAME) union compatible?

FUNCTIONAL APPLICATION OF SELECTION AND PROJECTION OPERATORS The selection and projection operators can be applied successively as many times as desired in the usual functional denotation as illustrated below. Thus the expression π sname,rating (σ rating>8 (S2)) yields the following relational instance

SET OPERATIONS The following set operations are also available in relational algebra: zUnion* zIntersection* zSet-difference* zCross-product N.B. (1) The asterisks indicate operations whose operand relations must be union-compatible. Two relation instances are said to be union- compatible if: - they have the same number of fields, - corresponding fields have the same domains. - they have the same semantics. (2) The results of set operations on sets and multisets are different, therefore we shall examine both of these separately.

EXAMPLES OF SET OPERATIONS ON RELATIONS zUnion: Given the sample instances S1 and S2 The union of S1 and S2, i.e. S1 ∪ S2 is shown below

zGiven the two sample relational instances We can form: Intersection: S1 ∩S2 Set-Difference: S1 – S2

Given the two relational samples S1 and S2 We can form the Cross-product S1  R1: # of col. = (# col. S1) +( # col. R1) # of rows = (# rows S1)  (# rowsR1)

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, and perhaps best approached through examples as will be done here.

EXAMPLES OF DIVISION

DIVISION Interpretation of the division operation A/B: - Divide the attributes of A into 2 sets: A1 and A2. - Divide the attributes of B into 2 sets: B2 and B3. - Where the sets A2 and B2 have the same attributes. - For each set of values in B2: - Search in A2 for the sets of rows (having the same A1 values) whose A2 values (taken together) form a set which is the same as the set of B2’s. - For all the set of rows in A which satisfy the above search, pick out their A1 values and put them in the answer.

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)

Union, Intersection, Difference z T= R U S : A tuple t is in relation T if and only if t is in relation R or t is in relation S z T = R  S: A tuple t is in relation T if and only if t is in both relations R and S z T= R - S :A tuple t is in relation T if and only if t is in R but not in S

Set-Intersection  Denoted by the symbol .  Results in a relation that contains only the tuples that appear in both relations. zR  S = R – (R – S) zSince set-intersection can be written in terms of set-difference, it is not a fundamental operation.

Examples A1A2 1Red 3White 4green B1B2 3White 2Blue RS

Examples A1A2 1Red 3White 4Green 2Blue A1A2 3White R  S R  S S - R B1B2 2Blue A1A2 1Red 4Green R - S