Download presentation
Presentation is loading. Please wait.
Published byLynette Greene Modified over 9 years ago
1
Relational Algebra CMSC 461 Michael Wilson
2
Relational algebra Before we get into SQL, want to take a look at what exactly SQL is really modeling Foundation for SQL and other query languages
3
Base operations Union Difference Cartesian Product Projection Selection Rename
4
Base operations With these operations you can accomplish everything you need in relational algebra There are more operations, but they are for simplification
5
Union Say you have two relations with tuples {a, b, c} and {d, e, f} The union of the two is {a, b, c, d, e, f} One requirement: the two relations must have the same attributes This is called being “ union-compatible ” Notation: A ∪ B
6
Difference Also known as the relative complement Say you have two relations Relation A: {a, b, c} Relation B: {a, b, f} Notation: B \ A The relative complement of A in B B \ A results in the set of elements in B that are not in A B \ A = {f} The two relations must be union-compatible
7
Cartesian product This one can get kind of ugly Requirement: the two relations involved must have completely disjoint attributes A Cartesian product of two relations A and B is the set of every tuple in A paired with every tuple in B Notation: A × B
8
Cartesian product Two relations A and B with disjoint attributes A has tuples {a, b, c} B has tuples {d, e, f} Each tuple has attribute values a has a 1, a 2, …, a n b has b 1, b 2, …, b n Etc.
9
Cartesian product Resulting product by tuples: (a, d) (a, e) (a, f) (b, d) (b, e) (b, f) (c, d) (c, e) (c, f)
10
Cartesian product Listing out the attribute values: (a 1, a 2, …, a n, d 1, d 2, …, d n ) (a 1, a 2, …, a n, e 1, e 2, …, e n ) (a 1, a 2, …, a n, f 1, f 2, …, f n ) (b 1, b 2, …, b n, d 1, d 2, …, d n ) (b 1, b 2, …, b n, e 1, e 2, …, e n ) (b 1, b 2, …, b n, f 1, f 2, …, f n ) (c 1, c 2, …, c n, d 1, d 2, …, d n ) (c 1, c 2, …, c n, e 1, e 2, …, e n ) (c 1, c 2, …, c n, f 1, f 2, …, f n )
11
Projection This is an operation that is used on one relation Essentially restricts the attributes of a relation to the ones you’re interested about Notation: π a1,a2,…an (R) a i = attribute I R = relation to project
12
Projection Example (from Wikipedia): π contactName,contactPhoneNumber (addressBook)
13
Projection addresslastContacted contactPhoneNumber numberTypecontactName 111 Great Street 1 day ago 555 5555CellPhil 123 Not So Great Street Last month 555 6666LandlineHenry 8 Get Out of Here Way Last week 555 7777WorkBob 7 RUN! Drive 2 years ago 555 8888CellOctavio
14
Projection contactNamecontactPhoneNumber Phil555 5555 Henry555 6666 Bob555 7777 Octavio555 8888
15
Selection Lists a set of tuples that match the specified criteria Notation: σ p (R) p is a selection predicate This consists of terms that are connected by logic operators ^ (and) v (or) ¬ (not)
16
Selection Terms have the following format: is =, ≠, >, ≥, <, ≤ Examples: phoneNumber=555 5555 GPA>2.0
17
Selection UMBC IDAgeFNameLNameGPA SM1111120Mario 4.0 SM1111219LuigiMario3.9 MT1111121SamusAran4.0 FF1111321SnowVilliers0.2 SW1123425SnowWhite3.5
18
Selection σ GPA<4.0 (R) Select all from our relation where the attribute value for GPA is less than 4.0
19
Selection UMBC IDAgeFNameLNameGPA SM1111219LuigiMario3.9 FF1111321SnowVilliers0.2 SW1123425SnowWhite3.5
20
Selection σ age>20^fName=‘Snow’ (R) Select all from our relation where the attribute value for age is greater than 20 and the attribute value for fName is equal to ‘Snow’
21
Selection UMBC IDAgeFNameLNameGPA FF1111321SnowVilliers0.2 SW1123425SnowWhite3.5
22
Rename The result of a rename is literally the same as the original relation, but with an attribute renamed ρ a/b (R) The b attribute in the resulting tuples is renamed to a
23
Rename UMBC IDAgeFNameLNameGPA SM1111120Mario 4.0 SM1111219LuigiMario3.9 MT1111121SamusAran4.0 FF1111321SnowVilliers0.2 SW1123425SnowWhite3.5
24
Rename ρ MIT ID/UMBC ID (R) Rename UMBC ID to MIT ID
25
Rename MIT IDAgeFNameLNameGPA SM1111120Mario 4.0 SM1111219LuigiMario3.9 MT1111121SamusAran4.0 FF1111321SnowVilliers0.2 SW1123425SnowWhite3.5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.