DS4001 Databases (7.
5 credits)
Lecture 3 – Introduction to Relational Algebra
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
• Cardinality - numer of elements of the set
– |A x B| = |A| x |B|
• Example
– A deck of cards: 4 suits and 13-element cards
Source: https://en.wikipedia.org/wiki/Cartesian_product
Projection
• Given (a, 𝑏) ∈ A x B the projections are defined as
– 𝜋! 𝑎, 𝑏 = 𝑎
– 𝜋" 𝑎, 𝑏 = 𝑏
• 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)
• A binary relation R ⊆ A x B consists of a set of pairs
– Denoted using aRb
Relations
• Let R be a relation on a set A, that is R ⊆ A x A
– R is Reflexive if for all x ∈ A. xRx
– R is Symmetric if for all x, y ∈ A. xRy –> yRx
– R is Transitive if for all x, y, z ∈ A. xRy and yRz -> xRz
• Example - Let A = {0, 1, 2, 3} and a relation R on A be given by
– R={ (0,0), (0,1), (0,3), (1,0), (1,1), (2,2), (3,0), (3,3) }
– Is R reflexive? Symmetric? Transitive?
Relations
• Let R be a relation on a set A, that is R ⊆ A x A
– R is Reflexive if for all x ∈ A. xRx
– R is Symmetric if for all x, y ∈ A. xRy –> yRx
– R is Transitive if for all x, y, z ∈ A. xRy and yRz -> xRz
• Since relations are also sets
– Set operations also apply
Functions
• A function from a set X to a set of Y, assigns to each element of X
exactly on element of Y
– X is the domain of the function
– Denoted as x ↦ f (x)
–or f(x) =y
– A function from A to B is also a relation f ⊆ A x B
• Example Function as mapping
– Rsquare ⊆ Z x N such that Rsquare(n) = n2
Identifying a function
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
• Primary key is unique Course(id, Course_name, Course_code, Credit_hours, Teacher)
– identifies a single tuple id Course_name Course_code Credit_hours Teacher
Intro to
• If not specified 1 Computer CS1310 4 11
– Some DBMS may create an Science
internal primary key by default
2 Data Structures CS3320 4 22
Discrete
3 MATH2410 3 33
Mathematics
4 Database CS3380 3 44
Relational Model – Foreign Key
Course(id, Course_name, Course_code, Credit_hours, Teacher)
Teacher(id, First_name, Last_name) id Course_name Course_code Credit_hours Teacher
id First_name Last_name Intro to
1 Computer CS1310 4 11
11 Walter White Science
2 Data Structures CS3320 4 22
22 John Smith
Discrete
3 MATH2410 3 33
Mathematics
4 Database CS3380 3 44
Relational Model – Foreign Key
Course(id, Course_name, Course_code, Credit_hours, Teacher)
Teacher(id, First_name, Last_name) id Course_name Course_code Credit_hours Teacher
id First_name Last_name Intro to
1 Computer CS1310 4 11
11 Walter White Science
2 Data Structures CS3320 4 22
22 John Smith
Discrete
3 MATH2410 3 33
Mathematics
4 Database CS3380 3 44
Teaches(Teacher_id, Course_id)
Teacher_id Course_id
11 1
22 2
22 3
22 4
Relational Algebra
• Fundamental operations to retrieve and manipulate tupels in a relation
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
𝜎Tid=‘a1’(T) 𝜎Tid=‘a3’ ∧ Cid=‘12’ (T)
Tid Cid Tid Cid
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
SELECT Tid, Cid*2
FROM T
WHERE Tid=‘a2’;
Relational Algebra - Union
A(Tid, Cid) B(Tid, Cid)
• Syntax Tid Cid Tid Cid
– (A ∪ B)
a1 10 a2 11
• Collect all tuples in both relations a2 11 a3 13
(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
• Collect tuples that appear in both relations a2 11 a3 13
(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)
A.Tid A.Cid B.Tid B.Cid
SELECT * FROM A, B; a1 10 a2 11
a1 10 a3 13
SELECT * FROM A CROSS JOIN B; a2 11 a2 11
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
SELECT * FROM A NATURAL JOIN B;
Next Lecture on Wednesday
• Some useful In-build functions & exercises
– Ordering
– Aggregation
– Grouping
– Having
• DML & DDL exercises
• Lab 1 intoduction
• Project – finding teammates (3 person per team)