Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.

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.
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.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
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.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple 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.
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.
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.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
Introduction to Database Systems 1 Relational Algebra Relational Model: Topic 3.
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.
Relational Model: Examples. Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
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.
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.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
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)
ICOM 5016 – Introduction to Database Systems Lecture 6 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
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.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
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.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Relational Algebra & Calculus
Database Systems (資料庫系統)
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.
Relational Algebra.
LECTURE 3: Relational Algebra
Instructor: Mohamed Eltabakh
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.
Presentation transcript:

Relational Algebra

Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational Calculus a.tuple relational calculus (TRC) b.domain relational calculus (DRC)  Practical 1. SQL (SEQUEL from System R) 2. QUEL (Ingres) 3. Datalog (Prolog-like)  Theoretical QL’s:  give semantics to practical QL’s  key to understand query optimization in relational DBMSs

Relational Algebra n Basic operators ù select (  )  project (  ) ù union (  ) ù set difference ( - ) ù cartesian product ( x ) ù rename (  ) n The operators take one or two relations as inputs and give a new relation as a result. relational operator relation

Example Instances R1 S1 S2 Boats Schema: Boats(bid, bname, color) Sailors(sid, sname, rating, age) Reserves( sid, bid, day)

Projection n Examples: ; n Retains only attributes that are in the “projection list”. n Schema of result: ù exactly the columns in the projection list, with the same names that they had in the input relation. n Projection operator has to eliminate duplicates (How do they arise? Why remove them?) ù Note: real systems typically don’t do duplicate elimination unless the user explicitly asks for it. (Why not?)

Projection S2

Selection (  ) n Selects rows that satisfy selection condition. n Result is a relation. Schema of result is same as that of the input relation. n Do we need to do duplicate elimination?

Selection n Notation:  p (r) n p is called the selection predicate, r can be the name of a table, or another query n Predicate: 1. Simple ù attr1 = attr2 ù Attr = constant value ù(also,, etc) 2. Complex ù predicate1 AND predicate2 ù predicate1 OR predicate2 ù NOT (predicate)

Union and Set-Difference n All of these operations take two input relations, which must be union-compatible : ù Same number of columns (attributes). ù `Corresponding’ columns have the same type. n For which, if any, is duplicate elimination required?

Union S1 S2

Set Difference S1 S2 S2 – S1

Cartesian-Product n S1  R1: Each row of S1 paired with each row of R1. Like the c.p for mathematical relations: every tuple of S1 “appended” to every tuple of R1 n Q: How many rows in the result? n Result schema has one field per field of S1 and R1, with field names `inherited’ if possible. ù May have a naming conflict: Both S1 and R1 have a field with the same name. ù In this case, can use the renaming operator…

Cartesian Product Example R1 S1 R1 X S1 =

Rename (  ) n Allows us to refer to a relation by more than one name and to rename conflicting names Example:  x (E) returns the expression E under the name X n If a relational-algebra expression E has arity n, then  x (A1, A2, …, An) (E) returns the result of expression E under the name X, and with the attributes renamed to A1, A2, …., An. Ex.  temp1 (sid1,sname,rating, age, sid2, bid, day) (R1 x S1)

Compound Operator: Intersection n In addition to the 6 basic operators, there are several additional “Compound Operators” ù These add no computational power to the language, but are useful shorthands. ù Can be expressed solely with the basic ops. n Intersection takes two input relations, which must be union-compatible. n Q: How to express it using basic operators? R  S = R  (R  S)

Intersection S1 S2

Compound Operator: Join n Joins are compound operators involving cross product, selection, and (sometimes) projection. n Most common type of join is a “natural join” (often just called “join”). R S conceptually is: ù Compute R  S ù Select rows where attributes that appear in both relations have equal values ù Project all unique atttributes and one copy of each of the common ones. n Note: Usually done much more efficiently than this. n Useful for putting “normalized” relations back together.

Natural Join Example R1 S1 R1 S1 =

Other Types of Joins n Condition Join (or “theta-join”): n Result schema same as that of cross-product. n May have fewer tuples than cross-product. n Equi-join: special case: condition c contains only conjunction of equalities.

Compound Operator: Division n Useful for expressing “for all” queries like: Find sids of sailors who have reserved all boats. n For A/B attributes of B are subset of attrs of A. ù May need to “project” to make this happen. n E.g., let A have 2 fields, x and y; B have only field y: A/B contains all tuples (x) such that for every y tuple in B, there is an xy tuple in A.

Examples of Division A/B A B1 B2 B3 A/B1 A/B2A/B3

Expressing A/B Using Basic Operators n Division is not essential op; just a useful shorthand. ù (Also true of joins, but joins are so common that systems implement joins specially.) n Idea: For A/B, compute all x values that are not `disqualified’ by some y value in B. ù x value is disqualified if by attaching y value from B, we obtain an xy tuple that is not in A. Disqualified x values = A/B = Disqualified x values

Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-only) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number)

Example Queries n Find all loans of over $1200  amount >1200 (loan) n Find the loan number for each loan of an amount greater than $1200  loan-number (  amount > 1200 (loan)) loan (loan-number, branch-name, amount)

Example Queries n Find the names of all customers who have a loan, an depositor account, or both, from the bank  customer-name (borrower)   customer-name (depositor) n Find the names of all customers who have a loan and a depositor account at bank.  customer-name (borrower)   customer-name (depositor) depositor (customer-name, account-number) borrower (customer-name, loan-number)

Example Queries n Find the names of all customers who have a loan at the Perryridge branch but do not have an depositor account at any branch of the bank.  customer-name (  branch-name = “Perryridge” (borrower loan)) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number) –  customer-name ( depositor )

Example Queries Find the largest account balance n Rename account relation as d n The query is:  account.balance < d.balance (account x  d (account)))  balance (account) -  account.balance ( account (account-number, branch-name, balance)

Example Queries n Find all customers who have an account from at least the “Downtown” and the Uptown” branches. ù Query 1  CN (  BN=“Downtown ” (depositor account))   CN (  BN=“Uptown ” (depositor account)) where CN denotes customer-name and BN denotes branch-name. ù Query 2  customer-name, branch-name (depositor account)   temp(branch-name ) ({(“Downtown”), (“Uptown”)}) account (account-number, branch-name, balance) depositor (customer-name, account-number)

Find all customers who have an account at all branches located in Boston.  customer-name, branch-name (depositor account)   branch-name (  branch-city = “Boston” (branch)) Example Queries

Extended Relational Operations n Additional Operators that extend the power of the language ù Based on SQL… make the language less clean n Generalized projections n Outer Joins n Update

General Projection Notation:  e1, e2, …, en (Relation) ei: can include any arithmetic operation – not only attributes Example: credit = Then:  cname, limit – balance =

Outer Joins Motivation: loan borrower loan borrower = Join result loses: any record of Perry any record of Smith

Outer Join ( ) n Left outer Join ( ) preserves all tuples in left relation loan borrower = n Right outer Join ( ) preserves all tuples in right relation

Outer Join (cont) n Full Outer Join ( ) ù preserves all tuples in both relations

Update (  ) 1) Deletion: r  r – s account  account –  bname = Perry (account) 2) Insertion: r  r s n branch  branch {( BU, Boston, 9M)} 3) Update: r   e1, e2, …, en (r) account   bname, acct_no, bal * 1.05 (account)  