Relational Algebra
Relational Algebra
Database system
ISXXXX
Course #1
Module
1 Relational Model
Terminology
2 • Relational Data Structure
• Mathematical Relations
• Database Relations
• Properties of Relations
• Relational Keys
• Representing Relational Database Schemas
Views
4 • Terminology
• Purpose of Views
1 1 3 • Updating Views
History of Relational
Model
2 4
Integrity Constraints
•
•
Nulls
Entity Integrity 3
• Referential Integrity
• General Constraints
Relational Model
Sub-module
1 1 3
History of Relational
Model
2 4
Relational Model
History of Relational Model
1970 1980s
1976
Relational Model
History of Relational Model
IBM’s System R
1975
IBM San Jose Research built a database system called System
R that proved the practicality of the relational model
Milestones
Introduce the SQL language as a structured query language
1975
Relational Model
History of Relational Model
1970 1980s
1975
Relational Model
Sub-module
Terminology
2 • Relational Data Structure
• Mathematical Relations
• Database Relations
• Properties of Relations
• Relational Keys
• Representing Relational Database Schemas
1 3
2 4
Relational Model
Terminology – Relational Data Structure
Example Attribute
Cardinality
S02 Morgan Seattle D02 D02 Biology Science
S03 Michael New York D02 D03 Chemical Science
S04 Brian Miami D03 D04 Informatics Engineering
Degree
Relational database
Relational Model
Terminology – Relational Data Structure
Example
Domain Domain
Attribute Domain name Description Domain Definition
StuID Student ID Set of all possible ID Character size: 3, range: S01 – S99
DeptID Department ID Set of all possible ID Character size: 3, range: D01 – D99
Address Student Address Set of all cities in USA Character size: 15
Relational Model
Terminology – Relational Data Structure
Alternative Terminology
Relation Attribute Domain Tuple
The set of
A table with A namedTable
column allowable values Row
A row of a
column and row of a relation for one or more relation
Relation Tuple
attributes
File Record
Relational
Degree Cardinality
database
Column
A collection of
The number of Attribute
The number of relations with
attributes tuples Field distinct relation
names
Relational Model
Terminology – Database Relations
Super key R1
• (BookID, BookName)
• (BookID, Author) Candidate key R1 Primary key R1 Foreign key R2
• (BookID, BookName, Author) • (BookID) • (BookID) • (BookID)
• (BookName, Author) • (BookName, Author)
1 3
2 4
Integrity Constraints
•
•
Nulls
Entity Integrity 3
• Referential Integrity
• General Constraints
Relational Model
Integrity Constraints
Entity Integrity
Primary key cannot be null
Relational Model
Sub-module
Views
4 • Terminology
• Purpose of Views
1 3 • Updating Views
2 4
Relational Model
Views – Terminology
View
Concept
All updates to a base relation should be immediately reflected in all views that
reference that base relation. Similarly, if a view is updated, then the underlying base
relation should reflect the change.
Selection
Aggregation
and
Projection
grouping
operation
Relational
Algebra
Operations
Division Set
operation operations
Join
operations
Relational Algebra
Selection
Ilustration
Selection
Example
List all staff with a salary greater than £10000.
Notation: σsalary > 10000 (Staff)
staffNo fName lName salary
staffNo fName lName salary
S01 John White 30000
S01 John White 30000
S02 David Ford 12000
S02 David Ford 12000
S03 Susan Brand 9000
Relational Algebra
Projection
Ilustration
Projection
Ilustration
Union
Example
List all cities where there is either a branch office or a property for rent Notation: R S
branchNo street city postcode propertyNo city type city
B01 Deer Rd London SW1 4EH P01 London House London
B02 Argyll St Aberdeen AB2 3SU P02 Aberdeen Flat Aberdeen
B03 Main St Glasgow G11 9QX P03 Bristol Flat Glasgow
Bristol
Relational Algebra
Set Operation – Set Difference
Ilustration
Set Difference
Example
List all cities where there is a branch office but no properties for rent Notation: R S
branchNo street city postcode propertyNo city type city
B01 Deer Rd London SW1 4EH P01 London House Bristol
B02 Argyll St Aberdeen AB2 3SU P02 Aberdeen Flat
B03 Main St Glasgow G11 9QX P03 Bristol Flat
Relational Algebra
Set Operation – Intersection
Ilustration
Intersection
Example
List all cities where there is both a branch office and at least one property for rent Notation: R S
branchNo street city postcode propertyNo city type city
B01 Deer Rd London SW1 4EH P01 London House London
B02 Argyll St Aberdeen AB2 3SU P02 Aberdeen Flat Aberdeen
B03 Main St Glasgow G11 9QX P03 Bristol Flat
Relational Algebra
Set Operation – Cartesian Product
Ilustration
Cartesian
Product
Defines a relation that is the concatenation of every
tuple of one relation with every tuple of the other
relations. The notation is “”.
Example
Concatenate all branches and properties for rent Notation: R S
branchNo city propertyNo type Branch.branchNo Branch.city Property.propertyNo Property.type
Ilustration
Theta Join
Example
List all branches and properties for rent which located in the same city Notation: R ⨝R.city = S.city S
branchNo city propertyNo type city R.branchNo R.city S.propertyNo S.type
Ilustration
Natural Join
Example
List all branches and properties for rent which located in the same city Notation: R ⨝ S
branchNo city propertyNo type city R.branchNo R.city S.propertyNo S.type
Left
Outer
Join
Outer
Join
Full Right
Outer Outer
Join Join
Relational Algebra
Join Operation – Left Outer Join
Ilustration
Left Outer Join
Ilustration
Right Outer Join
Ilustration
Full Outer Join
Example
List all branches and properties Notation: R ⟗ S
branchNo city propertyNo type city R.branchNo R.city S.propertyNo S.type
Ilustration
Division
Example
Identify the branch that located in the UK Notation: R ÷ S
branchNo city city branchNo
B02 Miami
B01 London
Relational Algebra
Aggregation Operation
Aggregation
Applies the aggregate function to the attributes or tuples of
a relation. The aggregate functions are COUNT, SUM, AVG,
MIN, and MAX.
Example
What is lowest price of the available properties? Notation: γ minPrice ← MIN(price)
propertyNo city price minPrice
Grouping
Groups the tuples of relation by the grouping attributes and
then applies the aggregate function to define a new
relation.
Example
Find the number of property each city and
identify the lowest price
Notation: γ GROUP(city) and γ minPrice ← MIN(price)
propertyNo city price city numberProperty minPrice
https://dbis-uibk.github.io/relax/landing
Relational Algebra
RelaX – Editor Explanation
Relational
notation
Workspace
Query input
field
History of
typed query
1 Make a dataset
Relational Algebra
RelaX – Practice
1 Make a dataset
1
2
1 Open Group Editor
1 Make a dataset
1 2
4
2 Input the attribute name
5
5 Click OK to finish
Relational Algebra
RelaX – Practice
1 Make a dataset
1 Make a dataset
We need to give a
name for the dataset
1 Make a dataset
After that, another error will appear
1 Make a dataset
1 Click Preview
2
Relational Algebra
RelaX – Practice
1 Make a dataset
2 Quests
2 Quests
2 Produce a list of salaries for all staff, showing only the fName, IName, and salary details
2 Quests
3 List all cities where there is either a branch office or a property for rent
(∏city (Branch))
∪
(∏city (PropertyForRent))
Relational Algebra
RelaX – Practice
2 Quests
4 List all cities where there is a branch office but no properties for rent
(∏city (Branch))
-
(∏city (PropertyForRent))
Relational Algebra
RelaX – Practice
2 Quests
5 List all cities where there is both a branch office and at least one property for rent
(∏city (Branch))
∩
(∏city (PropertyForRent))
Relational Algebra
RelaX – Practice
2 Quests
σBranch.branchNo = Staff.branchNo
(Branch ⨯ Staff)
Relational Algebra
RelaX – Practice
2 Quests
7 List all branches and properties for rent which located in the same city
Branch ⨝Branch.city =
PropertyForRent.city
PropertyForRent
Relational Algebra
RelaX – Practice
2 Quests
Branch ⟕Branch.branchNo =
PropertyForRent.branchNo
PropertyForRent
Relational Algebra
RelaX – Practice
2 Quests
Branch ⟖Branch.branchNo =
PropertyForRent.branchNo
PropertyForRent
Relational Algebra
RelaX – Practice
2 Quests
(∏street,branchNo (Branch))
÷
(∏branchNo (σrooms=3 (PropertyForRent)))
Relational Algebra
RelaX – Practice
2 Quests
γ min_rent←min(rent)
(PropertyForRent)
Homework
Surprise!
• Buatlah database sederhana yang diupload
di github
• Buatlah 15 soal Aljabar Relasional terkait
database yang tadi sudah diupload di github
• Tuliskan jawaban berupa query dan
screenshot hasilnya.
• Dikumpulkan oleh perwakilan masing-
masing kelompok
• Pengumpulan maksimal …………..
Relational Model
Terminology – Database Relations
Concept
The common convention for representing a relation schema is to give the name of
the relation followed by the attribute names in parentheses. Normally, the primary
key is underlined.
Example