Lecture 3
Lecture 3
5 credits)
Yuantao Fan
[email protected]
Halmstad University
Overview
• Review on Sets
– Definition
– Operations
– Properties
• Introduction to Relational Algebra
– Selection 𝜎
– Projection 𝜋
– Union ∪
– Intersection ∩
– Product x
– Join ⋈
What is a set?
• A set is a well defined collection of distinct objects, called the “elements” or “members” of the set
– A set might be finite or infinite
– x is an element of set A is denoted by 𝑥 ∈ A
– Negated by writing x ∉ A
• Notations
– Enumeration – enumerate all elements (explicitly or implicitly)
• Continent = {Asia, Africa, NorthAmerica, SouthAmerica, Antarctica, Europe, and Australia}
• M = {2, 4, 6, 8, …, 100}
– Set builder notation
• { variable | descriptive statement }
• E.g. Even numbers = { 2𝑘 | 𝑘 ∈ ℤ }
• Q ={ x | x = p / q where p and q are integers and q ≠0 }
– Commonly used sets
• N - set of all natural numbers
• Z - set of all integers
• Q - set of all rational numbers
• …
Properties and operations
Union Intersection
U U
• Union: A ∪ B = { x | x ∈ A or x ∈ B }
A B A B
• Intersection: A ∩ B = { x | x ∈ A and x ∈ B }
• Difference: U \ A = { x | x ∈ U and x ∉ A }
– Also written as U - A U U
A B A B
• Complement
%
– Given a universal set U, U - A is also written as A
Difference Complement
Properties and operations
• Subset:
– A ⊆ B if for all x ∈ A then x ∈ B
• Propoer subset:
– A ⊂ B if A ⊆ B and A ≠ B
• Equality:
– A = B if A ⊆ B and B ⊆ A
Souce: https://www.onlinemathlearning.com/venn-diagrams.html
Cartesian Product
• The Cartesian product of two sets A and B is
denoted as 𝐴 × 𝐵
– 𝐴 × 𝐵 = { (a, 𝑏) | a ∈ A and b ∈ B }
– Note that (a, 𝑏) is ordered pairs
• Example
– A deck of cards: 4 suits and 13-element cards
Source: https://en.wikipedia.org/wiki/Cartesian_product
Projection
• Example
– Points in 2D-space: ℝ# = ℝ × ℝ
– If point r ∈ ℝ# , then
– 𝜋$ 𝑟 is the x coordinate
– 𝜋% 𝑟 is the y coordinate
Relations
• A relation R is a subset of the cartesian product of two or more sets
– R ⊆ A1 x A2 x … x An
– Equivalent notations
• (a1, a2, …, an) satisfies R
• (a1, a2, …, an) ∈ R
• R (a1, a2, …, an)
Source: https://www.cuemath.com/calculus/What-are-functions/
Relational Model
Course(Course_name, Course_code, Credit_hours, Teacher)
• A relation is unordered set that Course_name Course_code Credit_hours Teacher
contain the relation ship of attributes
Intro to
that represent entities
Computer CS1310 4 11
Science
• A tuple is a set of attrubute values Data
CS3320 4 22
(domain) in the relation Structures
Discrete
MATH2410 3 33
• Example Mathematics
– (Intro to Computer Science, CS1310, 4, Database CS3380 3 44
CS)
Relational Model - Primary Key
Source: https://medium.com/@sahirnambiar/relational-algebra-the-underpinnings-of-sql-74959481231a
Relational Algebra - Selection T(Tid, Cid)
Tid Cid
• Syntax
– 𝜎&'()*+,-( (𝑅) a1 10
a2 11
• Choose a subset of the tuples from a relation
a3 12
that satisfies a selection predicate
– Filtering using predicates a3 13
– Combine multiple predicates
a1 10 a3 12
SELECT * SELECT *
FROM T FROM T
WHERE Tid=‘a1’; WHERE Tid=‘a3’ AND Cid=‘12’;
Relational Algebra - Projection
T(Tid, Cid)
Tid Cid
• Syntax
– 𝜋!.,!#,…!1 (𝑅) a1 10
a2 11
• Generate a relation with tuples that contains
attributes specified a3 12
– Can manipulate the values a3 13
– Can rearrange the order
𝜋Tid,Cid*2 (𝜎Tid=‘a2’(T))
Tid Cid
a2 24
(A ∪ B)
Tid Cid
a1 10
a2 11
(SELECT * FROM A) a2 11
UNION ALL
(SELECT * FROM B); a3 13
Relational Algebra - Intersection
A(Tid, Cid) B(Tid, Cid)
• Syntax Tid Cid Tid Cid
– (A ∩ B)
a1 10 a2 11
(A ∩ B)
Tid Cid
a2 11
(SELECT * FROM A)
INTERSECT
(SELECT * FROM B);
Relational Algebra - Product
A(Tid, Cid) B(Tid, Cid)
• Syntax Tid Cid Tid Cid
– (A x B)
a1 10 a2 11
• Generate a relation that contains all possible a2 11 a3 13
combinations of the tuples from both
relations (A x B)
SELECT * FROM A, B; a1 10 a2 11
a1 10 a3 13
a2 11 a3 13
Relational Algebra - Join
A(Tid, Cid) B(Tid, Cid)
• Syntax Tid Cid Tid Cid
– (A ⋈ B)
a1 10 a2 11
• Generate a relation that contains all tuples a2 11 a3 13
with a common value(s) of one (or more)
attrubute(s) (A ⋈ B)
Tid Cid
a2 11
• Lab 1 intoduction