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