Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 370 Database Systems Lecture 11 Relational Algebra.

Similar presentations


Presentation on theme: "CS 370 Database Systems Lecture 11 Relational Algebra."— Presentation transcript:

1 CS 370 Database Systems Lecture 11 Relational Algebra

2 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

3 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

4 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

5 Selection Example… sidsnameratingage 22Dustin745.0 31Lubber855.5 58Rusty1035.5 sidsnameratingage 28Yuppy935.0 31Lubber855.5 44Guppy535.0 58Rusty1035.5 sidbidday 2210110/10/96 5810311/12/96 Instance of S1 of Sailors Instance of R1 of Reserves Instance of S2 of Sailors

6 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 28Yuppy935.0 58Rusty1035.0

7 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

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

9 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

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

11 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.

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

13 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.

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

15 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

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

17 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

18 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

19 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

20 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

21 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

22 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


Download ppt "CS 370 Database Systems Lecture 11 Relational Algebra."

Similar presentations


Ads by Google