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

Slides:



Advertisements
Similar presentations
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
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.
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.
D ATABASE S YSTEMS I A DMIN S TUFF. 2 Mid-term exam Tuesday, Oct 2:30pm Room 3005 (usual room) Closed book No cheating, blah blah No class on Oct.
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.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
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.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
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.
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.
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.
RELATIONAL ALGEBRA and Computer Assignment 1 Prof. Sin-Min LEE Department of Computer Science.
CS 370 Database Systems Lecture 11 Relational Algebra.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
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.
The Relational Algebra and Calculus
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Relational Algebra Operators
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Relational Algebra p BIT DBMS II.
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.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Database Systems (資料庫系統)
Fundamental of Database Systems
Relational Algebra.
Relational Algebra Chapter 4, Part A
RELATIONAL ALGEBRA (Chapter 2)
Database Systems 10/13/2010 Lecture #4.
Relational Algebra.
LECTURE 3: Relational Algebra
CENG 351 File Structures and Data Managemnet
Relational Algebra & Calculus
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?

UNION, SET DIFFERENCE & SET INTERSECT zUnion puts all tuples of two relations in one relation. To use this operator, two conditions must hold: 1.The two relations must be of the same arity. 2.The domain of i th attribute of the two participating relation must be the same. zSet difference operator computes tuples that are in one relation, but not in another. zSet intersect operator computes tuples that are common in two relations: zThe five fundamental operations of the relational algebra are: select, project, cartesian product, Union, and set difference zAll other operators can be constructed using these operators

EXAMPLE zAssume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) zQuery 1: Find the bid of red colored boats:

EXAMPLE zAssume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) zQuery 1: Find the bid of red colored boats: y∏ bid (б color=red (Boats))

EXAMPLE zAssume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) zQuery 1: Find the name of sailors who have reserved Boat number 2.

EXAMPLE zAssume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) zQuery 1: Find the name of sailors who have reserved Boat number 2. y∏ sname (б bid=2 (Sailors (sid) Reserve))

EXAMPLE zAssume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) zQuery 1: Find the name of sailors who have reserved both a red and a green boat.

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

RELATIONAL ALGEBRA (SECTION 3.2)

RELATIONAL ALGEBRA INTRODUCTION zAssume the following two relations: Emp (SS#, name, age, salary, dno) Dept (dno, dname, floor, mgrSS#)  Relational algebra is a procedural query language, i.e., user must define both “ how ” and “ what ” to retrieve. zRelational algebra consists of a set of operators that consume either one or two relations as input. An operator produces one relation as its output. zUnary operators include: select, project, and rename zBinary operators include: cartesian product, equality join, natural join, join, semi-join, division, union, and set difference.

SELECT OPERATOR zSelect (б): selects tuples that satisfy a predicate; e.g., retrieve the employees whose salary is 30,000 б Salary=30,000 (Employee) zConjunctive ( ) and disjunctive ( ) selection predicates are allowed; e.g., retrieve employees whose salary is higher than 30,000 and are younger than 25 years old: б Salary>30,000 age<25 (Employee) zNote that only selection predicates are allowed. A selection predicate is either (1) a comparison (=, ≠, ≤, ≥, ) between an attribute and a constant (e.g., salary = 30,000) or (2) a comparison between two different attributes of the same relation (e.g., salary = age × 100). zNote: This operator is different than the SELECT command of SQL. < < <

EXAMPLE zEmp table: SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh440001

EXAMPLE zEmp table: zб Salary=30,000 (Employee) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh440001

EXAMPLE zEmp table: zб Salary=30,000 (Employee) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh SS#NameAgeSalarydno 4Kathy

EXAMPLE zEmp table: zб Age>22 (Employee) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh440001

EXAMPLE zEmp table: zб Age>22 (Employee) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh SS#NameAgeSalarydno 1Joe Kathy

PROJECT OPERATOR zProject (∏) retrieves a column. It is a unary operator that eliminate duplicates. e.g., name of employees: ∏ name (Employee) e.g., name of employees earning more than 30,000: ∏ name ( б Salary>30,000 (Employee))

EXAMPLE zEmp table: SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh440001

EXAMPLE zEmp table: z∏ age (Emp) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh Age

EXAMPLE zEmp table: z∏ name,age ( б Salary=4000 (Emp) ) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh440001

EXAMPLE zEmp table: z∏ name,age (б Salary=4000 (Emp) ) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh SS#NameAgeSalarydno 5Shideh440001

EXAMPLE zEmp table: z∏ name,age ( б Salary=4000 (Emp) ) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh NameAge Shideh4

CARTESIAN PRODUCT zCartesian Product (R 1 × R 2 ) combines two relations by concatenating their tuples together, evaluating all possible combinations. If the name of a column is identical for two relations, this ambiguity is resolved by attaching the name of each relation to a column. e.g., Emp × Dept y(SS#, name, age, salary, Emp.dno, Dept.dno, dname, floor, mgrSS#) zIf t(Emp) and t(Dept) is the cardinality of the Employee and Dept relations respectively, then the cardinality of Emp × Dept is: t(Emp) × t(Dept)

CARTESIAN PRODUCT (Cont … ) zExample: Emp table: Dept table: 345John Doe2325, Jane Java2528, Joe SQL2232,0001 SS#Nameagesalarydno 1Toy1345 2Shoe2943 dnodnamefloormgrSS#

CARTESIAN PRODUCT (Cont … ) zCartesian product of Emp and Dept: Emp × Dept: 34 5 John Doe 2325, Toy Jane Java 2528, Toy Joe SQL2232, Toy1345 John Doe 2325, Shoe2943 Jane Java 2528, Shoe Joe SQL2232, Shoe2943 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

CARTESIAN PRODUCT zExample: retrieve the name of employees that work in the toy department:

CARTESIAN PRODUCT zExample: retrieve the name of employees that work in the toy department:  ∏ name (б Emp.dno=Dept.dno (Emp × б dname= ‘ toy ’ (Dept)))

CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) 345John Doe2325,00011Toy Jane Java2528,00021Toy Joe SQL2232,00011Toy1345 John Doe2325,00012Shoe2943 Jane Java2528,00022Shoe Joe SQL2232,00012Shoe2943 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) 345John Doe 2325,00011Toy Joe SQL2232,00011Toy Jane Java 2528,00022Shoe2943 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) 345John Doe 2325,00011Toy Joe SQL2232,00011Toy1345 SS#NameagesalaryEmp.dnoDept.dnodnamefloormgrSS#

CARTESIAN PRODUCT (Cont … )  ∏ name (б dname= ‘ toy ’ (б Emp.dno=Dept.dno (Emp × Dept))) John Doe Joe SQL Name

RENAME OPERATOR zRename operator changes the name of its input table to its subscript, yρ e2 (Emp) yChanges the name of Emp table to e2

EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN zEquality join connects tuples from two relations that match on certain attributes. The specified joining columns are kept in the resulting relation.  ∏ name (б dname= ‘ toy ’ (Emp Dept))) zNatural join connects tuples from two relations that match on the specified common attributes  ∏ name (б dname= ‘ toy ’ (Emp Dept))) zHow is an equality join between Emp and Dept using dno different than a natural join between Emp and Dept using dno?  Equality join: SS#, name, age, salary, Emp.dno, Dept.dno, …  Natural join: SS#, name, age, salary, dno, dname, … zJoin is similar to equality join using different comparison operators yA S op = {=, ≠, ≤, ≥, } att op att (dno)

EXAMPLE JOIN zEquality Join, (Emp Dept))) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh EMP dnodnamefloormgrss# 1Toy15 2Shoe21 Dept (dno) SS#NameAgeSalaryEMP.dnoDept.dn o dnamefloormgrss # 1Joe Shoe21 2Mary Toy15 3Bob Toy15 4Kathy Shoe21 5Shideh Toy15

EXAMPLE JOIN zNatural Join, (Emp Dept))) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh EMP dnodnamefloormgrss# 1Toy15 2Shoe21 Dept (dno) SS#NameAgeSalarydnodnamefloormgrss# 1Joe Shoe21 2Mary Toy15 3Bob Toy15 4Kathy Shoe21 5Shideh440001Toy15

EXAMPLE JOIN zJoin, (Emp ρ x (Emp)))) SS#NameAgeSalarydno 1Joe Mary Bob Kathy Shideh EMP dnodnamefloormgrss# 1Toy15 2Shoe21 Dept Salary > 5 * salary SS#NameAgeSalarydnox.SS#x.Namex.Ag e x.Salar y x.dno 2Mary Shideh Bob Shideh Kathy Shideh440001

EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN (Cont … ) zExample: retrieve the name of employees who earn more than Joe:  ∏ name (Emp (sal>x.sal) б name= ‘ Joe ’ (ρ x (Emp))) zSemi-Join selects the columns of one relation that joins with another. It is equivalent to a join followed by a projection: yEmp (dno) Dept ≡ ∏ SS#, name, age, salary, dno (Emp Dept)