0% found this document useful (0 votes)
27 views

Relational Algebra

The document discusses the relational model in a database system course. It covers terminology related to relational data structures like relations, attributes, domains and tuples. It also covers the history of the relational model including Edgar Codd's seminal 1970 paper and the development of IBM's System R in 1975. The document provides examples to illustrate relational data structure concepts.

Uploaded by

Fantasypie 888
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Relational Algebra

The document discusses the relational model in a database system course. It covers terminology related to relational data structures like relations, attributes, domains and tuples. It also covers the history of the relational model including Edgar Codd's seminal 1970 paper and the development of IBM's System R in 1975. The document provides examples to illustrate relational data structure concepts.

Uploaded by

Fantasypie 888
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

Sistem Basis Data

Database system
ISXXXX

Course #1
Module

1 Relational Model

2 Relational Algebra and Relational Calculus


RELATIONAL MODEL
Relational Model
Sub-module

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

Edgar Frank Codd


1970
Published a seminar paper entitled
“A relational model of data for large shared data banks”
Propose the concept of Relational Model
• Able to allow a high degree of data independence
• Able to deal with data semantics, consistency, and
Source picture: computer.org redundancy problems (by proposing normalized relation)
• Able to expand to the data manipulation language

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

Source picture: mcjones.org


Until the late of 1970s, several DBMS are developed:

1970 (source: silicon.co.uk) (source: dbdb.io) 1980s

1975
Relational Model
History of Relational Model

1980s The commercial products based on the relational model started


to appear
Microsoft Visual
Interbase R:base
Access FoxPro
Microsoft Microsoft Embarcadero R:base
Technologies Technologies
Milestones
• Support object-oriented concepts (Stonebraker and Rowe, 1986)
• Support deductive capabilities (Gardarin and Valduriez, 1989)

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

StuID Name Address DeptID DeptID DeptName Faculty


S01 George Chicago D01 D01 Statistics Science
Relation

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

StuID Name Address DeptID DeptID DeptName Faculty


S01 George Chicago D01 D01 Statistics Science
S02 Morgan Seattle D02 D02 Biology Science
S03 Michael New York D02 D03 Chemical Science
S04 Brian Miami D03 D04 Informatics Engineering

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

A set of structure and relationship


of relation in a database
Example:
D(R1, R2, …, Rn)
Database
A relation defined by a set of Schema where
attribute and domain pairs R1, R2, …, Rn are the set of relations
Relation
Example: State
R = {(branchNo: B005, street: 22 Deer A set of relation schemas,
Rd)} each with a distinct name.
Relation
Schema Example:
where R = {R1, R2, …, Rn}
“branchNo” and “street” are attribute
“B005” and “22 Deer Rd” are domain
where
R1, R2, …, Rn are set of relation
Relational Model
Terminology – Mathematical Relations

To understand the true meaning of relation, we


need to review the concept in mathematics

Suppose: D1 = {2, 4} For example:


D2 = {1, 3, 5} R = {(2,1), (4,1)}
The product of D1 and D2:
D1 x D2 = {(2,1), (2,3), (2,5), (4,1), (4,3), (4,5)}
R = {(x,y) | x ∈ D1, y ∈ D2, y=1}
Any subset of this product is a relation
Note
D1 x D2 x … x Dn =
Relational Model
Terminology - Properties of Relations

The relation has a name that is


Each cell of the relation contains
distinct from all other relation
exactly one atomic (single) value
names in the relational schema

Each attribute has a distinct The values of an attribute are all


name from the same domain

Each tuple is distinct; there are The order of attributes has no


no duplicate tuples significance

The order of tuples has no


significance, theoretically
Relational Model
Terminology – Relational Keys

Candidate Primary Foreign


Super key
Set of attributes that
key The minimal of
key The candidate key key An attribute which
that is selected to
uniquely identifies a super key that points to the
identify tuples
tuple within a uniquely identifies a primary key of
uniquely within the
relation tuple in a relation another relation
relation.

Suppose: We have two relations


R1 = {BookID, BookName, Author}
R2 = {GenreID, GenreName, BookID}

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)

Super key R2 Candidate key R2 Primary key R2


• (GenreID) • (GenreID)
• (GenreID, GenreName)
Relational Model
Sub-module

1 3
2 4

Integrity Constraints


Nulls
Entity Integrity 3
• Referential Integrity
• General Constraints
Relational Model
Integrity Constraints

Nulls Entity Integrity


• Represents a value for an attribute that is • In a base relation, no attribute of a primary key
currently unknown or no value has yet been can be null
supplied

Referential Integrity General Constraints


• If a foreign key exists in a relation, either the • Additional rules specified by the users or
foreign key value must match a candidate key database administrators of a database that
value of some tuple in its home relation or the define or constrain some aspect of the
foreign key value must be wholly null enterprise
Relational Model
Integrity Constraints

Example Foreign key is match with


Nulls Referential Integrity Primary key

StuID Name Address DeptID DeptID DeptName Faculty


S01 George Chicago D01 D01 Statistics Science
S02 Morgan null D02 D02 Biology Science
S03 Michael New York D02 D03 Chemical Science
S04 Brian Miami D03 D04 Informatics Engineering

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

Base Schema View

A named relation whose A result of relational


tuples are physically stored operations to produce a new
in the database relation from the existing
relation(s)

A view is a virtual relation that does not necessarily exist in the


database but can be produced upon request by a particular use
Relational Model
Views – Terminology

Example Base relation

branchNo city Street staffNo fName lName branchNo


B001 London Deer Rd S01 John White B001
B002 Aberdeen Argyll St S02 David Ford B002

View

staffNo fName lName staffNo fName lName city


S01 John White S01 John White London
S02 David Ford S02 David Ford Aberdeen
Relational Model
Views – Purposes of View

It provides a powerful and flexible security


mechanism by hiding parts of the database from
certain users

It permits users to access data in a way that is


customized to the user’s needs

It can simplify complex operations on the base


relations
Relational Model
Views – Updating Views

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.

Restrictions on the modification


1) Updates are allowed through a view defined using a simple query involving a single base
relation and containing either the primary key or a candidate key of the base relation
2) Updates are not allowed through views involving multiple base relations
3) Updates are not allowed through views involving aggregation or grouping operations
RELATIONAL ALGEBRA AND RELATIONAL
CALCULUS
Relational Algebra

Selection

Aggregation
and
Projection
grouping
operation
Relational
Algebra
Operations

Division Set
operation operations

Join
operations
Relational Algebra
Selection

Ilustration
Selection

Produces a relation that contains only those tuples of


R that satisfy the specified predicate. The notation is
“σ”.

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

Defines a relation that contains a vertical subset of R,


extracting the values of specified attributes and
eliminating duplicates. The notation is “∏”.
Example
Produce a list of salaries for all staff, showing only the fName,
IName, and salary details
Notation: ∏fName, lName, salary (Staff)
fName lName salary
staffNo fName lName salary
John White 30000
S01 John White 30000
David Ford 12000
S02 David Ford 12000
Susan Brand 9000
S03 Susan Brand 9000
Relational Algebra
Set Operation – Union

Ilustration
Union

Defines a relation that contains all the tuples of one


or more relations, duplicate tuples being eliminated.
The notation is “”.

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

The Set difference operation defines a relation


consisting of the tuples that are in one relation, but
not in other relations. The notation is “”.

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

Defines a relation consisting of the set of all tuples


that are in one relation and the others. The notation is
“”.

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

B01 London P01 House B01 London P01 House

B02 Aberdeen P02 Flat B01 London P02 Flat


B02 Aberdeen P01 House
B02 Aberdeen P02 Flat
Relational Algebra
Join Operation – Theta Join

Ilustration
Theta Join

Defines a relation that contains tuples satisfying the


predicate from the relations. The notation is “⨝θ”
(where or predicate can be <, >, =, ≤, ≥, ≠).

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

B01 London P01 House London B01 London P01 House

B02 Aberdeen P02 Flat London B01 London P02 Flat


Relational Algebra
Join Operation – Natural Join

Ilustration
Natural Join

A theta join of two relations where the is “=“. The


notation is “⨝”. Natural join can be referred as
Equijoin.

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

B01 London P01 House London B01 London P01 House

B02 Aberdeen P02 Flat London B01 London P02 Flat


Relational Algebra
Join Operation – Outer Join

Left
Outer
Join

Outer
Join
Full Right
Outer Outer
Join Join
Relational Algebra
Join Operation – Left Outer Join

Ilustration
Left Outer Join

A join produces a relation that contains all tuples


from the first relation and the subset of the second
relation which has the same value. The notation is
“⟕”.
Example
List all branches with each properties Notation: R ⟕ S
branchNo city propertyNo type city R.branchNo R.city S.propertyNo S.type

B01 London P01 House London B01 London P01 House

B02 Aberdeen P02 Flat London B01 London P02 Flat


B02 Aberdeen
Relational Algebra
Join Operation – Right Outer Join

Ilustration
Right Outer Join

A join produces a relation that contains all tuples


from the second relation and the subset of the first
relation which has the same value. The notation is
“⟖”.
Example
List all properties and the responsible branches Notation: R ⋉ S
branchNo city propertyNo type city R.branchNo R.city S.propertyNo S.type

B01 London P01 House London B01 London P01 House

B02 Aberdeen P02 Flat Miami P02 Flat


Relational Algebra
Join Operation – Full Outer Join

Ilustration
Full Outer Join

A join that combines the result of the left and right


outer join. The notation is “⟗”.

Example
List all branches and properties Notation: R ⟗ S
branchNo city propertyNo type city R.branchNo R.city S.propertyNo S.type

B01 London P01 House London B01 London P01 House

B02 Aberdeen P02 Flat Miami B02 Aberdeen


P02 Flat
Relational Algebra
Division Operation

Ilustration
Division

Defines a relation over the attributes that are not


attributes of the other relations, however consists of
the set of tuples that match the combination of every
tuple in the other relations. The notation is “÷”.

Example
Identify the branch that located in the UK Notation: R ÷ S
branchNo city city branchNo

B01 London London B01

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

P01 London 450000 450000

P02 Miami 505000


P01 London 485000
Relational Algebra
Grouping Operation

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

B01 London 450000 London 2 450000

B02 Miami 505000 Miami 1 505000

B01 London 485000


Relational Algebra
RelaX – Relational Algebra Calculator

https://dbis-uibk.github.io/relax/landing
Relational Algebra
RelaX – Editor Explanation

Relational algebra editor


SQL editor
Group editor
Dataset selector

Relational
notation

Workspace
Query input
field

History of
typed query

Execute query button Download button


Let’s
practice!
Relational Algebra
RelaX – Practice

1 Make a dataset
Relational Algebra
RelaX – Practice

1 Make a dataset
1
2
1 Open Group Editor

2 Click the “add new relation” button


Relational Algebra
RelaX – Practice

1 Make a dataset
1 2

1 Input the relation name


3

4
2 Input the attribute name

Select the data type


3 [number, string, date]

4 Input the record/tuple

5
5 Click OK to finish
Relational Algebra
RelaX – Practice

1 Make a dataset

Continue to make the


other relations

Don’t worry, we will wait…


Take your time
Relational Algebra
RelaX – Practice

1 Make a dataset

To solve this error

We need to give a
name for the dataset

Syntax: “group: [DB name]”


Relational Algebra
RelaX – Practice

1 Make a dataset
After that, another error will appear

We need to input null in line 23


since it was a “blank” tuple

To solve this error


Note
It is better to not input “null” in the “add
new relation editor”, because the system
will treat it as string instead of null
Relational Algebra
RelaX – Practice

1 Make a dataset

1 Click Preview

2 Click “use Group in Editor”


1

2
Relational Algebra
RelaX – Practice

1 Make a dataset

The dataset is ready to use


Relational Algebra
RelaX – Practice

2 Quests Use Relational Algebra Editor to complete these


quests

1 List all staff with a salary


greater than £10000
2 Produce a list of salaries for all
staff, showing only the fName, 3 List all cities where there is
either a branch office or a
IName, and salary details property for rent

4 List all cities where there is a


branch office but no properties 5 List all cities where there is
both a branch office and at 6 List all branches and staffs who
work in each branches
for rent least one property for rent

7 List all branches and properties


for rent which located in the 8 List all branches with each 9 List all properties and the
properties responsible branches
same city

List the address of branches


10 which has property with 3 11 What is lowest price of the
available properties?
rooms
Relational Algebra
RelaX – Practice

2 Quests

1 List all staff with a salary greater than £10000

σsalary > 10000 (Staff)


Relational Algebra
RelaX – Practice

2 Quests

2 Produce a list of salaries for all staff, showing only the fName, IName, and salary details

∏fName, lName, salary (Staff)


Relational Algebra
RelaX – Practice

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

6 List all branches and staffs who work in each branches

σ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

8 List all branches with each properties

Branch ⟕Branch.branchNo =
PropertyForRent.branchNo
PropertyForRent
Relational Algebra
RelaX – Practice

2 Quests

9 List all properties and the responsible branches

Branch ⟖Branch.branchNo =
PropertyForRent.branchNo
PropertyForRent
Relational Algebra
RelaX – Practice

2 Quests

10 List the address of branches which has property with 3 rooms

(∏street,branchNo (Branch))
÷
(∏branchNo (σrooms=3 (PropertyForRent)))
Relational Algebra
RelaX – Practice

2 Quests

11 What is lowest price of the available properties?

γ 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

A set of tuples in a relation

Example: R = {T1, T2, …, Tn} A set of tuples in a database


Relation
where
State Example:
Dst = {val(D1), val(D2), …, val(Dn)}
T1, T2, …, Tn are set of tuples / record
Database
State where
A relation defined by a set of
val(Di) is the value of tuples in
attribute and domain pairs
Relation database D
Example: State
R = {(branchNo: B005, street: 22 Deer A set of relation schemas,
Rd)} each with a distinct name.
Relation
Schema Example:
where R = {R1, R2, …, Rn}
“branchNo” and “street” are attribute
“B005” and “22 Deer Rd” are domain
where
R , R , …, R are set of relation
Relational Model
Representing Relational Database Schema

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

Branch (branchNo, street, city, postcode)


Staff (staffNo, fName, lName, position, gender, DOB, salary)
Client (clientNo, fName, lName, telNo)

You might also like