Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra Instructor: Mohamed Eltabakh 1.

Similar presentations


Presentation on theme: "Relational Algebra Instructor: Mohamed Eltabakh 1."— Presentation transcript:

1 Relational Algebra Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu 1

2 What we learned so far… Entity-Relationship Model Converting it to relational model Relational Model Creating table Defining constraints Inserting, deleting, and updating data 2 We learn next how to query the data

3 Query Language Define data retrieval operations for relational model Express easy access to large data sets in high-level language, not complex application programs 3 Classification of Query Languages ProceduralDeclarative You define how to get the data You do not define how to get the data

4 Algebra Behind the Query Language Relational Algebra Set of operators that operate on relations Operator semantics based on Set or Bag theory Relational algebra form underlying basis (and optimization rules) for SQL 4

5 Relational Algebra Basic operators Set Operations (Union: ∪, Intersection: ∩,difference: – ) Select: σ Project: π Cartesian product: x rename: ρ More advanced operators, e.g., grouping and joins The operators take one or two relations as inputs and produce a new relation as an output One input  unary operator, two inputs  binary operator 5

6 Relational Algebra Similar in concept to arithmetic operators and expressions In relational algebra, instead of variables we have relations 6 (w + t) / ((x + y) * 3) +, /, * are called binary operators w, t, x, y are called variables π customer_name (R ⋈ σ balance <100 (S) ) R, S are DB relations Relational operators

7 Set Operators Union, Intersection, Difference Defined only for union compatible relations Relations are union compatible iff they have same sets of attributes (schema), and the same types (domains) of attributes Example : Union compatible or not? Student (sNumber, sName) Course (cNumber, cName) Not compatible 7

8 Union over sets:  Consider two relations R and S that are union-compatible AB 12 34 R AB 12 34 56 S AB 12 34 56 R  SR  S 8

9 Union over sets:  Notation: R ∪ S Defined as: R ∪ S = {t | t ∈ R or t ∈ S} For R ∪ S to be valid, they have to be union-compatible 9

10 Difference over sets: – R – S are the tuples that appear in R and not in S Defined as: R – S = {t | t ∈ R and t ∈ S} AB 12 34 R AB 12 56 S AB 34 R – S 10

11 Intersection over sets: ∩ Consider two Relations R and S that are union- compatible AB 12 34 R AB 12 34 56 S AB 12 34 R ∩ S 11

12 Intersection: ∩ Notation: R ∩ S Defined as: R ∩ S = { t | t ∈ r and t ∈ s } Note: R ∩ S = R– (R–S) R S 12

13 Selection: σ Select: σ c (R): c is a condition on R’s attributes Select subset of tuples from R that satisfy selection condition c ABC 125 346 127 R σ (C ≥ 6) (R) ABC 346 127 13

14 Selection: σ Notation: σ c (R) c is called the selection predicate Defined as: σ c (R) = {t | t ∈ R and c(t) is true} c is a formula in propositional calculus consisting of terms connected by : ∧ (and), ∨ (or), ¬ (not) Each term is one of: op | op op is one of: =,= ̸,>,≥.<.≤ Example of selection: σ branch_name=“Perryridge” ^ balance>1000 (account) 14

15 Selection: Example R σ ((A=B) ^ (D>5)) (R) 15 σ (D > C) (R)

16 Project: π π A1, A2, …, An (R), with A1, A2, …, An  attributes A R returns all tuples in R, but only columns A1, A2, …, An A1, A2, …, An are called Projection List ABC 125 346 127 128 R π A, C (R) AC 15 36 17 18 16

17 Extended Projection: π L (R) Standard project L contains only column names of R Extended projection L may contain expressions and assignment operators π C, V  A, X  C*3+B (R) 17

18 Extended Projection: π L (R) Example π C, V  A, X  C*3+B (R) 18 ABC 125 346 127 128 R CVX 5117 6322 7123 8126 Rename column A to V Compute this expression and call it X

19 Extended Projection: π L (R) Example π B, ‘Ok’ As Test (R) 19 ABC 125 346 127 128 R BTest 2Ok 4 2 2 What If I want to select column B, and add another column called ‘Test’ containing word ‘Ok’

20 Cross Product (Cartesian Product): X R S R X S 20

21 Cross Product (Cartesian Product): X Notation R x S Defined as: R x S = {t q | t ∈ r and q ∈ s} Assume that attributes are all unique, otherwise renaming must be used 21

22 Renaming: ρ ρ S (R) changes relation name from R to S ρ S(A1, A2, …, An) (R) renames also attributes of R to A1, A2, …, An BCD 2310 2311 6712 R XCD 2310 2311 6712 ρ S(B->X, C, D) (R) S BCD 2310 2311 6712 ρ S (R) S 22

23 The true power is when you form expressions and put several operators together We do the same with arithmetic operators 23 Composition of Operators (w + t) / ((x + y) * 3)

24 Composition of Operators Can build expressions using multiple operations Example: σ A=C (R x S) R S R X S σ A=C (R x S) 24

25 Banking Example 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) 25

26 Example Queries 26

27 Example Queries (Cont’d) 27

28 Example Queries (Cont’d) 28

29 Example Queries Find customers’ names who have neither accounts nor loans π customer_name (customer) - (π customer_name (borrower) U π customer_name (depositer)) 29


Download ppt "Relational Algebra Instructor: Mohamed Eltabakh 1."

Similar presentations


Ads by Google