Download as pdf or txt
Download as pdf or txt
You are on page 1of 232

Introduction to Informatics

for Students of Other Subjects (TUM-BWL)


IN8005

Social Computing
Prof. Dr. Georg Groh Research Group Fakultät für Informatik

1
Part 0:
Content and Goals of this Module,
Fundamental Terminology of Informatics

2
Informatics: Fundamental Terminology
● Informatics: subject: information + information processing
(representation, storing, mapping, transmission etc.)
with computer-systems.
(--> theory of Informatics, applied Informatics, Computer Science (hardware etc.))

3
Informatics: Fundamental Terminology
● Informatics: subject: information + information processing
(representation, storing, mapping, transmission etc.)
with computer-systems.
(--> theory of Informatics, applied Informatics, Computer Science (hardware etc.))

● Information

4
Informatics: Fundamental Terminology
● Informatics: subject: information + information processing
(representation, storing, mapping, transmission etc.)
with computer-systems.
(--> theory of Informatics, applied Informatics, Computer Science (hardware etc.))

● Information: has a formal representation


(with syntax + semantics) (initial encoding)
in the form of data.

39

5
Informatics: Fundamental Terminology
● Informatics: subject: information + information processing
(representation, storing, mapping, transmission etc.)
with computer-systems.
(--> theory of Informatics, applied Informatics, Computer Science (hardware etc.))

● Information: has a formal representation


(with syntax + semantics) (initial encoding)
in the form of data.

39 = 3 ∗ 10& + 9 ∗ 10)

6
Informatics: Fundamental Terminology
● Informatics: subject: information + information processing
(representation, storing, mapping, transmission etc.)
with computer-systems.
(--> theory of Informatics, applied Informatics, Computer Science (hardware etc.))

● Information: has a formal representation


(with syntax + semantics) (initial encoding)
in the form of data.

● Data: sequences of symbols.

39 = 3 ∗ 10& + 9 ∗ 10)

7
Informatics: Fundamental Terminology
● Informatics: subject: information + information processing
(representation, storing, mapping, transmission etc.)
with computer-systems.
(--> theory of Informatics, applied Informatics, Computer Science (hardware etc.))

● Information: has a formal representation


(with syntax + semantics) (initial encoding)
in the form of data.
1 2 3 4
● Data: sequences of symbols. 38 39 38 40
(--> data-structure: formally defined
organizational form of data for efficient
39 = 3 ∗ 10& + 9 ∗ 10)
processing)

8
Informatics: Fundamental Terminology
● Informatics: subject: information + information processing
(representation, storing, mapping, transmission etc.)
with computer-systems.
(--> theory of Informatics, applied Informatics, Computer Science (hardware etc.))

● Information: has a formal representation


(with syntax + semantics) (initial encoding)
in the form of data.
1 2 3 4
● Data: sequences of symbols. 38 39 38 40
(--> data-structure: formally defined
organizational form of data for efficient
39 = 3 ∗ 10& + 9 ∗ 10)
processing)

● Encoding: Mapping a sequence a of symbols


of an alphabet A to a semantically
equivalent sequence b of symbols from an 100111
alphabet B
9
Informatics: Fundamental Terminology
● Informatics: subject: information + information processing
(representation, storing, mapping, transmission etc.)
with computer-systems.
(--> theory of Informatics, applied Informatics, Computer Science (hardware etc.))

● Information: has a formal representation


(with syntax + semantics) (initial encoding)
in the form of data.
1 2 3 4
● Data: sequences of symbols. 38 39 38 40
(--> data-structure: formally defined
organizational form of data for efficient
39 = 3 ∗ 10& + 9 ∗ 10)
processing)

● Encoding: Mapping a sequence a of symbols


of an alphabet A to a semantically
equivalent sequence b of symbols from an 100111
alphabet B = 1 ∗ 2+ + 0 ∗ 2, + 0 ∗ 2- +
1 ∗ 2. + 1 ∗ 2& + 1 ∗ 2) 10
Mini-Exercise
● 10-system („Decimal Numbers“) (base: 10, ten number symbols:
{0,1,2,3,4,5,6,7,8,9}): example: 4910

● 2-system („Binary Numbers“) (base: 2, two number symbols :


{0,1}): example: 1002 = 410
○ What is the binary encoding / binary representation of 4910 ?
A B C D
1100012 1101002 1100002 1100112
Mini-Exercise
● 10-system („Decimal Numbers“) (base: 10, ten number symbols:
{0,1,2,3,4,5,6,7,8,9}): example: 4910

● 2-system („Binary Numbers“) (base: 2, two number symbols :


{0,1}): example: 1002 = 410
○ What is the binary encoding / binary representation of 4910 ?
A B C D
1100012 1101002 1100002 1100112
Mini-Exercise
● 10-system („Decimal Numbers“) (base: 10, ten number symbols:
{0,1,2,3,4,5,6,7,8,9}): example: 4910

● 2-system („Binary Numbers“) (base: 2, two number symbols :


{0,1}): example: 1002 = 410
○ What is the binary encoding / binary representation of 4910 ?
A B C D
1100012 1101002 1100002 1100112

○ What is the decimal encoding / decimal representation of 1010101012 ?


A B C D
34510 34010 34110 34210
Mini-Exercise
● 10-system („Decimal Numbers“) (base: 10, ten number symbols:
{0,1,2,3,4,5,6,7,8,9}): example: 4910

● 2-system („Binary Numbers“) (base: 2, two number symbols :


{0,1}): example: 1002 = 410
○ What is the binary encoding / binary representation of 4910 ?
A B C D
1100012 1101002 1100002 1100112

○ What is the decimal encoding / decimal representation of 1010101012 ?


A B C D
34510 34010 34110 34210
Mini-Exercise
● 10-system („Decimal Numbers“) (base: 10, ten number symbols:
{0,1,2,3,4,5,6,7,8,9}): example: 4910

● 2-system („Binary Numbers“) (base: 2, two number symbols :


{0,1}): example: 1002 = 410
○ What is the binary encoding / binary representation of 4910 ?
A B C D
1100012 1101002 1100002 1100112

○ What is the decimal encoding / decimal representation of 1010101012 ?


A B C D
34510 34010 34110 34210

● 16-system („Hexa-decimal Numbers“) (Base: 16, sixteen number symbols: :


{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}): example: 2F16 = 4710
○ What is the hexa-decimal encoding / hexa-decimal representation of 4910 ?

A B C D
3116 3416 2AF16 A316
Mini-Exercise
● 10-system („Decimal Numbers“) (base: 10, ten number symbols:
{0,1,2,3,4,5,6,7,8,9}): example: 4910

● 2-system („Binary Numbers“) (base: 2, two number symbols :


{0,1}): example: 1002 = 410
○ What is the binary encoding / binary representation of 4910 ?
A B C D
1100012 1101002 1100002 1100112

○ What is the decimal encoding / decimal representation of 1010101012 ?


A B C D
34510 34010 34110 34210

● 16-system („Hexa-decimal Numbers“) (Base: 16, sixteen number symbols: :


{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}): example: 2F16 = 4710
○ What is the hexa-decimal encoding / hexa-decimal representation of 4910 ?

A B C D
3116 3416 2AF16 A316
Content and Learning Outcomes of the Lecture

Content
● Datenbases and SQL

● Object-Orientierted Programming: Java (+ VBA)

● Algorithms and Data Structures

Learning Outcomes

● Practical capability to use databases and object-oriented


programming languages for the solution of basic problems in
academia and job

● Understand and memorize selected basic concepts and ways of


thinking of Informatics to built upon in advanced lectures and be
able to communicate and collaborate with Informatics co-workers 17
Part I: Databases and SQL

A large share of the slides and elements of slides were taken over from Prof. A. Kemper, TUM,
(http://www3.in.tum.de/teaching/) and the book [1]. These will not all be cited separately for reasons of legibility. 18
Books in German

● [1] A. Kemper , A. Eickler


Datenbanksysteme – Eine Einführung.
Oldenbourg Verlag, 2011. ab 8. Auflage.

● [2] A. Kemper, M. Wimmer


Übungsbuch Datenbanksysteme
Oldenbourg Verlag, ab 3. Auflage, 2011.

19
Books in English

● [3] Avi Silberschatz, Henry F. Korth, S.


Sudarshan: Database System Concepts
McGraw Hill, 2010

20
Part I.1: Introduction and
ER-Modelling

21
Database Management Systems (DBMS)

● Typical Challenge of Informatics: Storing data

● Database Management System (DBMS): Informatics system for storing


data that allows for efficient queries on these data.

● Expectation / Requirements:
− regarding storage and manipulation of data: e.g. security, safety and
reliability
− regarding query processing: e.g. efficiency
− regarding query results: correctness and completeness (usually both
100 % ßà compare information retrieval (< 100 % precision and
recall)

22
Motivation for Using a DBMS

Typical problems of information processing without a DBMS

● redundancy and inconsistency

● limited accessibility and linkability

● problems w.r.t. multi-user operation (e.g. lost update)

● Loss of data (<-> safety (backup, revovery))

● violation of integrity conditions (<-> enforcement)

● security problems

● high costs for application development

23
Levels of Abstraction of a DBMS

Profs Library Administration View1 View2 View3 ...


Tabelle Student
MatrNr VName NName ...
1234
...
Hans
...
Müller
...
...
...
Logical Tier
... ... ... ...

001010101010101000111 Physical Tier

Data Independence

• physical independence
• logical independence
24
Data Modelling

cutout of the
real world
manual/intellectual
modelling
conceptual schema
(ER-schema)
semi-automatic
transformation

Relational XML object-oriented


schema schema schema

25
Data Modelling: Example

Students
Professors
Lectures
real world: university

conceptual modelling

MatrNr PersNr
Students Professors
Name
attend Name
read
LectNr
Lecture
Title 26
Logical Data Models

● Network Model

● Hierarchical Data Model

● Relational Data Model

● XML Schema

● Object Oriented Data Model

● Object Relational Schema

● Deductive Data Model

27
The Relational Data Model

Students attend Lectures


MatrNr Name MatrNr LectNr VorlNr Title
26120 Fichte 25403 5022 5001 Grundzüge
25403 Jonas 26120 5001 5022 Glaube und Wissen
... ... ... ... ... ...

select Name
from Students, attend, Lectures
where Students.MatrNr = attend.MatrNr and
attend.LectNr = Lectures.LectNr and
Lectures.Title = `Grundzüge´;

update Lectures
set Title = `Grundzüge der Logik´
where LectNr = 5001;

28
Database Design: Abstraction Levels

1. Conceptual Level

2. Implementation Level (logical level)

3. Physical Level

29
Phases of Database Design

Information Requirements Analysis


structure
requirements
Requirements Specification

Conceptual Design

ER Schema

DBMS-
Data processing Implementation Design
characteristics
requirements
Logical DB-Structure

Physical Design Hardware/OS-


characteristics
Physical DB-Structure
30
Requirements Analysis

1. Identification of organisational units


(e.g. matriculation office)

2. Identification of tasks that must be supported


(e.g. calculate marks, register for exams)

3. Plan for collecting requirements


(e.g. who do I ask when? Regarding which requirements (e.g. regarding performance?)

4. Collect requirements
(information structure requirements, data processing requirements)

5. Filtering
(e.g. check for completeness, comprehensibility)

6. Classifications
(map information to objects, relations, operations and events)

7. Formalization
(-> result: formal requirements specification)

31
Information Structure Requirements

● objects (e.g. university employees)

● attributes (e.g. salary)

● relations between objects (e.g. examining)

Object Description: University Employee


Object Description: University Employee Relation Description: conduct examination
Object Description: University Employee
• number of instances: 1000
number of instances: 1000
• • attributes: • related objects:
number of instances: 1000
• • attributes: • Professor (as examiner)
• EmployeeNumber
• attributes:
• EmployeeNumber
• type: char • Student (as examinee)
• EmployeeNumber
type: 9char • Lecture (as subject)
• • length:
type: 9char
• length:

• range of values: • attributes of the relation:
• • length:
range of 9 0…999999999
values: 0…999999999 • Date
• number
• range ofofrepetitions:
values: 0
0…999999999
number
• • degree of of repetitions:
definition: 100 0% • Time
• number
• degree of of repetitions:
definition: 100 0% • Mark
• identifying:
• degree yesdefinition: 100
of %
• Salary • • identifying: yes
identifying: yes
• number of instances: 100000 (per year)
• Salary
• type: decimal
• Salary type: (8,2)
decimal
• • length:
type: (8,2)
• length: decimal

• number of repetitions: 0
• length:
number of(8,2)
repetitions:

• degree
• of definition:
number of 90 %0 0
repetitions:
degree ofno
• • identifying: definition: 90 %
degree ofno
• • identifying: definition: 90 %
• identifying: no

32
Data Processing Requirements

Process Description: Creation of Transcript

• frequency: biannual
• required data:
• exams
• examination regulations
• student information
• …
• priority: high
• amount of required data:
• 500 students
• 3000 exams
• 10 examination regulations

33
Phases of Database Design

Information Requirements Analysis


structure
requirements
Requirements Specification

Conceptual Design

ER Schema

DBMS-
Data processing Implementation Design
characteristics
requirements
Logical DB-Structure

Physical Design Hardware/OS-


characteristics
Physical DB-Structure
34
Entity/Relationship-Modelling

● Entity
MatrNr Name Semester
● Relationship
○ binary, ternary, recursive...
Students
● Attribute audience

● Key enrolledIn
○ unique attribute or course
○ unique combination of attributes
Lectures
● Role
LectNr Title Credits

35
University Schema
isPrerequisiteFor
Predecessor Successor
MatrNr LectNr

Name Students enrolledIn Lectures Credits

Semester Title

Mark examine read

EmployeeNr
Rank
Name Assistants workFor Professors Room

ResearchField
EmployeeNr Name
36
Relations as Mathematical Relations
e.g. likes

E1 : Human R E2 : Food
R Í E1 x E2
E1 E2
Hannah Spaghetti
Horst Sushi
Judith R Steak
(Hannah, Sushi)

E1 = {Judith, Hannah, Horst} (Horst, Sushi) E2 = {Spaghetti, Sushi, Steak}


(Judith, Spaghetti)

(Horst, Spaghetti)

R = {(Hannah, Sushi), (Horst, Sushi), (Judith, Spaghetti), (Horst, Spaghetti)}

37
Functionalities
z.B. marriedTo
1 1
E1 (e.g. Men) R E2 (e.g. Women)
R Í E1 x E2

1:1 E1 E2 1:N

N:M
N:1

38
Functionalities
e.g. memberOfParliament
1 N
E1 (e.g. State) R E2 (e.g. Human)
R Í E1 x E2

1:1 E1 E2 1:N

N:M
N:1

39
Functionalities
e.g. read
N 1
E1 (e.g. Lecture) R E2 (e.g. Professor)
R Í E1 x E2

1:1 E1 E2 1:N

N:M
N:1

40
Functionalities
e.g. enrolledIn
N M
E1 (e.g. Lecture) R E2 (e.g. Students)
R Í E1 x E2

1:1 E1 E2 1:N

N:M
N:1

41
Exercise

A car repair shop needs a database. Create an ER model, such that the following entity-
and relationship types and attributes are modelled correctly:

• car with the attributes vehicle identification number, brand, model,


year of manufacture
• customer with the attributes customer number, family name,
telephone number
• repair order with the attributes order number and date
• spare part type with the attributes product code, producer
• regards between car and repair order
• awards between customer and repair order (a repair order is assumed
to be awarded by one customer only)
• requires between repair order and spare part type

42
Exercise
vehicle model brand year of
identification manufacture
number
customer family telephone
number name number

car customer

regards
awards

spare part
requires repair order
type

product order date


producer
code number 43
Exercise
vehicle model brand year of
identification manufacture
number
customer family telephone
number name number

car customer
1
1
regards
awards
N
N

spare part N M
requires repair order
type

product order date


producer
code number 44
Mini Exercise A

● What are the functionalities of the following relation?

(ready to drive)
wheel attachedTo
car

A B C D

wheel 1 attachedTo
1 (ready to drive) wheel N attachedTo
M (ready to drive) wheel
1 attachedTo
N (ready to drive) wheel
N attachedTo
1 (ready to drive)
car car car car
Mini Exercise A

● What are the functionalities of the following relation?

(ready to drive)
wheel attachedTo
car

A B C D

wheel 1 attachedTo
1 (ready to drive) wheel N attachedTo
M (ready to drive) wheel
1 attachedTo
N (ready to drive) wheel
N attachedTo
1 (ready to drive)
car car car car
Mini Exercise B

● What are the functionalities of the following relation?

single
Guest books
room

A B C D

1 1 N M 1 N N 1
guest books single room guest books single room guest books single room guest books single room
Mini Exercise B

● What are the functionalities of the following relation?

single
Guest books
room

A B C D

1 1 N M 1 N N 1
guest books single room guest books single room guest books single room guest books single room

A single room may be rented by different guests at different times


Functionalities of n-ary Relations

Example:
E1
N

P M
E3 R E2

49
Functionalities of n-ary Relations

or:
E1
N

1 M
E3 R E2

50
Partial Functions

in general:
E1
N

P M
En R E2

1
Ek

Function:
R : E1 x ... x Ek-1 x Ek+1 x ... x En ® Ek right-unique
relation

51
Partial Functions

in general:
for some of these
E1 there could also
N be a 1

P M
En R E2

1
Ek

R : E1 x ... x Ek-1 x Ek+1 x ... x En ® Ek

52
Partial Functions - Example

N 1
FÍXxY ; F: X ® Y X F Y

F Í ℤ x ℤ; N 1
ℤ F ℤ
F = { …, (-3,9), (-2,4), (-1,1),
(0,0), (1,1), (2,4), (3,9), … }

F: ℤ® ℤ
F(x) = x2

53
Partial Functions - Example

N 1
FÍXxY ; F: X ® Y X F Y

F Í ℤ x ℤ; 1 1
ℤ F ℤ
F = { (0,0), (1,1), (2,4), (3,9), … }

F: ℕ ® ℕ
F(x) = x2

(injective Function)

54
Partial Functions - Example

N 1
FÍXxY ; F: X ® Y X F Y

F Í ℤ x ℤ; 1 1
ℤ F ℤ
F = { (0,0), (1,1), (2,4), (3,9), … }

F: ℕ ® ℕ of course ℕ Í ℤ
à we could also model: F Í ℕ x ℕ
F(x) = x2
1 1
ℕ F ℕ

(injective Funktion)(but then the aspect of functionalities


would be trivial here)
55
Partial Functions - Example

N 1
FÍXxY ; F: X ® Y X F Y

• a relation # ⊆ %×' is a “partial function” if it is right-


unique: all ( ∈ % have at most one function value
F Í ℤ x ℤ; #(() ∈ ', some ( ∈ % may not have1a function value. 1
ℤ F ℤ
• a partial function # ⊆ %×' is “left total” or “total” (in
F= { (0,0), (1,1),mathematics
(2,4), (3,9), …then
only } often written as “#: % → '“) if all
( ∈ % have a function value #(() ∈ '
• a function is “left unique” or “injective”: all y ∈ ' have at
most one ( ∈ % with 2 = # (
F: ℕ ® ℕ
• a function is “surjective” or ”right total” if all y ∈ ' have
F(x) = x2 at least one ( ∈ % with 2 = # (
• a function is “bijective” if it is surjective and injective ßà
all y ∈ ' have exactly one ( ∈ % with 2 = # (
(injective Function)

56
Example: ternary relation supervise

1 Professors
Students N supervise
1
SeminarTopics

Mark

there are two partial functions:

supervise: SeminarTopics x Students ® Professors


supervise: Professors x Students ® SeminarTopics

57
Example: ternary relation supervise
1 Professors
N
Students supervise
1
Mark SeminarTopics

supervise: SeminarTopics x Students ® Professors


supervise: Professors x Students ® SeminarTopics

Enforced Consistency Constraints:


1. Students may do only one seminar topic with the same professor (in order
to enforce a certain topic broadness) (cmp. 1 at SemiarTopic)

2. Students may submit the same seminar topic only to one professor – they
may not submit a seminar topic to one professor and later submit the same
seminar topic to a different professor (in order to prevent cheating) (cmp. 1
at Professors)

However, the following states are still possible

• Professors may “reuse” seminar topics (issue topics to a couple of students)


• The same topic my be issued by several professors (but then of course to
different students)
58
Example: ternary relation supervise
Professors
Example issue of
the relation supervise p1
b1 p2
Students
b2 p3
s1
b3 p4
s2
s3 b4
t1
s4 b5
t2
b6 t3
t4
Dashed lines mark illegal instances
SeminarTopics 59
Mini Exercise A

● We have the following ER Diagram:

! ⊆ #1×#2×#3×#4 already contains the tuples (aa, bb, cc, dd) and (aa, gg,
cc, ee). Is it legal to add the tuple (aa, bb, ee, ff) to B ?

A B

yes no
Mini Exercise A

● We have the following ER Diagram:

! ⊆ #1×#2×#3×#4 already contains the tuples (aa, bb, cc, dd) and (aa, gg,
cc, ee). Is it legal to add the tuple (aa, bb, ee, ff) to B ?

A B

yes no

Adding the new tuple does not violate any of the


consistency constraints specified by the two partial
functions E1xE2xE3àE4 and E1xE2xE4àE3
Mini Exercise B

● We have the following ER Diagram:

Apples Pears

1 1
B
1 N

Oranges Mangos

! ⊆ Apples × Pears × Oranges × Mangos already contains the tuples


(a1, p17, o9, m6), (a1, p17, o9, m102) and (a4, p5, o45, m6). Is it allowed to add
the tuple (a1, p23, o9, m6) to B? Why / why not?

A B

yes no
Mini Exercise B

● We have the following ER Diagram:

Apples Pears

1 1
B
1 N

Oranges Mangos

! ⊆ Apples × Pears × Oranges × Mangos already contains the tuples


(a1, p17, o9, m6), (a1, p17, o9, m102) and (a4, p5, o45, m6). Is it allowed to add
the tuple (a1, p23, o9, m6) to B? Why / why not?

no: that the tuple (a1, p17, o9, m6) is in B implies that
A B adding (a1, p23, o9, m6) would violate the integrity
condiction specified through the partial function
yes no Apples x Oranges x Mangos à Pears
Mini Exercise B

● We have the following ER Diagram:


further examples:

Apples (a112, p17, o9, m6): adding not allowed; would violate
Pears
the partial fctn. Pears x Oranges x Mangos à Apples

1 1 (a1, p17, o9, m34): adding allowed


B
1 N (a1, p17, o89, m6): adding not allowed; would violate
the partial fctn. Apples x Pears x Mangos à Oranges

Oranges (a22, p11, o9, m102): adding allowed


Mangos

! ⊆ Apples × Pears × Oranges × Mangos already contains the tuples


(a1, p17, o9, m6), (a1, p17, o9, m102) and (a4, p5, o45, m6). Is it allowed to add
the tuple (a1, p23, o9, m6) to B? Why / why not?

no: that the tuple (a1, p17, o9, m6) is in B implies that
A B adding (a1, p23, o9, m6) would violate the integrity
condiction specified through the partial function
yes no Apples x Oranges x Mangos à Pears
Mini-Assignment: Enter the Functionalities!
isPrerequisiteFor
Predecessor Successor
MatrNr LectNr

Name Students enrolledIn Lectures Credits

Semester Title

Mark examine read

EmployeeNr
Rank
Name Assistants workFor Professors Room

ResearchArea
EmployeeNr Name
65
Mini-Assignment: Enter the Functionalities!
isPrerequisiteFor
Predecessor Successor
MatrNr LectNr
N M
N M
Name Students enrolledIn Lectures Credits
N
Semester N Title
M

Mark examine read

EmployeeNr 1
1 Rank
Name Assistants workFor Professors Room
N 1
ResearchArea
EmployeeNr Name
66
(min, max)-Notation
R Í E1 x ... x Ei x ... x En
E1
(min1, max1)
(m
in
n, ma m ax 2)
xn ) (m in 2 ,
En R E2

(mini, maxi)
Ei

For every ei Î Ei there are


• at least mini many tuples of the kind (..., ei , ...)Î R and
• at most maxi many tuples of the kind (..., ei , ...)Î R
67
Example for (min, max)-Notation: Polyeder

Polyeder PolyID
1
Example-
Hull
Polyeder:
N Cube
Face FaceID
N
Boundary
M
Edges EdgeID Example-
Polyeder:
N
Tetraeder
StartEnd X
M
Y
Vertices
Z

68
Example for (min, max)-Notation: Polyeder

Polyeder PolyID
1 (4, *)
Example-
Hull
Polyeder:
N (1,1) Cube
Face FaceID
N (3, *)
Boundary
M (2, 2)
Edges EdgeID Example-
(2, 2) Polyeder:
N
Tetraeder
StartEnd X
M (3, *)
Y
Vertices
Z

69
Relation Hull as Table / Mathematical Relation

Polyeder PolyID
1 (4, *)
Hull
N (1,1)
Face FaceID
Hull
PolyID FaceID
Tetraeder_Horst Face_1
Tetraeder_Horst Face_2
Tetraeder_Horst Face_3
Tetraeder_Horst Face_4
Octaeder_Heiner Fc_a
Octaeder_Heiner Fc_b
Octaeder_Heiner Fc_c
... 70
Weak, Existence-Dependent Entities

Height BldNr RoomNr


Size
1 N
Building lies_in Room

● Relation between “strong” and “weak” type is always 1:N


(or 1:1, rarely)
● Why can’t that be a N:M-relation?
● RoomNr is unique only within a building
● Key of Room is: BldNr and RoomNr.

71
Generalisation

Employees EmployeeNr
Room
Rank Is-a ResearchArea

Professors Assistants

Room EmployeeNr
EmployeeNr
Rank
ResearchArea

Professors Assistants

72
Exercises

73
Exercise A

74
Exercise A

1 1

75
Exercise A

(0,1) (0,1)

1 1

76
Exercise A

1 N

77
Exercise A

(0,*) (0,1)

1 N

78
Exercise A

N 1

79
Exercise A

(0,1) (0,*)

N 1

80
Exercise A

N M

81
Exercise A

(0,*) (0,*)

N M

82
Exercise B

83
Exercise B
à find an
appropriate
example to non-
trivial modelling
overlap between
functionalities and
min-max:

let e.g. E1 := {1,2}, E2 := {a,b}, E3 := {x,y}, and R = {(1, a, x)}.

• N:M:1 functionality e.g. forbids adding the tuple (1, a, y).


• idea: express that forbiddance by setting max1, max2 or max3 to 1:
• max1 = 1. à adding (1, a, y) would be forbidden BUT the tuple (1, b, x) would be also
be forbidden, but would be allowed with the N:M:1 functionality.
• max2 = 1. à adding (1, a, y) would be forbidden BUT adding the tuple (2, a, x) would
also be forbidden, but would be allowed with the N:M:1 functionality.
• max3 = 1. à adding (1, a, y) would not be forbidden according to (min,max) but would
be forbidden due to the N:M:1 functionality AND adding the tuple (1, b, x) would be
forbidden according to (min,max), but would be allowed with the N:M:1 functionality.
84
Exercise B
Further example:

1 Professors
N (0,*)
Students supervise
(0,*) 1
(0,*) SeminarTopics

Mark

there are two partial functions:


supervise: SeminarTopics x Students ® Professors
supervise: Professors x Students ® SeminarTopics

two enforced Consistency Constraints:


Students may submit the same seminar topic only to one professor
Students may do only one seminar topic with the same professor
85
Exercise B
Further example:

1 Professors
N (0,*)
Students supervise
(0,*) 1
(0,*) SeminarTopics

Mark
these have to be like that (e.g. such that
professors can reuse seminar topics) but (as such)
there are two partial functions:
may violate consistency constraints, which can be
supervise: SeminarTopics x Students
modelled®via
Professors
the 1:N:1 functionalities
supervise: Professors x Students ® SeminarTopics

two enforced Consistency Constraints:


Students may submit the same seminar topic only to one professor
Students may do only one seminar topic with the same professor
86
Exercise B
Further example:

1 Professors
1 (0,*)
Assistants supervise
(0,1) 1
(0,1) DissertationTopics

Mark

87
Exercise C

Model a train information system, in which the most important features are
represented: start and destination stations and stations connected
by the train, including arrival and departure times.
Provide functionalities as well as (min, max)!

88
Exercise C

89
Exercise C

90
Exercise C

N 1

91
Exercise C

N 1

92
Exercise C

N 1

1 1

N N

93
Exercise C

N 1

1 1

N N

94
Exercise C

N 1

1 1
1 1

N N N

95
Exercise C

N 1

1 1
(0,*) 1 1 (0,*)

(1,1)
N N N
(1,1)

96
Exercise C

N 1
(1,1) (0,*)
1 1
(0,*) 1 1 (0,*)

(1,1)
N N N
(1,1)

97
Exercise C

N 1
(1,1) (0,*)
1 1
(0,*) 1 1 (0,*)
(1,*) (1,*)

(1,1) (1,*)
N N N
(1,1)

98
Exercise D

In computer graphics, surfaces are usually approximated with


triangles (triangulation)

complement the following incomplete ER diagram for such a triangulation with


functionalities and (min,max)!

triangle boundary line boundary point

99
Exercise D

In computer graphics, surfaces are usually approximated with


triangles (triangulation)

complement the following incomplete ER diagram for such a triangulation with


functionalities and (min,max)!

N M N M
triangle boundary line boundary point
(3,3) (1,2) (2,2) (2,*)

100
Relational Design

101
Phases of Database Design

Information Requirements Analysis


structure
requirements
Requirements Specification

Conceptual Design

ER Schema

DBMS-
Data processing Implementation Design
characteristics
requirements
Logical DB-Structure

Physical Design Hardware/OS-


characteristics
Physical DB-Structure
102
Foundations of the Relational Model

Let A1, A2, … , An be Attributes und


D1, D2, ... , Dn their Domains (ranges of values)

● Mathematical Relation: R Í D1 x ... x Dn


Example: telephone book Í string x string x integer

● Tuple: t Î R
Example: t = („Mickey Mouse“, „Main Street“, 8124711)

● Schema: determines the structure of the stored data


Example: telephone book:
{[Name: string, Street: string, TelephoneNo:integer]}
Notation: sch(R)

103
Foundations of the Relational Model

TelephoneBook
Name:String Street:String TelephoneNo:
integer
Mickey Mouse Main Street 4711
Donald Duck Broadway 95672
... ... ...

• Extension: the current state of the data base


• Key: minimal set of attributes whose values determine a tuple
uniquely
• Primary Key: underlined
– One of the key candidates is chosen as the primary key
– significance: identifying tuples
104
ER University Schema
isPrerequisiteFor
Predescessor Successor
MatrNr LectNr
N M
N M
Name Students enrolledIn Lectures Credits
N
Semester N Title
M

Mark examine read

EmployeeNr 1
1 Rank
Name Assistants workFor Professors Room
N 1
ResearchArea
EmployeeNr Name
105
Relational Representation of Entities

Students: {[MatrNr:integer, Name: string, Semester: integer]}

Lectures: {[LectNr:integer, Title: string, credits: integer]}

Professors: {[EmployeeNr:integer, Name: string, Rank: string, Room: integer]}

Assistants: {[EmployeeNr:integer, Name: string, ResearchArea: string]}

106
Relational Representation of Relations
A 1k1 ... A11
...

E1
AR1

...
R ARkR

A21 An1
E2 En
...
...

...
A 2k 2 A nkn
... ...

R R
R:{[ A11 ,...., A1k , A 21 ,..., A 2k ,..., An1 ,..., Ank , A ,..., A ]}1 kR
$!#!"1 $!#!"2 $!#!"n $!#!
"
Schlüssel
key ofvon
E1 E1 Schlüssel
key of von
E2 E 2 Schlüssel
key of von
En En Attribute
Attributesvon
of R R
107
Relational Representation of Relations: Example
MatrNr LectNr

Name Students enrolledIn Lectures Credits


N M
Semester Title

Students enrolledIn
Lectures
MatrNr ... MatrNr LectNr
26120 5001 LectNr ...
... ...
27550 5001 5001 ...
26120 ...
27550 4052 ... ...
... ... 28106 5041 4052 ...
27550 ... 28106 5052
... ...
... ... … …

primary key of enrolledIn


enrolledIn: {[MatrNr: integer, LectNr: integer]}

foreign keys 108


Relational Representation of Relations: Example 2
EmployeeNr LectNr
Professors read Lectures
... 1 N ...

Professors read Lectures


EmployeeNr ... EmployeeNr LectNr LectNr ...
2125 ... 2137 5001 5001 ...
... ... 2125 5041 5041 ...
... ... ... ... ... ...
2125 5049
... ... 5049 ...
... ...
2137 ... ... ...

primary key of read


Only that is the primary key of
read !!! The related Prof is
read: {[EmployeeNr: integer, LectNr: integer]}
already uniquely determined
via 1:N
foreign keys 109
Refinement of Relational Schemas
EmployeeNr LectNr
Professors read Lectures
... 1 N ...

"#1:N relations (also 1:1 rel.) can be refined:

● initial design:
Lectures: {[LectNr, Title, Credits]}
Professors : {[EmployeeNr, Name, Rank, Room]}
read: {[EmployeeNr, LectNr]} ! read: {[LectNr, EmployeeNr]}

● Refinement via aggregation:


Lectures: {[LectNr, Title, Credits, readBy]}
Professors : {[EmployeeNr, Name, Rank, Room]}

110
Refinement of Relational Schemas
EmployeeNr LectNr
Professors read Lectures
... 1 N ...

"#1:N relations (also 1:1 rel.) can be refined:

● initial design:
Lectures: {[LectNr, Title, Credits]}
Professors : {[EmployeeNr, Name, Rank, Room]}
read: {[EmployeeNr, LectNr]} ! read: {[LectNr, EmployeeNr]}

● Refinement via aggregation:


Lectures: {[LectNr, Title, Credits, readBy]}
Professors : {[EmployeeNr, Name, Rank, Room]}

Rule: Relations with identical keys may be aggregated but


only those and no others!
111
Refinement of Relational Schemas
EmployeeNr LectNr
Professors read Lectures
... 1 N ...

example: extension of Professors and Lectures after Refinement:

Professors Lectures
Employee Name Rank Room LectNr Title Credits readBy
Nr 5001 Foundations 4 2137
2125 Sokrates C4 226 5041 Ethics 4 2125
2126 Russel C4 232 5043 Epistemology 3 2126
2127 Kopernikus C3 310 5049 Mäeutics 2 2125
2133 Popper C3 52 4052 Logic 4 2125
2134 Augustinus C3 309 5052 Philosophy of Science 3 2126
2136 Curie C4 36 … … … …
2137 Kant C4 7

112
Refinement of Relational Schemas

Beware: That is not how it works: (à Anomalies)

Professors Lectures
Employee Name Rank Room reads LectNr Title Credits
Nr 5001 Foundations 4
2125 Sokrates C4 226 5041 5041 Ethics 4
2125 Sokrates C4 226 5049 5043 Epistemology 3
2125 Sokrates C4 226 4052 5049 Mäeutics 2
... ... .... ... ... 4052 Logic 4
2134 Augustinus C3 309 5022 5052 Philosophy of Science 3
2136 Curie C4 36 ??? 5022 Advanced Theology 2
... ... .... ... ... … … …

● Update-Anomaly: What happens if Sokrates moves to another room?


● Delete-Anomaly: What happens if “Advanced Theology“ is removed?
● Insert-Anomaly: Curie is a research prof only and doesn’t teach.
113
Relational Modelling of Weak Entities

N Mark
1
MatrNr Students take Exams
ExamID
N N

encompass conduct

LectNr M M EmployeeNr
Lectures Professors

Exams: {[MatrNr: integer, ExamID: string, Mark: integer]}

114
Relational Modelling of Weak Entities

N Mark
1
MatrNr Students take Exams
ExamID
N N

encompass conduct

LectNr M M EmployeeNr
Lectures Professors

Exams: {[MatrNr: integer, ExamID: string, Mark: integer]}


encompass: {[MatrNr: integer, ExamID: string, LectNr: integer]}
conduct: {[MatrNr: integer, ExamID: string, EmployeeNr: integer]}

115
Relational Modelling of Weak Entities

N Mark
1
MatrNr Students take Exams
ExamID
N N

encompass conduct

LectNr M M EmployeeNr
Lectures Professors

Exams: {[MatrNr: integer, ExamID: string, Mark: integer]}


encompass: {[MatrNr: integer, ExamID: string, LectNr: integer]}
conduct: {[MatrNr: integer, ExamID: string, EmployeeNr: integer]}

Foreign key to a weak entity: the (globally unique) key of the weak entity
Exams, that is MatrNr and ExamID, must be taken over as foreign key into the
relations encompass and conduct. 116
Mini-Exercise A

● We have the following ER Diagram:

has Room
RoomNr State the relational schema for
NrOfBeds the entity Customer and the
relation booked considering a
possible refinement (that is AFTER
Price booked a possible refinement)! Don’t
forget types!

GivenName
PassportNr Customer
FamilyName

Customer: {[PassportNr:string, GivenName:string, FamilyName:string]}


A
booked: {[RoomNr:integer, BookingKey:integer, PassportNr: string, Price:integer]}
Customer: {[PassportNr:integer, FirstName:string, FamilyName:string, Price:integer]}
B
booked: {[RoomNr:integer, PassportNr: string, Price:integer]}
Customer: {[PassportNr:integer, FirstName:string, FamilyName:string, Price:integer,
C
RoomNr:integer, BookingKey:integer]}
Mini-Exercise A

● We have the following ER Diagram:

has Room
RoomNr State the relational schema for
NrOfBeds the entity Customer and the
relation booked considering a
possible refinement (that is AFTER
Price booked a possible refinement)! Don’t
forget types!

GivenName
PassportNr Customer
FamilyName

Customer: {[PassportNr:string, GivenName:string, FamilyName:string]}


A
booked: {[RoomNr:integer, BookingKey:integer, PassportNr: string, Price:integer]}
Customer: {[PassportNr:integer, FirstName:string, FamilyName:string, Price:integer]}
B
booked: {[RoomNr:integer, PassportNr: string, Price:integer]}
Customer: {[PassportNr:integer, FirstName:string, FamilyName:string, Price:integer,
C
RoomNr:integer, BookingKey:integer]}
Mini-Exercise B

● We have the following ER Diagram:

keyOfA keyOfB
A B
1 1

State two possible relational


R
models for the relation R (let the
type of the keys be integer)!
M N

keyOfC C D keyOfD

R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}


A
R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}
R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}
B
R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}
R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}
C
R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}
Mini-Exercise B

● We have the following ER Diagram:

keyOfA keyOfB
A B
1 1

State two possible relational


R
models for the relation R (let the
type of the keys be integer)!
M N

keyOfC C D keyOfD

R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}


A
R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}
R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}
B
R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}
R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}
C
R:{[ keyOfA:integer, keyOfB:integer, keyOfC:integer, keyOfD:integer ]}
Exercise C

train information system:


convert the ER-model into a
rational schema (initial
conversion)!

121
Exercise C

train information system:


convert the ER-model into a
rational schema (initial
conversion)!

• city: {[name: string, state: string, inhabitants: integer]}


• station: {[name: string, number of tracks: integer]}
• train: {[trainID: string, length: integer]}

122
Exercise C

train information system:


convert the ER-model into a
rational schema (initial
conversion)!

• city: {[name: string, state: string, inhabitants: integer]}


• station: {[name: string, number of tracks: integer]}
• train: {[trainID: string, length: integer]}

• is in: {[stationName: string, cityName: string, state: string]}


• start: {[trainID: string, stationName: string]}
• final destination: {[trainID: string, stationName: string]}
• connects: {[fromStation: string, toStation: string, trainID: string, arrives: date, departs: date]}
123
Exercise C

Refine the ER model by


eliminating relations!

1. city: {[name: string, state: string, inhabitants: integer]}


2. station: {[name: string, number of tracks: integer]}
3. train: {[trainID: string, length: integer]}

4. is in: {[stationName: string, cityName: string, state: string]}


5. start: {[trainID: string, stationName: string]}
6. final destination: {[trainID: string, stationName: string]}
7. connects: {[fromStation: string, toStation: string, trainID: string, arrives: date, departs: date]}
124
Exercise C
• city: {[name: string, state: string, inhabitants:
integer]}
• station: {[name: string, number of tracks:
integer, cityName: string, state: string]}
• train: {[trainID: string, length: integer,
startStation: string, finalDestinationStation:
string]}
• connects: {[fromStation: string, toStation:
string, trainID: string, arrives: date, departs:
date]}

1. city: {[name: string, state: string, inhabitants: integer]}


2. station: {[name: string, number of tracks: integer]}
3. train: {[trainID: string, length: integer]}
(4) à (2)
(5) à (3)
4. is in: {[stationName: string, cityName: string, state: string]} (6) à (3)
5. start: {[trainID: string, stationName: string]}
6. final destination: {[trainID: string, stationName: string]}
7. connects: {[fromStation: string, toStation: string, trainID: string, arrives: date, departs: date]}
125
Exercise D
Nr designator name inhabitants

1 electoral N 1 federal
located in
provide an initial district state
relational model 1
and refine!
associated year
N
of 1 N electoral
electoral achieves
sub-district result

voting
Nr is_a
location
# votes # votes
N
receives first vote second vote

1 N
N direct 1 1 political 1
nomination receives
candidate parties

social family
insurance nr name #members
name 126
Exercise D
initial relational model:

1. located in: {[districtNr: integer, stateName: string]}


2. associated: {[subDistrictNr: integer, districtNr: integer]}
3. of: {[candidateSocSecNr: integer, districtNr: integer]}
4. nomination: {[candidateSocSecNr: integer, partyName: string]}

5. electoral district: {[nr: integer, designator: string]}


6. federal state: {[name: string, #inhabitants: integer]}
7. electoral sub-district: {[nr: integer, voting location: string]}
8. direct candidate: {[social insurance nr: integer, family name: string ]}
9. first vote: {[year: integer, subDistrictNr: integer, candidateSocSecNr: integer, #votes: integer]}
10. second vote: {[year: integer, subDistrictNr: integer, partyName: string, #votes: integer]}
11. political parties: {[name: string, #members: ineteger]} 127
Exercise D
initial relational model:

remark: the three weak relationships


receives, achieves, receives are also initially
not modelled separately but right away
absorbed into the entities first vote and
second vote. electoral result is not modeled
separately because it is “absorbed” into first
vote and second vote (because of is_a)
1. located in: {[districtNr: integer, stateName: string]}
2. associated: {[subDistrictNr: integer, districtNr: integer]}
3. of: {[candidateSocSecNr: integer, districtNr: integer]}
4. nomination: {[candidateSocSecNr: integer, partyName: string]}

5. electoral district: {[nr: integer, designator: string]}


6. federal state: {[name: string, #inhabitants: integer]}
7. electoral sub-district: {[nr: integer, voting location: string]}
8. direct candidate: {[social insurance nr: integer, family name: string ]}
9. first vote: {[year: integer, subDistrictNr: integer, candidateSocSecNr: integer, #votes: integer]}
10. second vote: {[year: integer, subDistrictNr: integer, partyName: string, #votes: integer]}
11. political parties: {[name: string, #members: ineteger]} 128
Exercise D
refinement
5’ electoral district: {[nr: integer, designator:
string, stateName: string]}
8’ direct candidate: {[social insurance nr: integer,
family name: string, districtNr: integer]}
11’ political parties: {[name: string, #members:
ineteger, candidateSocSecNr: integer]}
7‘ electoral sub-district: {[nr: integer, voting
location: string, districtNr: integer]}

1. located in: {[districtNr: integer, stateName: string]}


2. associated: {[subDistrictNr: integer, districtNr: integer]}
3. of: {[candidateSocSecNr: integer, districtNr: integer]}
4. nomination: {[candidateSocSecNr: integer, partyName: string]}
(1) à (5)
5. electoral district: {[nr: integer, designator: string]}
(3) à (8)
6. federal state: {[name: string, #inhabitants: integer]} (4) à (11)
7. electoral sub-district: {[nr: integer, voting location: string]} (2) à (7)
8. direct candidate: {[social insurance nr: integer, family name: string ]}
9. first vote: {[year: integer, subDistrictNr: integer, candidateSocSecNr: integer, #votes: integer]}
10. second vote: {[year: integer, subDistrictNr: integer, partyName: string, #votes: integer]}
11. political parties: {[name: string, #members: ineteger]} 129
Mini-Exercise

● How do you get along with the course so far?

I don’t get a thing L I whish god wouldn’t have allowed Informatics to


A
exist on this earth
I understand some things well, some others not so well. However, I
B
have to review stuff at home

C All in all I understand the stuff quite well

D Groh HURRY UP !!!! I’M B O R E D !!!!!


Relational Algebra and SQL

131
The Relational Algebra

minimal algebra syntactic sugar

• s Selection • ÷ Division
• p Projection • Ç Intersection
• x Cartesian Produkt • ⨝ Join
• r Renaming • ⟗ Outer Join
• È Union • ⋉ Semi-Join (right)
• - Set Minus • ⋊ Semi-Join (left)
• ⟕ Left Outer Join
• ⟖ Right Outer Join
• ...

132
Selection

sP : E → E‘ with P: Predicate, E: Relation (Table), E‘: Relation


(Table):
sP (E) selects all Tuples (Rows) of E, which satisfy P.
Result: Relation (Table) E‘

Example: s Semester > 10 (Students)


Students
MatrNr Name Semester sSemester > 10 (Students)
24002 Xenokrates 18
25403 Jonas 12 s MatrNr Name Semester
Semester > 10
26120 Fichte 10 24002 Xenokrates 18
26830 Aristoxenos 8 25403 Jonas 12
27550 Schopenhauer 6
28106 Carnap 3
29120 Theophrastos 2
29555 Feuerbach 2 133
Selection

The Selection predicate P in sP (E) is a (first order logic) formula: it can


be evaluated to true or false and is constructed of

● names of attributes of the argument relation E

● Constants

● arithmetic comparison operators =, <, ≤, >, ≥, ≠

● logic operators ∧ (and), ∨ (or) und ¬ (not)

P is evaluated on all e ∈ E. If P(e) = true, e is included


into the result relation E‘.

134
Projection

PA : E → E‘ with A: sub-set of attributes of E: “chooses columns”

Professors
Employee Name Rank Room
Nr PRank(Professors)
2125 Sokrates C4 226
2126 Russel C4 232 PRank Rank
2127 Kopernikus C3 310 C4
2133 Popper C3 52
C3
2134 Augustinus C3 309
2136 Curie C4 36
2137 Kant C4 7

135
Cartesian Product
example: E‘ = Professors x enrolledIn

Professors enrolledIn E‘= Professors x enrolledIn


Employee Name Rank Room MatrNr LectNr Employee
Nr Name Rank Room MatrNr LectNr
Nr
x =
26120 5001
2125 Sokrates C4 226 27550 5001 2125 Sokrates C4 226 26120 5001
2126 Russel C4 232 2125 Sokrates C4 226 27550 5001
... ...
... ... ... ... ... ... ... ... ... ...
... ...
... ... ... ...
... ... 2125 Sokrates C4 226 25403 5022
2137 Kant C4 7
... ... 2126 Russel C4 232 26120 5001
25403 5022 ... ... ... ... ... ...
2126 Russel C4 232 25403 5022
... ... ... ... ... ...
2137 Kant C4 7 25403 5022

• problem: large results (e.g. |E‘| = |Professors| * |enrolledIn| = 7 * 12 )


• ”better" operation: Join (see later)
136
Notations

● sch(E): set of all attributes of E


e.g.: sch(Professors) = {EmployeeNr, Name, Rank, Room}

● E.A : qualified name of the attribute A in relation (table) E


e.g.: Professors.EmployeeNr, Assistants.EmployeeNr

Professors Assistants
Employee Name
Nr
Rank Room x Employee
Nr
Name ResearchArea Boss
=
... ... ... ... ... ... ... ...

Professoren x Assistants
Professors.Employee Professors.Name Rank Room Assistants.Employee Assistants.Name ResearchArea Boss
Nr Nr
... ... ... ... ... ... ... ...

137
Notations

● sch(E): set of all attributes of E


e.g.: sch(Professors) = {EmployeeNr, Name, Rank, Room}

● E.A : qualified name of the attribute A in relation (table) E


e.g.: Professors.EmployeeNr, Assistants.EmployeeNr

● e.A : for tuples (rows) e ∈ E : value of the attribute A in e

e.g.: (2125, Sokrates, C4, 226).Name = Sokrates

● e.{A1,A2,...} : (analogous)

e.g.: (2125, Sokrates, C4, 226).{Name,Rank} = (Sokrates, C4)


138
Renaming
either
● of attributes: example:
rprecursor ¬ predecessor(isPrerequisiteFor)

● of whole relations: example:


riPF1 (isPrerequisiteFor)

139
Renaming

why? Example: determining indirect predecessors (2nd level) of


the lecture 5216:

PiPF1. Predecessor( siPF2.Succesor=5216 Ù iPF1.Successor = iPF2.Predecessor


( riPF1 (isPrerequisiteFor) x riPF2 (isPrerequisiteFor) ) )

riPF1 (isPrerequisiteFor) x riPF2 (isPrerequisiteFor)


iPF1 iPF2
Predecessor Successor Predecessor Successor
5001 5041 5001 5041
... ... ... ...
5001 5041 5041 5216
... ... ... ...
5052 5259 5052 5259

140
Union
(required: identical schema in both relations!)

Example: E‘ = Π EmployeeNr, Name (Assistants) !""Π EmployeeNr, Name (Professors)


E‘

ΠEmployeeNr, Name (Professors) ΠEmployeeNr, Name (Assistants) EmployeeNr Name

PersNr Name PersNr Name 2125 Sokrates


2126 Russel

=
2125 Sokrates 3002 Platon

2126 Russel
!" 3003 Aristoteles 2127 Kopernikus

3004 Wittgenstein 2133 Popper


2127 Kopernikus
3005 Rhetikus 2134 Augustinus
2133 Popper
3006 Newton 2136 Curie
2134 Augustinus
3007 Spinoza 2137 Kant
2136 Curie
3002 Platon
2137 Kant 3003 Aristoteles
3004 Wittgenstein
3005 Rhetikus
3006 Newton
3007 Spinoza
141
Set Minus, Intersection

(analogous to union)

● Example for set minus:


MatrNr of Students that have not taken an exam:

ΠMatrNr (Students) - ΠMatrNr (examine)

● Example for intersection:


Find employeeNr of all C4 Professors that read at least one lecture:

PEmployeeNr(rEmployeeNr¬readBy(Lectures)) Ç PEmployeeNr(sRank=C4(Professors))

intersection may be expressed via set minus: R Ç S = R - (R - S) 142


Natural Join

given: two relations R and S: (regard overlapping attributes Bi)


R(A1,..., Am, B1,..., Bk)
S(B1,..., Bk, C1,..., Cn)
then we define the Join R ⨝ S as

R ⨝ S = PA1,..., Am, R.B1,..., R.Bk, C1,..., Cn(sR.B1=S.B1 Ù...Ù R.Bk = S.Bk(RxS))

R⨝S
R-S RÇS S-R
A1 A2 ... Am B1 B2 ... Bk C1 C2 ... Cn
... ... ... ... ... ... ... ... ... ... ... ...
... ... ... ... ... ... ... ... ... ... ... ...
143
Natural Join
Example: Students ⨝ enrolledIn:
Students enrolledIn Students ⨝ enrolledIn
MatrNr Name Semester MatrNr LectNr Name Semester MatrNr LectNr
24002 Xenokrates 18 26120 5001 Fichte 10 26120 5001
Schopenhauer 6 27550 5001
25403 Jonas 12 27550 5001
Schopenhauer 6 27550 4052
26120 Fichte 10 27550 4052
26830 Aristoxenos 8 ⨝ 28106 5041 = Carnap
Carnap
3
3
28106
28106
5041
5052
27550 Schopenhauer 6 28106 5052 Carnap 3 28106 5216
28106 Carnap 3 28106 5216 Carnap 3 28106 5259
29120 Theophrastos 2 28106 5259 Theophrastos 2 29120 5001
29555 Feuerbach 2 29120 5001 Theophrastos 2 29120 5041
29120 5041 Theophrastos 2 29120 5049
Feuerbach 2 29555 5022
29120 5049
Jonas 12 25403 5022
29555 5022
25403 5022

Remark: Join is associative: (A ⨝ B) ⨝ C = A ⨝ (B ⨝ C)


144
Theta-Join

Let us have:
R(A1,..., An)
S(B1,..., Bm)

then we define the Theta-Join R ⨝Θ S (Join with Join-Prädikat !"#as:

R ⨝Θ S := $Θ (R x S) R ⨝q S
R S
A1 A2 ... An B1 B2 ... Bm
● Example:

R ⨝A1>B1 ∧ A3<B2 S

145
Relational Division

● Definition: t Î R ÷ S if for each tuple ts Î S a tuple tr Î R exists


such that:

∀ A ∈ sch(S) : tr.A = ts.A und tr.(sch(R) – sch(S)) = t

Example: R S R÷S
M V
÷ V = M
m1 v1 v1 m1
m1 v2 v2
m1 v3
m2 v2
m2 v3

● we have: R ÷ S = P(R - S)(R) - P (R - S)((P (R - S)(R) x S) - R)


146
Mini-Exercise

● What is the result of R ÷ S?

C
Mini-Exercise

● What is the result of R ÷ S?

C
Grouping and Aggregation

● γ not part of core relational algebra but nevertheless a very


important operator (ßà select with groupby )

● γA; f (E) groups according to attribute (or set of attributes) A and


applies to each group an Aggregate Function (or set of aggregate
functions) f . (possible aggregate functions: count(), max(),
min(), sum(), avg() etc.)

gSemester; count(*)(Students) greadBy; count(*),sum(Credits)(Lectures)


gSemester;count(*)(Students) greadBy; count(*),sum(Credits)(Lectures)
Semester count(*) readBy count(*) sum(Credits)
18 1 2125 3 10
12 1 2126 3 8
10 1 2133 1 2
8 1 2134 1 2
6 1 2137 2 8
3 1
2 2
149
Exercises

150
Exercise A
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find all students that got a 1
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 by Sokrates!
5043 Epistemology 3 2126 26120 Fichte 10
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find all students that got a 1
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 by Sokrates!
5043 Epistemology 3 2126 26120 Fichte 10
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find all students that got a 1
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 by Sokrates!
5043 Epistemology 3 2126 26120 Fichte 10
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134
2136
● How many columns does the answer-table of this partial expression have?
Augustinus
Curie
C3
C4
309
36
5052 5259

2137 Kant C4 7 enrolledIn


MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 A B C D
28106 5001 2126 1 25403 5022
25403
27550
5041
4630
2125
2137
2
2
8 7 4 1
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find all students that got a 1
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 by Sokrates!
5043 Epistemology 3 2126 26120 Fichte 10
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134
2136
● How many columns does the answer-table of this partial expression have?
Augustinus
Curie
C3
C4
309
36
5052 5259

2137 Kant C4 7 enrolledIn


MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 A B C D
28106 5001 2126 1 25403 5022
25403
27550
5041
4630
2125
2137
2
2
8 7 4 1
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find all students that got a 1
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 by Sokrates!
5043 Epistemology 3 2126 26120 Fichte 10
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134
2136
● How many rows does the answer-table of this partial expression have?
Augustinus
Curie
C3
C4
309
36
5052 5259

2137 Kant C4 7 enrolledIn


MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 A B C D
28106 5001 2126 1 25403 5022
25403
27550
5041
4630
2125
2137
2
2
8 7 4 1
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find all students that got a 1
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 by Sokrates!
5043 Epistemology 3 2126 26120 Fichte 10
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134
2136
● How many rows does the answer-table of this partial expression have?
Augustinus
Curie
C3
C4
309
36
5052 5259

2137 Kant C4 7 enrolledIn


MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 A B C D
28106 5001 2126 1 25403 5022
25403
27550
5041
4630
2125
2137
2
2
8 7 4 1
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find all students that got a 1
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 by Sokrates!
5043 Epistemology 3 2126 26120 Fichte 10
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
● How many rows does the answer-table of this partial expression have?
2134
2136
Augustinus
Curie
C3
C4
309
36
5052 5259

2137 Kant C4 7 enrolledIn


MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 A B C D
28106 5001 2126 1 25403 5022
25403 5041 2125 2 1 2 21 0
27550 4630 2137 2
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find all students that got a 1
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 by Sokrates!
5043 Epistemology 3 2126 26120 Fichte 10
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
● How many rows does the answer-table of this partial expression have?
2134
2136
Augustinus
Curie
C3
C4
309
36
5052 5259

2137 Kant C4 7 enrolledIn


MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 A B C D
28106 5001 2126 1 25403 5022
25403 5041 2125 2 1 2 21 0
27550 4630 2137 2
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find professors whose
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 lectures depend only on self-
5043 Epistemology 3 2126 26120 Fichte 10 read direct predecessors!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find professors whose
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 lectures depend only on self-
5043 Epistemology 3 2126 26120 Fichte 10 read direct predecessors!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001 Remark: We were looking for all those profs p which
examine 29120 5041 only read lectures for which all direct predecessors
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
are also read by p.
28106 5001 2126 1 25403 5022 This includes profs that do not read any lectures at all
25403 5041 2125 2
27550 4630 2137 2 or that read lectures which do not have predecessors.
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find professors whose
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 lectures depend only on self-
5043 Epistemology 3 2126 26120 Fichte 10 read direct predecessors!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049 Schema of table Professors
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001 Remark: We were looking for all those profs p which
examine 29120 5041 only read lectures for which all direct predecessors
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
are also read by p.
28106 5001 2126 1 25403 5022 This includes profs that do not read any lectures at all
25403 5041 2125 2
27550 4630 2137 2 or that read lectures which do not have predecessors.
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find professors whose
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 lectures depend only on self-
5043 Epistemology 3 2126 26120 Fichte 10 read direct predecessors!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
● How many columns does the answer-table of this partial expression have?
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001 Remark: We were looking for all those profs p which
examine 29120 5041 only read lectures for which all direct predecessors
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 A are also readBby p. C D
28106 5001 2126 1 25403 5022 This includes profs that do not read any lectures at all
25403
27550
5041
4630
2125
2137
2
2
8 or that read7lectures which4do not have predecessors.
1
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find professors whose
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 lectures depend only on self-
5043 Epistemology 3 2126 26120 Fichte 10 read direct predecessors!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
● How many columns does the answer-table of this partial expression have?
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001 Remark: We were looking for all those profs p which
examine 29120 5041 only read lectures for which all direct predecessors
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 A are also readBby p. C D
28106 5001 2126 1 25403 5022 This includes profs that do not read any lectures at all
25403
27550
5041
4630
2125
2137
2
2
8 or that read7lectures which4do not have predecessors.
1
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find professors whose
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 lectures depend only on self-
5043 Epistemology 3 2126 26120 Fichte 10 read direct predecessors!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
● How many rows does the answer-table of this
EmployeeNr Name Rank Room
2125 Sokrates C4 226
5001 5043 partial expression have?
5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001 Remark: We were looking for all those profs p which
examine 29120 5041 only read lectures for which all direct predecessors
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 A are also readBby p. C D
28106 5001 2126 1 25403 5022 This includes profs that do not read any lectures at all
25403
27550
5041
4630
2125
2137
2
2
70 or that read10
lectures which1do not have predecessors.
7
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find professors whose
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 lectures depend only on self-
5043 Epistemology 3 2126 26120 Fichte 10 read direct predecessors!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
● How many rows does the answer-table of this
EmployeeNr Name Rank Room
2125 Sokrates C4 226
5001 5043 partial expression have?
5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001 Remark: We were looking for all those profs p which
examine 29120 5041 only read lectures for which all direct predecessors
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 A are also readBby p. C D
28106 5001 2126 1 25403 5022 This includes profs that do not read any lectures at all
25403
27550
5041
4630
2125
2137
2
2
70 or that read10
lectures which1do not have predecessors.
7
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find students that attend
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 lectures for which they do
5043 Epistemology 3 2126 26120 Fichte 10 not have the correct
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6 prerequisites!
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
Find students that attend
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 lectures for which they do
5043 Epistemology 3 2126 26120 Fichte 10 not have the correct
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6 prerequisites!
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
2125 Sokrates C4 226
5001
5001
5043
5049
We construct a hypothetical instance
2126
2127
Russel
Kopernikus
C4
C3
232
310
5041 5216 of enrolledIn that would be valid if all
5043 5052
2133 Popper C3 52 5041 5052 students would also be enrolledIn all
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36 required prerequisite lectures.
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
We then subtract from this instance
EmployeeNr Name ResearchArea Boss
3002 Platon Ideology 2125
27550 5001 the actual instance of enrolledIn, such
27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041 that the sought for students remain.
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
Structured Query Language (SQL)

● „Implementation“ of relational algebra for DBMS

● consists of :
○ Data Definition Language (DDL): creates table structures
○ Data Manipulation Language (DML): delete, insert, modify etc.
‒ Query Language (QL): search

● one “recent” standard: SQL99

168
Students
Professors Lectures
EmployeeNr Name Rank Room MatrNr Name Semester
LectlNr Title Credits readBy
2125 Sokrates C4 226 24002 Xenokrates 18 5001 Foundations 4 2137
2126 Russel C4 232 25403 Jonas 12 5041 Ethics 4 2125
2127 Kopernikus C3 310 26120 Fichte 10 5043 Epistemology 3 2126
2133 Popper C3 52 26830 Aristoxenos 8 5049 Mäeutics 2 2125
2134 Augustinus C3 309 27550 Schopenhauer 6 4052 Logic 4 2125
2136 Curie C4 36 28106 Carnap 3 5052 Philosophy of Science 3 2126
2137 Kant C4 7 29120 Theophrastos 2 5216 Bio-Ethics 2 2126
29555 Feuerbach 2 5259 The Wiener Circle 2 2133
isPrerequisiteFor 5022 Advanced Theology 2 2134
Predecessor Successor enrolledIn
4630 The Three Critics 4 2137
5001 5041 MatrNr LectNr
5001 5043 26120 5001
5001 5049 27550 5001 Assistants
5041 5216 27550 4052 EmployeeNr Name ResearchArea Boss
5043 5052 28106 5041 3002 Platon Ideology 2125
5041 5052 28106 5052 3003 Aristoteles Syllogistics 2125
5052 5259 28106 5216 3004 Wittgenstein Linguistics 2126
28106 5259 3005 Rhetikus Celestial Mechanics 2127
examine 3006 Newton Keplerian Laws 2127
29120 5001
MatrNr LectNr EmployeeNr Mark 3007 Spinoza God and Nature 2126
29120 5041
28106 5001 2126 1
29120 5049
25403 5041 2125 2
29555 5022
27550 4630 2137 2
25403 5022
Data Definition Language: Data Types
corresponds to
relational modelling up
Data Types in SQL:
to now:

● char (n), varchar (n) ● string


● integer ● integer
● numeric(p,s) ● ?
● blob for large binary data ● ?
● clob for large string attributes ● ?
● date ● ?
● xml for XML documents ● ?

170
Data Definition Language: Create and Alter

create integrity constraints

create table Proessors


(EmployeeNr integer NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name varchar(30) not null,
Rank character(2),
Room integer);

alter
• ALTER TABLE Proessors RENAME TO Professors;
• ALTER TABLE Professors ADD familyStatus varchar(50);
• ALTER TABLE Professors DROP COLUMN city;
• ALTER TABLE Professors MODIFY (Name varchar(50));

171
Data Manipulation Language: Insert

insert into Students (MatrNr, Name)


values (28121, `Archimedes‘);

value ‘null’

Studenten
MatrNr Name Semester

29120 Theophrastos 2
29555 Feuerbach 2
28121 Archimedes -

172
Data Manipulation Language: delete and update

deletion of tuples
delete from Students
where Semester > 13;

modifying tuples
update Students
set Semester = Semester + 1;

173
Query Language: select

Example: simple SQL-Query

Result:

select EmployeeNr, Name EmployeeNr Name


2125 Sokrates
from Professors 2126 Russel
where Rank= ´C4´; 2136 Curie
2137 Kant

174
select <---> Translation into Relational Algebra

general (unnested) translation into


SQL-query : relational algebra:

select A1, ..., An


P A1, ..., An (sP (R1 x ... x Rk ))
from R1, ..., Rk
where P;

175
Queries Using Several Relations (Tables)

example: which professor reads Mäeutics?

select Name, Title


from Professors, Lectures
where EmployeeNr=readBy and Title=‘Mäeutics’;

PName,Title (sEmployeeNr=readBy !Title=‘Mäeutik’(Professors x Lectures))

176
select Name, Title
from Professors, Lectures PName,Title ( sEmployeeNr=readBy !Title=‘Mäeutik’(Professors x Lectures))
where EmployeeNr=readBy and Title=‘Mäeutik’;

Lectures
Professors
VorlNr Title Credits readBy
Employee Name Rank Room
Nr 5001 Foundations 4 2137
2125 Sokrates C4 226 5041 Ethics 4 2125
2126 Russel C4 232 ... ... ... ...
... ... ... ... 5049 Mäeutics 2 2125
... ... ... ... ... ... ... ...
2137 Kant C4 7 4630 The Three Critics 4 2137

x
Professors x Lectures
EmployeeNr Name Rank Room VorlNr Title Credits readBy
2125 Sokrates C4 226 5001 Foundations 4 2137
2125 Sokrates C4 226 5041 Ethics 4 2125
... ... ... ... ... ... ... ...
2125 Sokrates C4 226 5049 Mäeutics 2 2125
2126 Russel C4 232 5001 Foundations 4 2137
... ... ... ... ... ... ... ...
2137 Kant C4 7 4630 The Three Critics 4 2137
177
select Name, Title
from Professors, Lectures PName,Title ( sEmployeeNr=readBy !Title=‘Mäeutik’(Professors x Lectures))
where EmployeeNr=readBy and Title=‘Mäeutik’;

Professors x Lectures
EmployeeNr Name Rank Room VorlNr Title Credits readBy
2125 Sokrates C4 226 5001 Foundations 4 2137
2125 Sokrates C4 226 5041 Ethics 4 2125
... ... ... ... ... ... ... ...
2125 Sokrates C4 226 5049 Mäeutics 2 2125
2126 Russel C4 232 5001 Foundations 4 2137
... ... ... ... ... ... ... ...
2137 Kant C4 7 4630 The Three Critics 4 2137

Selection
sEmployeeNr=readBy !Title=‘Mäeutics’(Professors x Lectures)
EmployeeNr Name Rank Room VorlNr Title Credits readBy
2125 Sokrates C4 226 5049 Mäeutics 2 2125

Projection

PName,Title ( sEmployeeNr=readBy !Title=‘Mäeutics’(Professors x Lectures))


Name Title
Sokrates Mäeutics
178
Queries Using Several Relations (Tables): Tuple Variables

example: which students attend which lectures?

select Name, Title


from Students, enrolledIn, lectures
where Students.MatrNr = enrolledIn.MatrNr and
enrolledIn.LectNr = Lectures.LectNr;

alternatively: (recommended): use tuple variables:

select s.Name, le.Title


from Students s, enrolledIn e, Lectures le
where s. MatrNr = e.MatrNr and
e.LectNr = le.LectNr

(equivalent to ρ operator) 179


Exercise

State a select statement equivalent to the following relational algebra expression!


Exercise

State a select statement equivalent to the following relational algebra expression!


Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
write a select statement to
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 find all students that got a 1
5043 Epistemology 3 2126 26120 Fichte 10 by Sokrates!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
182
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
write a select statement to
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 find all students that got a 1
5043 Epistemology 3 2126 26120 Fichte 10 by Sokrates!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 select s.name, s.MatrNr
Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137
from Students s, examine e, Professors p
isPrerequisiteFor

Professors
Predecessor Successorwhere p.Name=’Sokrates’
5001 5041 and p.PersNr = e.PersNr
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049 and e.Mark = 1
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052 and e.MatrNr = s.MatrNr;
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
183
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
write a select statement to
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 find all students that got a 1
5043 Epistemology 3 2126 26120 Fichte 10 by Sokrates!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 select s.name, s.MatrNr
Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137
from Students s, examine e, Professors p
isPrerequisiteFor

Professors
Predecessor Successorwhere p.Name=’Sokrates’
5001 5041 and p.PersNr = e.PersNr
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049 and e.Mark = 1
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052 and e.MatrNr = s.MatrNr;
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
184
Set Operations union, intersect, minus

● example for union:

( select Name Π!"#$%&''(')"*)'+,-,


from Assistants ) Π!"#$%./01$''0/'+
union
( select Name
from Professors);

● intersect, minus: analogously

185
Nested Sub-Queries: exists

existential quantifier exists

example: professors that do not read lectures:

select p.Name
from Professors p
where not exists ( select *
from Lectures le
where le.readBy = p.EmployeeNr );

186
Nested Sub-Queries: exists

existential quantifier exists

example: professors that do not read lectures:

select p.Name Correlation


from Professors p
where not exists ( select *
from Lectures le
where le.readBy = p.EmployeeNr );

187
Nested Sub-Queries: exists

existential quantifier exists

example: professors that do not read lectures:

select p.Name Correlation


from Professors p
where not exists ( select *
from Lectures le
where le.readBy = p.EmployeeNr );

remark: of course, in this case it would be possible to de-correlate


both via join, e.g. with the equivalent of

188
Nested Sub-Queries: in

example: professors that do not read lectures:

select p.Name Correlation


from Professors p
where not exists ( select *
from Lectures le
where le.readBy = p.EmployeeNr );

variant with in

select p.Name
from Professors p
where p.EmployeeNr not in ( select readBy
from Lectures);
189
Nested Sub-Queries: in

example: professors that do not read lectures:

select p.Name Correlation


from Professors p
where not exists ( select *
from Lectures le
where le.readBy = p.EmployeeNr );

variant with in

select p.Name No Correlation


from Professors p (more efficient)
where p.EmployeeNr not in ( select readBy
from Lectures);
190
Exercise A

Decorrelate the following query!

select p.Name
from Professors p
where not exists ( select *
from examine e
where e.EmployeeNr = p.EmployeeNr );

191
Exercise A

Decorrelate the following query!

select p.Name
from Professors p
where not exists ( select *
from examine e
where e.EmployeeNr = p.EmployeeNr );

select p.Name
from Professors p
where p.EmployeeNr not in (
select e.EmployeeNr
from examine e );

192
Exercise A
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
write a select statement to
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 find names of professors
5043 Epistemology 3 2126 26120 Fichte 10 which lecture “Quantum
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6 Theory” and have already
5052 Philosophy of Science 3 2126 28106 Carnap 3 given a grade of 6 for it!
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
193
Exercise A
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
write a select statement to
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 find names of professors
5043 Epistemology 3 2126 26120 Fichte 10 which lecture “Quantum
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6 Theory” and have already
5052 Philosophy of Science 3 2126 28106 Carnap 3 given a grade of 6 for it!
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria
select distinct
4 2137 isPrerequisiteForp.Name

Professors
from Lectures
Predecessor Successor v, Professors p, examine pr
Rank Room where v.readBy = p.employeeNr
5001 5041
EmployeeNr Name
5001 5043
2125 Sokrates C4 226 and 5001
v.Title=’Quantum
5049 Theory’
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 and 5043
v.LectNr 5052 = pr.LectNr
2133 Popper C3 52
2134 Augustinus C3 309
and 5041
Mark=6;
5052
5052
5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
or MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon select p.Name
Ideology
2125 27550 4052
3003 Aristoteles Syllogistics
2125
3004 Wittgenstein
from
Linguistics
2126
Professors p 28106 5041
28106 5052
3005 where exists ( select *
Rhetikus Celestial Mechanics
2127
28106 5216
3006 Newton Keplerian Laws
2127
2126 from Lectures v, examine pr
28106 5259
3007 Spinoza God and Nature
29120 5001
examine where v.readBy = p.employeeNr
29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark and v.Title=’Quantum Theory’
29555 5022
28106
25403
5001
5041
2126
2125
1
2
and v.LectNr = pr.LectNr
25403 5022

27550 4630 2137 2 and Mark=6 ); 194


Comparison with "all"

Example: Find the students with the longest study times!


select Name
from Students
where Semester >= all ( select Semester
from Students);

195
Comparison with "all"

Example: Find the students with the longest study times!


select Name
from Students
where Semester >= all ( select Semester
from Students);

Remark: Usually only distinct results are reported automatically. E.g. for the
query select Semester from Students the number ’2’ is only
included once in the result, although there are maybe 500 students in the
second semester. You can enforce this behaviour by stating
select distinct Semester from Students

196
Comparison with "all"

Example: Find the students with the longest study times!


select Name
from Students
where Semester >= all ( select Semester
from Students);
more efficient:
select Name
from Students
where Semester >= ( select max(Semester)
from Students);

197
Comparison with "all"

Example: Find the students with the longest study times!


select Name
If using aggregate functions without attributes to group
from Students
by (that is applying the !-Operator without an attribute
where Semester
list but with >= allof)( select
a (list Semester the missing
aggregate-function(s)),
grouping will result in from
one group containing all tuples
Students);
and the aggregate function(s) is (are) applied to this one
group.
more efficient:
In this sub-query, max(Semester) is applied to the ONE
group that contains ALL the students.
select Name
from Students
where Semester >= ( select max(Semester)
from Students);

198
Comparison with "all"

Example: Find the students with the longest study times!


select Name
from Students
where Semester >= all ( select Semester
from Students);
more efficient:
select Name
from Students
where Semester >= ( select max(Semester)
from Students);

Remark (1) : besides all we also have any


Remark (2): all does NOT correspond to the all-quantifier ∀ (from
logics). “Students that are enrolled in all 4 hour lectures”
Aggregate Functions and Grouping

● aggregate functions: avg, max, min, count, sum, median


Example:
select avg(Semester) from Students;

● Grouping:

Example:
select readBy, sum (Credits)
from Lectures
group by readBy;

200
Aggregate Functions and Grouping

grouping: example 2: which C4 professor reads long


lectures on average?

select readBy, Name, sum(Credits)


from Lectures, Professors
where readBy= EmployeeNr and Rank = 'C4'
group by readBy, Name
having avg(Credits) >= 3;

201
Aggregate Functions and Grouping

grouping: example 2: which C4 professor reads long


lectures on average? aggregate fct.:
evaluated
separately for
select readBy, Name, sum(Credits) each group

from Lectures, Professors


where readBy= EmployeeNr and Rank = 'C4'
group by readBy, Name
having avg(Credits) >= 3;

selects only
as before: selects
certain groups
certain tuples from
the Cartesian
product (Theta Join
or Selection)

202
Lectures x Professors select readBy, Name, sum(Credits)
from Lectures, Professors
LectNr Title Credits readBy EmployeeNr Name Rank Room where readBy = EmployeeNr and Rank = 'C4'
group by readBy, Name
5001 Foundations 4 2137 2125 Sokrates C4 226 having avg(credits) >= 3;

5001 Foundations 4 2137 2126 Russel C4 232


... ... ... ... ... ... ... ...
5001 Foundations 4 2137 2137 Kant C4 7
5041 Ethics 4 2125 2125 Sokrates C4 226
... ... ... ... ... ... ... ...
... ... ... ... ... ... ... ...
4630 The Three Critics 4 2137 2137 Kant C4 7

selection with where condition


σreadBy= EmployeeNr ∧ Rank = 'C4‘ (Lectures x Professors)
LectNr Title Credits readBy EmployeeNr Name Rank Room
5001 Foundations 4 2137 2137 Kant C4 7
5041 Ethics 4 2125 2125 Sokrates C4 226
5043 Epistemology 3 2126 2126 Russel C4 232
5049 Mäeutics 2 2125 2125 Sokrates C4 226
4052 Logic 4 2125 2125 Sokrates C4 226
5052 Philosophy of Sci. 3 2126 2126 Russel C4 232
5216 Bio-Ethics 2 2126 2126 Russel C4 232
4630 The Three Critics 4 2137 2137 Kant C4 7
203
σreadBy= EmployeeNr ∧ Rank = 'C4‘ (Lectures x Professors) select readBy, Name, sum(Credits)
from Lectures, Professors
LectNr Title Credits readBy EmployeeNr Name Rank Room where readBy = EmployeeNr and Rank = 'C4'
group by readBy, Name
5001 Foundations 4 2137 2137 Kant C4 7 having avg(credits) >= 3;
5041 Ethics 4 2125 2125 Sokrates C4 226
5043 Epistemology 3 2126 2126 Russel C4 232
5049 Mäeutics 2 2125 2125 Sokrates C4 226
4052 Logic 4 2125 2125 Sokrates C4 226
5052 Philosophy of Sci. 3 2126 2126 Russel C4 232
5216 Bio-Ethics 2 2126 2126 Russel C4 232
4630 The Three Critics 4 2137 2137 Kant C4 7

grouping with groupBy


greadBy, Name (σreadBy= EmployeeNr ∧ Rank = 'C4‘ (Lectures x Professors))
LectNr Title Credits readBy EmployeeNr Name Rank Room
5041 Ethics 4 2125 2125 Sokrates C4 226
5049 Mäeutics 2 2125 2125 Sokrates C4 226
4052 Logic 4 2125 2125 Sokrates C4 226
5043 Epistemology 3 2126 2126 Russel C4 232
5052 Philosophy of Sci. 3 2126 2126 Russel C4 232
5216 Bio-Ethics 2 2126 2126 Russel C4 232
4630 The Three Critics 4 2137 2137 Kant C4 7
5001 Foundations 4 2137 2137 Kant C4 7
204
greadBy, Name (σreadBy= EmployeeNr ∧ Rank = 'C4‘ (Lectures x Professors)) select readBy, Name, sum(Credits)
from Lectures, Professors
LectNr Title Credits readBy EmployeeNr Name Rank Room where readBy = EmployeeNr and Rank = 'C4'
group by readBy, Name
5041 Ethics 4 2125 2125 Sokrates C4 226 having avg(credits) >= 3;
5049 Mäeutics 2 2125 2125 Sokrates C4 226
4052 Logic 4 2125 2125 Sokrates C4 226
5043 Epistemology 3 2126 2126 Russel C4 232
5052 Philosophy of Sci. 3 2126 2126 Russel C4 232
5216 Bio-Ethics 2 2126 2126 Russel C4 232
4630 The Three Critics 4 2137 2137 Kant C4 7
5001 Foundations 4 2137 2137 Kant C4 7

computing the aggregate function avg


greadBy, Name; avg(Credits) (σreadBy= EmployeeNr ∧ Rank = 'C4‘ (Lectures x Professors))
LectNr Title Credits readBy EmployeeNr Name Rank Room avg(credits)
5041 Ethics 4 2125 2125 Sokrates C4 226
5049 Mäeutics 2 2125 2125 Sokrates C4 226 3.333
4052 Logic 4 2125 2125 Sokrates C4 226
5043 Epistemology 3 2126 2126 Russel C4 232
5052 Philosophy of Sci. 3 2126 2126 Russel C4 232 2.667
5216 Bio-Ethics 2 2126 2126 Russel C4 232
4630 The Three Critics 4 2137 2137 Kant C4 7
4
5001 Foundations 4 2137 2137 Kant C4 7
205
greadBy, Name; avg(Credits) (σreadBy= EmployeeNr ∧ Rank = 'C4‘ (Lectures x Professors)) select readBy, Name, sum(Credits
from Lectures, Professors
LectNr Title Credits readBy EmployeeNr Name Rank Room avg(Credits) where readBy = EmployeeNr an
group by readBy, Name
5041 Ethics 4 2125 2125 Sokrates C4 226 having avg(credits) >= 3;

5049 Mäeutics 2 2125 2125 Sokrates C4 226 3.333


4052 Logic 4 2125 2125 Sokrates C4 226
5043 Epistemology 3 2126 2126 Russel C4 232
5052 Philosophy of Sci. 3 2126 2126 Russel C4 232 2.667
5216 Bio-Ethics 2 2126 2126 Russel C4 232
4630 The Three Critics 4 2137 2137 Kant C4 7
4
5001 Foundations 4 2137 2137 Kant C4 7

selecting groups with having


σavg(Credits)>3 (greadBy, Name; avg(Credits) (σreadBy= EmployeeNr ∧ Rank = 'C4‘ (Lectures x Professors)))
LectNr Title Credits readBy EmployeeNr Name Rank Room avg(Credits)
5041 Ethics 4 2125 2125 Sokrates C4 226
5049 Mäeutics 2 2125 2125 Sokrates C4 226 3.333
4052 Logic 4 2125 2125 Sokrates C4 226
4630 The Three Critics 4 2137 2137 Kant C4 7
4
5001 Foundations 4 2137 2137 Kant C4 7

206
σavg(Credits)>3 (greadBy, Name; avg(Credits) (σreadBy= EmployeeNr ∧ Rank = 'C4‘ (Lectures x Professors))) select readBy, Name, sum(Credits
from Lectures, Professors
LectNr Title Credits readBy EmployeeNr Name Rank Room avg(Credits) where readBy = EmployeeNr an
group by readBy, Name
5041 Ethics 4 2125 2125 Sokrates C4 226 having avg(credits) >= 3;

5049 Mäeutics 2 2125 2125 Sokrates C4 226 3.333


4052 Logic 4 2125 2125 Sokrates C4 226
4630 The Three Critics 4 2137 2137 Kant C4 7
4
5001 Foundations 4 2137 2137 Kant C4 7

computing aggregate function sum

σavg(Credits)>3 (greadBy, Name; avg(Credits), sum(Credits) (σreadBy= EmployeeNr ∧ Rank = 'C4‘ (Lectures x Professors)))
LectNr Title Credits readBy EmployeeNr Name Rank Room avg(Credits) sum(Credits)
5041 Ethics 4 2125 2125 Sokrates C4 226
5049 Mäeutics 2 2125 2125 Sokrates C4 226 3.333 10
4052 Logic 4 2125 2125 Sokrates C4 226
4630 The Three Critics 4 2137 2137 Kant C4 7
4 8
5001 Foundations 4 2137 2137 Kant C4 7

projection

ΠreadBy,Name,sum(Credits) (σavg(Credits)>3 (greadBy, Name; avg(Credits), sum(Credits) (σreadBy= EmployeeNr ∧ Rank = 'C4‘ (Lectures x Professors))))
readBy Name sum(Credits)
2125 Sokrates 10
2137 Kant 8 207
Aggregate Functions and Grouping

● actual processing is not that schematic

● application of aggregate functions: SQL creates one result


tuple per group
! in the select clause (except for the aggregated) only those
attributes may be listed which are also listed in the group by
clause

● example: how many lectures does each student visit?


select count(e.LectNr), e.MatrNr, s.name
from enrolledIn e, Students s
where e.MatrNr = s.MatrNr
group by e.MatrNr, s.Name

208
Exercise A
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
write a select statement to
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 find names of professors
5043 Epistemology 3 2126 26120 Fichte 10 which give more than 3
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6 lectures!
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
209
Exercise A
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
write a select statement to
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 find names of professors
5043 Epistemology 3 2126 26120 Fichte 10 which give more than 3
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6 lectures!
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004
3005
Wittgenstein Linguistics
Rhetikus Celestial Mechanics
2126
2127
28106 select p.Name
5052
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
from Lectures v, Professors p
3007 Spinoza God and Nature 2126
29120 5001 where v.readBy = p.EmployeeNr
examine 29120 5041
29120 5049 group by v.readBy, p.Name
MatrNr LectNr EmployeeNr Mark
28106 5001 2126 1
29555
25403
5022
5022
having count(*) > 3;
25403 5041 2125 2
27550 4630 2137 2
210
Nested Queries

1. sub-query in where clause

example: the results of which exams are


better than average?

select *
from examine ex
where ex.Mark < ( select avg (Mark)
from examine );

211
Geschachtelte Anfragen

2. sub-query in select clause

● for each result tuple the sub-query is executed

● example: determine the teaching load for each professor

select p.EmployeeNr, p.Name, ( select sum (le.Credits) as teachload


from Lectures le
where le.readBy = p.EmployeeNr )
from Professors p;

This sub-query is correlated (uses attributes of the


enclosing query)

212
Exercise

Implement the previous query with a group by!

determine the teaching load for each professor

select p.EmployeeNr, p.Name, ( select sum (le.Credits) as teachload


from Lectures le
where le.readBy = p.EmployeeNr )
from Professors p;

213
Exercise

Implement the previous query with a group by!

determine the teaching load for each professor

select p.EmployeeNr, p.Name, ( select sum (le.Credits) as teachload


from Lectures le
where le.readBy = p.EmployeeNr )
from Professors p;

select readBy, Name, sum(Credits) as teachload


from Lectures, Professors
where readBy = EmployeeNr
group by readBy, Name;

214
Correlated vs Uncorrelated Sub-Queries

example: which students are older than professors?

● correlated version:
select s.*
from Students s
where exists ( select p.*
from Professors p
where p.BirthYear > s.BirthYear );

● uncorrelated version:
select s.*
from Students s
where s.BirthYear < (select max (p.BirthYear)
from Professors p);
advantage: sub-query has to evaluated only once.
215
Decorrelation of Correlated Sub-Queries via Join

e.g.: which assistant has a boss who is younger than he/her?


select a.*
from Assistants a
where exists
( select p.*
from Professors p
where a.Boss = p.EmployeeNr and p.BirthYear>a.BirthYear )

--> decorrelation via Join:


select a.*
from Assistants a, Professors p
where a.Boss = p.EmployeeNr and p.BirthYear>a.BirthYear;

216
Nested Sub-Queries

3. sub-query in from clause:

--> using a result table of a sub-query as a table:


example: who is enrolled in more than two lectures?

select tmp.MatrNr, tmp.Name, tmp.noOfLectures


from ( select s.MatrNr, s.Name, count(*) as noOfLectures
from Students s, enrolledIn e
where s.MatrNr=e.MatrNr
group by s.MatrNr, s.Name ) tmp
where tmp.noOfLectures > 2;

217
All-Quantification via count-Aggregation

● all-quantification may always be expressed via count aggregation.


kann
● e.g.: MatrNr of students that are enrolled in all lectures:

select e.MatrNr
from enrolledIn e
group by e.MatrNr
having count (*) = (select count (*) from Lectures);

( count(*) in having counts all tuples of a group )

218
All-Quantification via count-Aggregation
E.g.: MatrNr of students that are enrolled in all 4 credit lectures:

select s.MatrNr
from Students s
where not exists
( select le.*
from Lectures le
where le.Credits=4 and not exists
( select enrolledIn.*
from enrolledIn e
where e.LectNr = le.LectNr and e.MatrNr = s.MatrNr ) );

analogously to the logical identity ∀" #$%& " ≡ ¬∃" ¬#$%&(")


(where x is a variable and Pred(x) an expression of first oder logic)

219
All-Quantification via count-Aggregation
E.g.: MatrNr of students that are enrolled in all 4 credit lectures:

select s.MatrNr
from Students s
where not exists
( select le.*
from Lectures le
where le.Credits=4 and not exists
( select enrolledIn.*
from enrolledIn e
where e.LectNr = le.LectNr and e.MatrNr = s.MatrNr ) );
or:
select e.MatrNr
from enrolledIn e, Lectures le
where e.LectNr = le.LectNr and le.Credits = 4
group by e.MatrNr
having count (*) = ( select count (*)
from lectures le
where le.Credits = 4 ); 220
Exercises

Practise for yourself:


either with the web-interface
or with XAMPP

221
SQL Web-Interface http://hyper-db.com/interface.html

222
XAMPP https://www.apachefriends.org/index.html

• download for your OS,


• install,
• execute “uni-database.sql”,
• work on that database in your browser

223
Lectures Students
VorlNr Title Credits readBy MatrNr Name Semester
Find all professors that in
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 total read more than 10
5043 Epistemology 3 2126 26120 Fichte 10 credits of lectures
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
Lectures Students
VorlNr Title Credits readBy MatrNr Name Semester
Find all professors that in
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 total read more than 10
5043 Epistemology 3 2126 26120 Fichte 10 credits of lectures
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
3002 Platon Ideology 2125
27550 5001 select Name
27550 4052
3003 Aristoteles Syllogistics 2125
3004 Wittgenstein Linguistics 2126
28106 5041 from Lectures, Professors
28106 5052
3005 Rhetikus Celestial Mechanics 2127
3006 Newton Keplerian Laws 2127
28106 5216 where readBy = EmployeeNr
28106 5259
3007 Spinoza God and Nature 2126
29120 5001 group by Name
examine 29120 5041

MatrNr LectNr EmployeeNr Mark


29120 5049 having sum(Credits) > 10;
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
Exercise A
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
write a select statement to
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 determine which professors
5043 Epistemology 3 2126 26120 Fichte 10 have which assistants!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3 columns of the result:
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
profName, assiName
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
226
Exercise A
Lectures Students
LectlNr Title Credits readBy MatrNr Name Semester
write a select statement to
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 determine which professors
5043 Epistemology 3 2126 26120 Fichte 10 have which assistants!
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3 columns of the result:
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
profName, assiName
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics select p.Name as profName, a.Name as assiName
2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126 from Assistants a, Professors p
29120 5001
examine 29120 5041
where a.Boss = p.EmployeeNr ;
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
227
Exercise A
Lectures Students We are looking for a select statement that
LectlNr Title Credits readBy MatrNr Name Semester
determines the names of students that are
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 enrolled in at least one lecture with 3
5043 Epistemology 3 2126 26120 Fichte 10 credits AND in at least one with 4 credits
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Logic 4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 Bio-Ethics 2 2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133 29555 Feuerbach 2
5022 Advanced Theology 2 2134
4630 The Three Criteria 4 2137 isPrerequisiteFor
Predecessor Successor
Professors
5001 5041
EmployeeNr Name Rank Room
5001 5043
2125 Sokrates C4 226 5001 5049
2126 Russel C4 232 5041 5216
2127 Kopernikus C3 310 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 enrolledIn
MatrNr LectNr
Assistants
26120 5001
EmployeeNr Name ResearchArea Boss
27550 5001
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics 2125 28106 5041
3004 Wittgenstein Linguistics 2126 28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216
3006 Newton Keplerian Laws 2127
28106 5259
3007 Spinoza God and Nature 2126
29120 5001
examine 29120 5041
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022
28106 5001 2126 1 25403 5022
25403 5041 2125 2
27550 4630 2137 2
228
Exercise A
Lectures Students We are looking for a select statement that
LectlNr Title Credits readBy MatrNr Name Semester
determines the names of students that are
5001 Foundations 4 2137 24002 Xenokrates 18
5041 Ethics 4 2125 25403 Jonas 12 enrolled in at least one lecture with 3
5043 Epistemology 3 2126 26120 Fichte 10 credits AND in at least one with 4 credits
5049 Mäeutics 2 2125 26830 Aristoxenos 8
4052 Variant Logic1 (via Join):
4 2125 27550 Schopenhauer 6
5052 Philosophy of Science 3 2126 28106 Carnap 3
5216 select distinct s.Name
Bio-Ethics 2 as NameStudent
2126 29120 Theophrastos 2
5259 The Wiener Circle 2 2133
from Students s, enrolledIn h,29555 Feuerbach
enrolledIn 2
hh, Lectures v, Lectures vv
5022 Advanced Theology 2 2134
where s.MatrNr =4 h.MatrNr
4630 The Three Criteria 2137
and
isPrerequisiteFor
s.MatrNr = hh.MatrNr Predecessor
and Successor
Professors
h.LectNr = v.LectNr and 5001 5041
EmployeeNr Name
v.creditsRank Room
= 3 and 5001 5043
2125 Sokrates C4 226
2126
hh.LectNrC4= vv.LectNr
Russel 232
and
5001 5049
5041 5216
2127 vv.creditsC3= 4 310
Kopernikus 5043 5052
2133 Popper C3 52 5041 5052
2134 Augustinus C3 309 5052 5259
2136 Curie C4 36
2137 Kant C4 7 Variant 2 (viaenrolledIn
sub-statements):
MatrNr LectNr
Assistants
select distinct s.Name
26120 5001 as NameStudent
EmployeeNr Name ResearchArea Boss
from Students s, enrolledIn
27550 5001 h, enrolledIn hh
3002 Platon Ideology 2125 27550 4052
3003 Aristoteles Syllogistics where s.MatrNr = h.MatrNr and s.MatrNr = hh.MatrNr and
2125 28106 5041
3004 Wittgenstein Linguistics h.LectNr
2126 in (select v.LectNr
28106 5052
3005 Rhetikus Celestial Mechanics 2127
28106 5216 from Lectures v
3006 Newton Keplerian Laws 2127
3007 Spinoza God and Nature 2126
28106 5259 where v.credits = 3
29120 5001
) and
examine 29120 5041
hh.LectNr in (select v.LectNr
29120 5049
MatrNr LectNr EmployeeNr Mark
29555 5022 from Lectures v
28106 5001 2126 1 25403 5022 where v.credits = 4
25403 5041 2125 2
27550 4630 2137 2 )
229
-----------------------------------------------------------------------------

230
Bibliography

(1) A. Kemper , A. Eickler: Datenbanksysteme – Eine Einführung. Oldenbourg Verlag


(2) A. Kemper, M. Wimmer: Übungsbuch Datenbanksysteme. Oldenbourg Verlag
(3) Avi Silberschatz, Henry F. Korth, S. Sudarshan: Database System Concepts
McGraw Hill, 2010
(4) Jan Van den Bussche, Stijn Vansummeren: Translating SQL into the Relational
Algebra. http://cs.ulb.ac.be/public/_media/teaching/infoh417/sql2alg_eng.pdf
(URL, Oktober 2014)

231
Recommendation for Studying

● minimal approach:
understand the slides and practices ER-modelling, translation into the
relational model and SQL select statements with a selection of the
assignments from the assignment sheets!

● standard approach:
minimal approach + read the corresponding parts of [1]: chapter 1, chapter
2: 2.1-2.12, chapter 3: 3.1-3.4, chapter 4: 4.1- 4.12!
If you need to use a corresponding English book, read the correspodning
parts of [3].
Do all the assignments from the assignment sheets!

● interested students:
standard approach + read in [1]: 2.13, 4.13-4.21, Kapitel 6 + do more
assignments from [2]: chapters 2, 3, 4 and 6!
232

You might also like