ICOM 5016 Introduction to Database Systems Lecture 2
ICOM 5016 – Introduction to Database Systems Lecture 2 – Sets and Relations Dr. Manuel Rodriguez Martinez and Dr. Bienvenido Vélez Department of Electrical and Computer Engineering University of Puerto Rico, Mayagüez Slides are adapted from: Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See www. db-book. com for conditions on re-use
Objectives n Introduce Set Theory n Complex Sets n Review of Set concepts l Power Sets l Partitions l Cardinality l Set notation l Empty set l Cartesian products l Subset l Binary relations n Set Operations l N-ary relations l Union l Intersection l Difference Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 n Relations 1. 2 ©Silberschatz, Korth and Sudarshan
On Sets and Relations n A set S is a collection of objects, where there are no duplicates l Examples 4 A = {a, b, c} 4 B = {0, 2, 4, 6, 8} 4 C = {Jose, Pedro, Ana, Luis} n The objects that are part of a set S are called the elements of the set. l Notation: 40 is an element of set B is written as 0 B. 43 is not an element of set B is written as 3 B. Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 3 ©Silberschatz, Korth and Sudarshan
Cardinality of Sets n Sets might have l 0 elements – called the empty set . l 1 element – called a singleton l N elements – a set of N elements (called a finite set) 4 Ex: l S = {car, plane, bike} elements – an infinite number of elements (called infinite set) 4 Integers, 4 Even Reals, numbers: E = {0, 2, 4, 6, 8, 10, …} – Dot notation means infinite number of elements Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 4 ©Silberschatz, Korth and Sudarshan
Cardinality of Sets (cont. ) n The cardinality of a set is its number of elements l Notation: cardinality of S is denoted by |S| l Could be: 4 an 4 integer number infinity symbol . n Countable Set - a set whose cardinality is: l Finite l Infinite but as big as the set of natural numbers (one-to-one correspondence) n Uncountable set – a set whose cardinality is larger than that of natural numbers. Ex: R - real numbers Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 5 ©Silberschatz, Korth and Sudarshan
Cardinality of Sets (cont. ) n Some examples: l A = {a, b, c}, |A| = 3 l N = {0, 1, 2, 3, 4, 5, …} 4|N| l R – set of real numbers 4|R| l = E = {0, 2, 3, 4, 6, 8, 10, …} 4|E| l = = the empty set 4| |=0 Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 6 ©Silberschatz, Korth and Sudarshan
Set notations and equality of Sets n Enumeration of elements of set S A = {a, b c} l E = {0, 2, 4, 6, 8, 10, …} n Enumeration of the properties of the elements in S l E = {x : x is an even integer} l E = {x: x I and x%2=0, where I is the integers. } n Two sets are said to be equal if and if only they both have the same elements l A = {a, b, c}, B = {a, b, c}, then A = B l if C = {a, b, c, d}, then A C 4 Because d A l Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 7 ©Silberschatz, Korth and Sudarshan
Sets and Subsets n Let A and B be two sets. B is said to be a subset of A if and only if every member x of B is also a member of A l Notation: B A l Examples: 4 A = {1, 2, 3, 4, 5, 6}, B = {1, 2}, then B A 4 D = {a, e, i, o, u}, F = {a, e, i, o, u}, then F D l If B is a subset of A, and B A, then we call B a proper subset 4 Notation: B A 4 A = {1, 2, 3, 4, 5, 6}, B = {1, 2}, then B A l The empty set is a subset of every set, including itself 4 A, for every set A l If B is not a subset of A, then we write B A Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 8 ©Silberschatz, Korth and Sudarshan
Set Union n Let A and B be two sets. Then, the union of A and B, denoted by A B is the set of all elements x such that either x A or x B. l A B = {x: x A or x B} n Examples: l A = {10, 20 , 30, 40, 100}, B = {1, 2 , 10, 20} then A B = {1, 2, 10, 20, 30, 40, 100} l C = {Tom, Bob, Pete}, then C = C l For every set A, A A = A (Idempotence Law) Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 9 ©Silberschatz, Korth and Sudarshan
Set Intersection n Let A and B be two sets. Then, the intersection of A and B, denoted by A B is the set of all elements x such that x A and x B. l A B = {x: x A and x B} n Examples: l A = {10, 20 , 30, 40, 100}, B = {1, 2 , 10, 20} then A B = {10, 20} l Y = {red, blue, green, black}, X = {black, white}, then Y X = {black} l E = {1, 2, 3}, M={a, b} then, E M = l C = {Tom, Bob, Pete}, then C = n For every set A, A A = A (Idempotence Law) n Sets A and B disjoint if and only if A B = l They have nothing in common Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 10 ©Silberschatz, Korth and Sudarshan
Set Difference n Let A and B be two sets. Then, the difference between A and B, denoted by A - B is the set of all elements x such that x A and x B. l A - B = {x: x A and x B} n Examples: l A = {10, 20 , 30, 40, 100}, B = {1, 2 , 10, 20} then A - B = {30, 40, 100} l Y = {red, blue, green, black}, X = {black, white}, then Y - X = {red, blue, green} l E = {1, 2, 3}, M={a, b} then, E - M = E l C = {Tom, Bob, Pete}, then C - = C l For every set A, A - A = Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 11 ©Silberschatz, Korth and Sudarshan
Power Set and Partitions n Power Set: Given a set A, then the set of all possible subsets of A is called the power set of A. l Notation: l Example: 4 A = {a, b, 1} then = { , {a}, {b}, {1}, {a, b}, {a, 1}, {b, 1}, {a, b, 1}} 4 Note: empty set is a subset of every set. n Partition: A partition of a nonempty set A is a subset of such that l Each set element P is not empty l For D, F , D F, it holds that D F = l The union of all P is equal to A. l Example: A = {a, b, c}, then = {{a, b}, {c}}. Also = {{a}, {b}, {c}}. But this is not: M = {{a, b}, {c}} Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 12 ©Silberschatz, Korth and Sudarshan
Cartesian Products and Relations n Cartesian product: Given two sets A and B, the Cartesian product between and A and B, denoted by A x B, is the set of all ordered pairs (a, b) such a A and b B. l Formally: A x B = {(a, b): a A and b B} l Example: A = {1, 2}, B = {a, b}, then A x B = {(1, a), (1, b), (2, a), (2, b)}. n A binary relation R on two sets A and B is a subset of A x B. l Example: A = {1, 2}, B = {a, b}, 4 then 4 and A x B = {(1, a), (1, b), (2, a), (2, b)}, one possible R A x B = {(1, a), (2, a)} JIQ: How many binary relations exist among two finite sets? Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 13 ©Silberschatz, Korth and Sudarshan
N-ary Relations n Let A 1, A 2, …, An be n sets, not necessarily distinct, then an n -ary relation R on A 1, A 2, …, An is a sub-set of A 1 x A 2 x … x An. l Formally: R A 1 x A 2 x … x An l R = {(a 1, a 2, …, an) : a 1 A 1 ∧ a 2 A 2 ∧… ∧ an An} l Example: 4 R = set of all real numbers 4 R x R = three-dimensional space = {(x, y, z): x R ∧ x 0 ∧ y R ∧y 0 ∧ z R and z 0} = Set of all three-dimensional points that have positive coordinates 4 P Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 14 ©Silberschatz, Korth and Sudarshan
The Relational Model (CACM 13: 6 1970) n Relation = Set of Tuples = Subset of A 1 x A 2 x … x An l Edgar “Ted” Codd 1923 -2003 Turing 1981 NO duplicates and NO order n Rows correspond to entities or objects n Columns correspond to attributes of properties of objects n Tables are interrelated through the use of attributes or foreign keys n Queries: Combine tables to form new tables Database System Concepts, 5 th Ed. , slide version 5. 0, June 2005 1. 15 ©Silberschatz, Korth and Sudarshan
- Slides: 15