CS 370 Database Systems Lecture 11 Relational Algebra.

Slides:



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

Relational Databases - Amit Bhawnani & Nimesh Shah.
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.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
©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.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
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.
Exercises ReservesSailorsBoats Find names of sailors who’ve reserved boat #103 Basic operations: Selection ( σ ) gives a subset of rows. Projection ( π.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
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.
CS 370 Database Systems Lecture 12 Introduction to SQL.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
1 The Relational Model By Elena Ciriani CS157A February 19, 2004 Professor Lee.
CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database.
The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT.
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.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
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.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
Computing & Information Sciences Kansas State University Friday, 26 Jan 2008CIS 560: Database System Concepts Lecture 2 of 42 Friday, 29 August 2008 William.
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.
Advanced Relational Algebra & SQL (Part1 )
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 5: Other Relational Languages.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
Relational Algebra HW2 Turn in as a hardcopy at the start of next class period. You may work this assignment in groups.
ICOM 5016 – Introduction to Database Systems Lecture 6 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
1 Session 3 Welcome: To session 3-the fourth learning sequence “Relational algebra “ Recap : In the previous learning sequences, we discussed the four.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
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.
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Relational Algebra.
LECTURE 3: Relational Algebra
Session 3 Welcome: To session 3-the second learning sequence
Instructor: Mohamed Eltabakh
Presentation transcript:

CS 370 Database Systems Lecture 11 Relational Algebra

The basic set of operations for the relational model is known as the relational algebra. These operations enable a user to specify basic retrieval requests. The result of a retrieval is a new relation, which may have been formed from one or more relations. The algebra operations thus produce new relations, which can be further manipulated using operations of the same algebra. A sequence of relational algebra operations forms a relational algebra expression, whose result will also be a relation that represents the result of a database query (or retrieval request). Relational Algebra

Six basic operations: selection, projection, union, cross-product, and difference, rename. –selection:  –projection:  –union:  –set difference: – –Cross-product: x –rename:  Each operation takes one or more relations as input and give a relation as output. Relational Algebra

SELECT Operation: SELECT operation is used to select a subset of the tuples from a relation that satisfy a selection condition. It is a filter that keeps only those tuples that satisfy a qualifying condition – those satisfying the condition are selected while others are discarded. Example: To select the EMPLOYEE tuples whose department number is four or those whose salary is greater than $30,000 the following notation is used:  DNO = 4 (EMPLOYEE)  SALARY > 30,000 (EMPLOYEE) In general, the select operation is denoted by  (R) where the symbol  (sigma) is used to denote the select operator, and the selection condition is a Boolean expression specified on the attributes of relation R Selection

Selection Example… sidsnameratingage 22Dustin Lubber Rusty sidsnameratingage 28Yuppy Lubber Guppy Rusty sidbidday /10/ /12/96 Instance of S1 of Sailors Instance of R1 of Reserves Instance of S2 of Sailors

Selection Consider the instance of the Sailors relation shown in the previous slide denoted by S2. We can retrieve rows corresponding to expert sailors by using the  operator. So, the expression;  rating>8 (S2) The subscript rating>8 specifies the selection criterion to be applied while retrieving tuples. sidsnameratingage 28Yuppy Rusty1035.0

Notation:  P (r) Defined as:  P (r) = { t | t  r  P(t) } and the relation schema of r is unchanged Read as: t is a tuple of r and t satisfies the predicate P. P is a formula of the form: attribute name op attribute name (e.g. salary > expense) orattribute name op constant (e.g. salary > 10000) where op is one of {       } Predicates can be connected by  (and)  (or)  (not) Example: P = (salary > 10000)  (age < 25) Select Operation

Relation r: Select Operation - Example  A  B  D  5 (r) :

Notation:  X (r) where X = {A 1, A 2,..., A k } is any subset of the schema of r Defined as:  X (r) = { t [X] | t  r } After the projection, duplicates are assumed to be eliminated automatically since a relation is a set! Not exactly implemented in real DBMSs: duplicates are not eliminated until explicitly told to do so Order of attributes in the projection list can be arbitrary. (One use of projection is to rearrange attributes) Note: in general the relation schema changes Projection Operation

Project Operation - Example Relation r:  A,C (r):

Union Operation Notation: r  s Defined as: r  s = { t | (t  r)  (t  s) } For r  s to be valid (r and s must be compatible), i.e., –r, s must have the same number of attributes –domains must be compatible pair-wise between r and s. R(EmpName, Age)S(StudName, Age) T(ProdID, Name)U(StudName, Age, CGA) Compatible relations: R and S, R and  StudName, Age U, etc. Incompatible relations: R and T, R and U, etc.

Union Operation - Example Relation r, s: r s r  s : AB  1  3  1  2 AB  1  1  2 AB  3  2

Set Difference Operation Notation: r  s Defined as: r  s = { t | (t  r)  (t  s) } For r  s to be valid: –r, s must have the same number of attributes –domains must be compatible pair-wise between r and s.

Set Difference Operation - Example Relation r, s: r s r  s : AB  2  3 AB  1  1 AB  1  1  2

Cartesian-Product Operation Notation: r  s Defined as: r  s = { t q | (t  r)  (q  s) } Assume that attributes of r and s are disjoint (i.e., r and s don’t have attributes with the same name) If they are not disjoint, then make them disjoint by renaming the attributes concerned

Cartesian-Product Operation - Example Relation r, s: r s r  s :

branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account (account_number, branch_name, balance) loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower (customer_name, loan_number) Banking Example

Find all loans of over $1200 Find the loan number for each loan of an amount greater than $1200  amount > 1200 (loan)  loan_number (  amount > 1200 (loan)) Find the names of all customers who have a loan, an account, or both, from the bank  customer_name (borrower)   customer_name (depositor) Example Queries

Find the names of all customers who have a loan at the Perryridge branch. Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank.  customer_name (  branch_name = “Perryridge” (  borrower.loan_number = loan.loan_number (borrower x loan))) –  customer_name (depositor)  customer_name (  branch_name=“Perryridge ” (  borrower.loan_number = loan.loan_number (borrower x loan))) Example Queries

Find the names of all customers who have a loan at the Perryridge branch. Query 2  customer_name (  loan.loan_number = borrower.loan_number ( (  branch_name = “Perryridge ” (loan)) x borrower)) Query 1  customer_name (  branch_name = “Perryridge” (  borrower.loan_number = loan.loan_number (borrower x loan))) Example Queries

Additional operations: Intersection (  ) Join ( ) Division ( / ) ReservesSailorsBoats Basic operations: Selection ( σ ) Projection ( π ) Cross-product (  ) Set-difference ( — ) Union (  ) :tuples in both relations. :like  but only keep tuples where common fields are equal. :tuples from relation 1 with matches in relation 2 : gives a subset of rows. : deletes unwanted columns. : combine two relations. : tuples in relation 1, but not 2 : tuples in relation 1 and 2. Relational Algebra Review

Additional operations: Intersection (  ) Join ( ) Division ( / ) Reserves Sailors Boats Basic operations: Selection ( σ ) Projection ( π ) Cross-product (  ) Set-difference ( — ) Union (  ) Find names of sailors who’ve reserved a green boat σ ( color=‘Green’ Boats) ( Sailors) π ( sname ) ( Reserves) Relational Algebra Review