foundations of information systems (ws 2004/05)foundations ... · • the (mathematical) concept of...

73
© © 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer Manthey Manthey Foundations Foundations of IS of IS 1 1 Mathematical Foundations Mathematical Foundations Mathematical Foundations Mathematical Foundations Mathematical Foundations Chapter 2 Chapter 2 Foundations of Information Systems (WS 2004/05) Foundations of Information Systems (WS 2004/05) Foundations of Information Systems (WS 2004/05) 2200 2200 2200 2200 2200 2200

Upload: others

Post on 08-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 11

Mathematical FoundationsMathematical Foundations

Mathematical FoundationsMathematical FoundationsMathematical Foundations

–– Chapter 2 Chapter 2 ––

Foundations of Information Systems (WS 2004/05)Foundations of Information Systems (WS 2004/05)Foundations of Information Systems (WS 2004/05)

∀∀∀∀∀∀∀∀∈∈∈∈∈∈∈∈

Page 2: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 22

OverviewOverview

•• Query languages for databases, such as SQL Query languages for databases, such as SQL –– to be introduced in the nextto be introduced in the nextchapter chapter –– are are formal languagesformal languages, relying on a rigorously defined syntax and, relying on a rigorously defined syntax andsemantics.semantics.

•• Most concepts in relational query languages are based on mathMost concepts in relational query languages are based on mathematicalematicalconstructs, particularly on formalisms in constructs, particularly on formalisms in set theoryset theoryand and predicate logicpredicate logic..

•• Therefore, it is inevitable to acquire at least some basic Therefore, it is inevitable to acquire at least some basic „„ know howknow how““ about about these two areas of mathematics. This chapter provides you wthese two areas of mathematics. This chapter provides you with a brief ith a brief summary of the most important aspects.summary of the most important aspects.

•• Furthermore, we will introduce two variants of logics and setFurthermore, we will introduce two variants of logics and settheory, theory, respresp.,.,tailored particularly for the manipulation of relations, betailored particularly for the manipulation of relations, being special sets anding special sets andthus requiring special operators and special syntax:thus requiring special operators and special syntax:

•• Relational algebraRelational algebrais the basis of relational query processing.is the basis of relational query processing.•• Relational calculusRelational calculusis the logical counterpart.is the logical counterpart.•• SQL is based on both, relational algebra as well as relationaSQL is based on both, relational algebra as well as relational calculus.l calculus.

Page 3: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 33

Set theorySet theory

Set TheorySet TheorySet Theory

∀∀∀∀∀∀∀∀∈∈∈∈∈∈∈∈∈∈∈∈

∅∅∅∅∅∅∅∅∅∅∅∅

∃∃∃∃∃∃∃∃¬¬¬¬¬¬¬¬−− 2.1 2.1 −−

Page 4: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 44

Concept of a setConcept of a set

"A set is a collection into a whole of definite, distinct objects

of our perception or our thought."

"A"A setsetis a collection into a whole of definite, distinct objects is a collection into a whole of definite, distinct objects

of our perception or our thought."of our perception or our thought."

GeorgGeorgCantor (1845Cantor (1845--1918), originator of set theory1918), originator of set theory

•• The (mathematical) concept of a The (mathematical) concept of a setset is of fundamental importance for is of fundamental importance for almost every area of computer science.almost every area of computer science.

•• goal of this sectiongoal of this section: summary of the most important basic concept: summary of the most important basic conceptof set theory (supposed to be known from school)of set theory (supposed to be known from school)

•• The notion of a set is "defined" in an The notion of a set is "defined" in an informalinformal way, as is common practiceway, as is common practicein mathematics (by intuition, in mathematics (by intuition, ““ naive set theory").naive set theory").

Page 5: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 55

Examples of setsExamples of sets

99417616417616

2121

256256382382

Sets are rather collections of Sets are rather collections of abstract ideas or individuals abstract ideas or individuals ("objects") than shopping ("objects") than shopping bags.bags.

Page 6: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 66

Representation of sets, empty setRepresentation of sets, empty set

•• There are two ways to denote sets:There are two ways to denote sets:

explicitly: by enumeration of all the members of the set{ e1, e2, . . . }

implicitly: by means of a defining expression A{ x | A(x) }

explicitlyexplicitly: by : by enumerationenumerationof all the members of the setof all the members of the set{ e1, e2, . . . }{ e1, e2, . . . }

implicitlyimplicitly : by means of a : by means of a defining expressiondefining expressionAA{ x | A(x) }{ x | A(x) }

e.g: { 2, 4, 6, 8 } e.g: { 2, 4, 6, 8 } { x | x is an even natural number and x < 10 }{ x | x is an even natural number and x < 10 }

•• A set containing no members is called A set containing no members is called empty setempty setand is denoted by and is denoted by { }{ }(or by (or by ∅∅∅∅∅∅∅∅).).

2244

6688

denotationsof the same set

denotationsdenotationsof the same setof the same set

Page 7: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 77

Properties of set denotationsProperties of set denotations

•• If a set is denoted explicitly, the If a set is denoted explicitly, the orderorderof the denotation is of the denotation is immaterialimmaterial("Members of a set do not have an order."). ("Members of a set do not have an order.").

•• The The number of occurrencesnumber of occurrencesof a member in a set denotation is of a member in a set denotation is irrelevantirrelevant, too , too ("Sets do contain every member only once.").("Sets do contain every member only once.").

{ 2, 4, 6, 8 }{ 2, 4, 6, 8 }{ 2, 4, 6, 8 }

{ 6, 4, 8, 2 }{ 6, 4, 8, 2 }{ 6, 4, 8, 2 }

two denotationsof the same set

two denotationstwo denotationsof the same setof the same set

another denotationof this set

another denotationanother denotationof this setof this set { 2, 2, 4, 8, 8, 8, 6 }{ 2, 2, 4, 8, 8, 8, 6 }{ 2, 2, 4, 8, 8, 8, 6 }

Page 8: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 88

•• The empty set has cardinality 0.The empty set has cardinality 0.

•• CautionCaution!! Repeated members of a set are counted only once, i.e. Repeated members of a set are counted only once, i.e.

Elements and cardinality of a setElements and cardinality of a set

•• According to Cantor's "definition" a set consists of "objectAccording to Cantor's "definition" a set consists of "objects".s".Every object in a set is called an Every object in a set is called an elementelementor or membermemberof this set.of this set.

•• symbolicallysymbolically::

e ∈ Bee ∈∈ BB object e is a member (element) of set Bobject e is a member (element) of set Be belongs to B, e is in B, B owns ee belongs to B, e is in B, B owns e

•• The number of the members of a set The number of the members of a set MM is called is called cardinalitycardinalityof M,of M,symbolically written:symbolically written:

| M ||| MM ||

| { 1, 2, 1, 4, 2, 7} | = 4| {| { 11, , 22,, 1,1, 44,, 2,2, 77 } | =} | = 44

Page 9: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 99

Comparison of setsComparison of sets

•• A set A is called a A set A is called a subsetsubsetof a set B, if every member of Aof a set B, if every member of Ais a member of B, too.is a member of B, too.

A ⊆ BA A ⊆⊆ BB

•• Two sets A und B are called Two sets A und B are called equalequal, if A is a subset of B, , if A is a subset of B, and if and if –– in addition in addition –– B is a subset of A. B is a subset of A.

A = BA A == BB

•• A set A is called a A set A is called a proper subsetproper subsetof B, if A is a subset of B, of B, if A is a subset of B, and if A is and if A is notnot equal to B.equal to B.

A ⊂ BA A ⊂⊂ BB

{ 1, 3} ⊆ { 1, 2, 3}{{ 1, 31, 3} } ⊆⊆ {{ 1, 2, 31, 2, 3}}

{ 1, 2, 3} = { 1, 2, 3}{{ 1, 2, 31, 2, 3} } == {{ 1, 2, 31, 2, 3}}

{ 1, 2, 3} ⊆ { 1, 2, 3}{{ 1, 2, 31, 2, 3} } ⊆⊆ {{ 1, 2, 31, 2, 3}}

{ 1, 3} ⊂ { 1, 2, 3}{{ 1, 31, 3} } ⊂⊂ {{ 1, 2, 31, 2, 3}}

Page 10: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 1010

Negating set comparisonsNegating set comparisons

Crossing outCrossing outa set comparison operator or the element operatora set comparison operator or the element operatormeans to means to negate negate the respective relationship:the respective relationship:

A ⊆ BA A ⊆⊆ BB

A = BA A == BB

A ⊂ BA A ⊂⊂ BB

e ∉ Be e ∉∉ BB

A is A is notnot a subset of Ba subset of B

A is A is nono proper subset of Bproper subset of B

A and B are A and B are notnot equalequal

e is e is nono member of Bmember of B

Page 11: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 1111

union A ∪∪∪∪ B = { e | e ∈ A or e ∈ B }

intersection A ∩∩∩∩ B = { e | e ∈ A and e ∈ B }

difference A \ B = { e | e ∈ A and e ∉ B }

unionunion A A ∪∪∪∪∪∪∪∪ B B == { { ee || e e ∈∈ A A oror e e ∈∈ B B }}

intersectionintersection A A ∩∩∩∩∩∩∩∩ B B == { { ee || e e ∈∈ A A andand e e ∈∈ B B }}

differencedifference A A \\ B B == { { ee || e e ∈∈ A A andand e e ∉∉ B B }}

Elementary set operatorsElementary set operators

•• In set theory, there are three basic In set theory, there are three basic operationsoperationsby which two sets can beby which two sets can becombined:combined:

defdef

defdef

defdef

•• Examples:Examples:{1, 2} ∪ {2, 3} = {1, 2, 3}

{1, 2} ∩ {2, 3} = {2}

{1, 2} \ {2, 3} = {1}

{1, 2} {1, 2} ∪∪ {2, 3} = {1, 2, 3}{2, 3} = {1, 2, 3}

{1, 2} {1, 2} ∩∩ {2, 3} = {2}{2, 3} = {2}

{1, 2} {1, 2} \\ {2, 3} = {1}{2, 3} = {1}

Page 12: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 1212

Examples of using set operatorsExamples of using set operators

AA

BB

A A \\ BB

A A ∩∩∩∩∩∩∩∩ BB

B B \\ AA

A A ∪∪∪∪∪∪∪∪ BB

Page 13: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 1313

Cartesian product and Cartesian product and tuplestuples

•• more binary basic operations of set theory:more binary basic operations of set theory:

A ×××× B = { ( a, b )| a ∈ A und b ∈ B }A A ×××××××× B B == { { ( a, b )( a, b )|| a a ∈∈ A und b A und b ∈∈ B B }}

(Cartesian) (Cartesian) productproduct

•• The members of a product of n sets are called (n)The members of a product of n sets are called (n)--tuplestuples..

•• generalized construction of products for n sets (n generalized construction of products for n sets (n ≥≥ 2) 2) ::

A1 ×××× . . . ×××× An = { ( a1, . . . , an ) | ai ∈ A i }AA11 ×××××××× . . . . . . ×××××××× AAnn == { { ( a( a11, . . . , a, . . . , ann )) || aaii ∈∈ AA ii }}

•• special denominations for special denominations for tuplestuples::•• n = 1: singletonn = 1: singleton•• n = 2: pairn = 2: pair•• n = 3: triplen = 3: triple

•• n = 4: quadruplen = 4: quadruple•• n = 5: quintuplen = 5: quintuple

Page 14: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 1414

Example of a products of two setsExample of a products of two sets

AA

BB

1155

77

A A ×××××××× BB( , 1 )( , 1 )

( , 1 )( , 1 )

( , 1 )( , 1 )

( , 1 )( , 1 )

( , 5 )( , 5 )

( , 5 )( , 5 )

( , 5 ) ( , 5 )

( , 5 )( , 5 )

( , 7 )( , 7 )

( , 7 )( , 7 )

( , 7 )( , 7 )

( , 7 )( , 7 )

Page 15: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 1515

RelationsRelations

Every subset R of a product A1 ×××× . . . ×××× An

is called a relation over A1, . . . , An

Every subset R of a product AEvery subset R of a product A11 ×××××××× . . . . . . ×××××××× AAnn

is called a is called a relationrelationover Aover A11, . . . , A, . . . , Ann

•• Relations are usually denoted in the form of a Relations are usually denoted in the form of a tabletable..

AA11 = { a, b, c }= { a, b, c }AA22 = { 1, 2 }= { 1, 2 }AA33 = { %, $ }= { %, $ }

a 1 %a 1 $b 2 $

a 1 %a 1 %a 1 $a 1 $b 2 $b 2 $

RR

•• In computer science, In computer science, sets of sets of tuplestuplesare very important for modeling are very important for modeling relationships between objects.relationships between objects.

•• Sets of Sets of tuplestuplesare called are called relationsrelations..

Page 16: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 1616

FunctionsFunctions

•• A A binarybinaryrelation R relation R ⊆⊆ A A ×× B is calledB is called•• rightright--unique, unique, if for every a if for every a ∈∈ AA

there is at most one b there is at most one b ∈∈ B with (a, b) B with (a, b) ∈∈ RR•• leftleft--unique, unique, if for every b if for every b ∈∈ BB

there is at most one a there is at most one a ∈∈ A with (a, b) A with (a, b) ∈∈ RR

•• RightRight--unique relations are also called unique relations are also called functionsfunctions(or mappings).(or mappings).They serve as mathematical models of They serve as mathematical models of unique assignmentsunique assignments..

•• A rightA right--unique relation f of unique relation f of A A ×× B is called a B is called a function from A to Bfunction from A to B..

•• NotationNotation:: f : A f : A →→ B instead of f B instead of f ⊆⊆ A A ×× B andB andf(a) = b instead of (a, b) f(a) = b instead of (a, b) ∈∈ ff

•• A is called A is called domain (of definition)domain (of definition)of f, B is called of f, B is called coco--domain domain of f.of f.

Page 17: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 1717

Arrow diagramsArrow diagrams

AA

BB

1155

77

fff

f( ) = 5 f( ) = 1 f( ) = 1f( ) = 5 f( ) = 1 f( ) = 1f( ) = 5 f( ) = 1 f( ) = 1

Functions are rather Functions are rather visualisedvisualisedby means of by means of arrowarrowdiagramsdiagramsthan tables.than tables.

Page 18: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 1818

LogicLogic

LogicLogicLogic

∀∀∀∀∀∀∀∀∀∀∀∀∈∈∈∈∈∈∈∈

∅∅∅∅∅∅∅∅

∃∃∃∃∃∃∃∃∃∃∃∃¬¬¬¬¬¬¬¬¬¬¬¬−− 2.2 2.2 −−

Page 19: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 1919

PropositionalPropositionallogiclogic

Theory of propositions and their connecting operators: Theory of propositions and their connecting operators: propositionalpropositionallogiclogic

A proposition (statement) is a sentence (a linguistic entity)of which it is reasonable to say that it is true or false.

A A proposition (statement)proposition (statement)is a sentence (a linguistic entity)is a sentence (a linguistic entity)of which it is reasonable to say that it is true or false. of which it is reasonable to say that it is true or false.

(Aristotle, Greek mathematician, 384(Aristotle, Greek mathematician, 384--322 B.C.)322 B.C.)

Examples of elementary statements:Examples of elementary statements:

• 5 < 6 true• 8 is prime false• The moon is made of cheese. false

•• 5 5 << 6 6 truetrue•• 8 is prime8 is prime falsefalse•• The moon is made of cheese.The moon is made of cheese.falsefalse

Page 20: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 2020

OperatorsOperators

•• Compound statements are composed from other statements by meCompound statements are composed from other statements by means ans of logical of logical operatorsoperators(connectors).(connectors).

•• BinaryBinary (dyadic) operators of (dyadic) operators of propositionalpropositionallogic:logic:

Conjunction ∧ "and" (also: & )

Disjunction ∨ "or" (also: | )

Implication ⇒ "if . . then . . "

Equivalence ⇔ "if and only if . . then . . " (also: iff)

ConjunctionConjunction ∧∧ "and" "and" (also: & ) (also: & )

DisjunctionDisjunction ∨∨ "or""or" (also: (also: | | ))

ImplicationImplication ⇒⇒ "if . . then . . ""if . . then . . "

EquivalenceEquivalence ⇔⇔ "if and only if . . then . . ""if and only if . . then . . " (also: (also: iffiff ))

•• UnaryUnary(monadic) operator: (monadic) operator: Negation ¬ "not"NegationNegation ¬¬ "not""not"

Page 21: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 2121

Compound statementsCompound statements

( ( 5 < 6 ) ∧∧∧∧ ( 6 < 7 ) ) ⇒⇒⇒⇒ ( 5 < 7 )( ( 5 < 6 ) ( ( 5 < 6 ) ∧∧∧∧∧∧∧∧ ( 6 < 7 ) ) ( 6 < 7 ) ) ⇒⇒⇒⇒⇒⇒⇒⇒ ( 5 < 7 )( 5 < 7 )

( ( 5 < 6 ) ∧∧∧∧ ( a ≠≠≠≠ b ) ) ⇒⇒⇒⇒ ( 5 < 7 )( ( 5 < 6 ) ( ( 5 < 6 ) ∧∧∧∧∧∧∧∧ ( ( aa ≠≠≠≠≠≠≠≠ bb ) ) ) ) ⇒⇒⇒⇒⇒⇒⇒⇒ ( 5 < 7 )( 5 < 7 )

•• ConclusionConclusion: Parts of (true) compound statements do : Parts of (true) compound statements do notnot need need to be related to each other in any wato be related to each other in any way.y.

•• Truth values of compound statements can be derived systematicTruth values of compound statements can be derived systematicallyallyfrom the truth values of their parts, e.g.from the truth values of their parts, e.g.

truetrue truetrue

truetruetruetrue

truetrue

•• By an analogous construction it is possible to assign the trBy an analogous construction it is possible to assign the truth value 'true'uth value 'true'to to thisthis statement:statement:

!!

Page 22: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 2222

Meaning of operators (1)Meaning of operators (1)

•• Operators are syntactical "tools", by which the meaning ("seOperators are syntactical "tools", by which the meaning ("semantics") mantics") of compound statements can be derived from the meaning of of compound statements can be derived from the meaning of their parts.their parts.

•• How this is to be done is determined by soHow this is to be done is determined by so--called called truth tablestruth tables::

A BA BT TT TT FT FF TF TF FF F

A A ∧∧ BBTTFFFFF F

T: trueT: trueF: falseF: false

e.g.e.g. truth tabletruth tablefor for conjunctionconjunction

•• to be read this wayto be read this way: If : If AA is true and is true and BB is false,is false,then the statement then the statement A A ∧∧ BB has the truth value false.has the truth value false.

Page 23: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 2323

Meaning of operators (2)Meaning of operators (2)

•• Truth tables of the remaining binary operators:Truth tables of the remaining binary operators:

A BA BT TT TT FT FF TF TF FF F

AA ∨∨ BBTTTTTTF F

A BA BT TT TT FT FF TF TF FF F

AA ⇒⇒ BBTTFFTTT T

A BA BT TT TT FT FF TF TF FF F

AA ⇔⇔ BBTTFFFFT T

•• Truth table of negation:Truth table of negation:

AATTFF

¬¬ AAFFTT

A bit unusual at first !A bit unusual at first !A bit unusual at first !

Page 24: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 2424

True and falseTrue and false

•• Moreover, two "primitive" statements belong to Moreover, two "primitive" statements belong to propositionalpropositionallogiclogicwhich are always true, or always false: which are always true, or always false:

truetruefalsefalse

TTFF

•• Laws of Laws of propositionalpropositionallogic making use of 'true' and 'false':logic making use of 'true' and 'false':

A ∧ ¬A ≡ falseA ∨ ¬A ≡ true

A A ∧∧ ¬¬A A ≡≡ falsefalseA A ∨∨ ¬¬A A ≡≡ truetrue

A ∧ false ≡ falseA ∧ true ≡ AA ∨ true ≡ trueA ∨ false ≡ A

A A ∧∧ false false ≡≡ falsefalseA A ∧∧ true true ≡≡ AAA A ∨∨ true true ≡≡ truetrueA A ∨∨ false false ≡≡ AA

Page 25: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 2525

ParametrizedParametrizedstatements in predicate logicstatements in predicate logic

•• Fundamental idea of predicate logicFundamental idea of predicate logic::Introduction of Introduction of parametrizedparametrizedpropositions propositions in which "variable" in which "variable" subexpressions subexpressions can be replaced by objects of the can be replaced by objects of the domain of discoursedomain of discoursewhich is to be modeled.which is to be modeled.

"N is prime" domain: natural numbers

"X is smaller then Y" domain: real numbers

"N is prime""N is prime" domain: natural numbersdomain: natural numbers

"X is smaller then Y""X is smaller then Y" domain: real numbersdomain: real numbers

variablesvariablesvariables

•• Only after Only after replacing replacing the variables by "concrete" elements from the domain, the variables by "concrete" elements from the domain, a truth value can be determined:a truth value can be determined:

"N is prime""N is prime""N is prime"

N N ←← 44

N N ←← 55

falsefalse

truetrue??

Page 26: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 2626

Formulas as Formulas as parametrizedparametrizedstatementsstatements

•• In predicate logic, (In predicate logic, (parametrizedparametrized) statements are called .) statements are called .

•• There are There are atomic formulasatomic formulaswhich cannot be further decomposed into subwhich cannot be further decomposed into sub--formulas, and formulas, and compound formulascompound formulaswhich are composed of which are composed of subformulassubformulas..

•• Formal notation for Formal notation for atomicatomicformulas:formulas:

is_prime ( N )is_prime ( N )is_prime ( N )

predicatepredicate parameter listparameter list

•• Binary predicates may also be used in Binary predicates may also be used in infix notationinfix notation::

X is_smaller_then YX is_smaller_then YX is_smaller_then Y respresp.. X < YX X << YY

predicatepredicate

parameterparameter

formulasformulasformulas

Page 27: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 2727

Compound formulasCompound formulas

•• Formulas can be combined into new formulas by the Formulas can be combined into new formulas by the operatorsoperatorsof of propositionalpropositionalcalculus:calculus:

is_prime(N) ∧ N < 10is_prime(N)is_prime(N) ∧∧ N N << 1010

N N ←← 1111

N N ←← 77

falsefalse

truetrue

•• In addition, predicate calculus provides soIn addition, predicate calculus provides so--calledcalledfor the construction of statements which hold for the construction of statements which hold for somefor someor or for allfor all objectsobjectsof the domain of discourse, of the domain of discourse, respresp.:.:

∃∃∃∃ N: ( is_prime(N) ∧ N < 10 )∃∃∃∃∃∃∃∃ N: ( N: ( is_prime(N)is_prime(N) ∧∧ N N << 10 )10 )

∀∀∀∀ N: ( is_prime(N)⇒ N < 10 )∀∀∀∀∀∀∀∀ N: ( N: ( is_prime(N)is_prime(N) ⇒⇒ N N << 10 )10 )

truetrue

falsefalse

There There existsexistsa prime smaller than 10.a prime smaller than 10.

AllAll primes are smaller than 10.primes are smaller than 10.

existential quantifierexistential quantifier(there (there EExists ...)xists ...)

universal quantifieruniversal quantifier(for (for AA ll)ll)

quantifiersquantifiersquantifiers

Page 28: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 2828

Quantification in natural language and logicQuantification in natural language and logic

∀ N: ( is_prime(N)⇒ N < 10 ) ∀∀ N:N: ( ( is_prime(N)is_prime(N) ⇒⇒ N N << 10 ) 10 )

•• Difficulties arise when formulating natural language sentencDifficulties arise when formulating natural language sentences by meanses by meansof quantifiers and variables:of quantifiers and variables:

Variables have to be introduced and quantified first,Variables have to be introduced and quantified first,prior to the actual statement.prior to the actual statement.

•• ExampleExample::Every prime is smaller than 10.Every prime is smaller than 10.Every prime is smaller than 10.

For every N holds: If N is prime, then N is smaller than 10.For every N holdsFor every N holds: If N is prime, then N is smaller than 10.: If N is prime, then N is smaller than 10.!!!

Page 29: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 2929

Predicates, formulas and relationsPredicates, formulas and relations

•• Predicates can be interpreted as Predicates can be interpreted as names of relationsnames of relations. .

•• All combinations of objects which make a formula come true iAll combinations of objects which make a formula come true if they are f they are substituted for the parameters of the formula form a substituted for the parameters of the formula form a tupletupleset (= set (= relationrelation).).

is_proper_divisor_of(X, Y)is_proper_divisor_of(X, Y)is_proper_divisor_of(X, Y)

X YX Y

2222332244332255. . .. . .

446666888899

10101010. . .. . .

Page 30: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 3030

TermsTerms

•• further extension of further extension of propositionalpropositionallogiclogic: expressions which denote certain : expressions which denote certain objectsobjectsof the part of the world under consideration (domain of discourof the part of the world under consideration (domain of discourse): se):

termstermsterms

•• There are There are atomicatomicand and compoundcompoundterms. terms.

•• Atomic terms denote a certain object Atomic terms denote a certain object directlydirectly, , compound terms use compound terms use functionsfunctionswhich have to be applied firstwhich have to be applied firstto identify the denoted object.to identify the denoted object.

"Rainer Manthey""Rainer "Rainer MantheyManthey""

lecturer("Foundations of DB and IS", "WS03/04")lecturerlecturer("Foundations of DB and IS", "WS03/04")("Foundations of DB and IS", "WS03/04")objectobject

termsterms

functionfunction

Page 31: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 3131

Atomic termsAtomic terms

•• There are only two kinds of atomic ("indivisible") terms:There are only two kinds of atomic ("indivisible") terms:

constantsvariables

constantsconstantsvariablesvariables

•• ConstantsConstantsare letters or sequences of letters which always denote exactlyare letters or sequences of letters which always denote exactlythe same object of the domain of discourse under considerthe same object of the domain of discourse under consideration, e.g.:ation, e.g.:

'IX''IX''IX'

"George W. Bush""George W. Bush""George W. Bush"

the number ninethe number nine

the president of the United Statesthe president of the United States

•• VariablesVariablesalso denote exactly one object, but this meaning can change also denote exactly one object, but this meaning can change depending on the object which is currently depending on the object which is currently boundboundto the variable:to the variable:

XXX

Page 32: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 3232

Compound terms and operatorsCompound terms and operators

root ( N )root ( N )root ( N )

operatoroperator parameterparameter

•• Binary function symbols may alternatively be used in Binary function symbols may alternatively be used in infix notationinfix notation::

sum(X, Y)sum(X, Y)sum(X, Y) respresp.. X + YX X ++ YY

operatoroperator

parameterparameter

•• Formal notation for Formal notation for compoundcompoundterms: terms:

•• The The 'operator''operator'denoting the name of a function is also called a denoting the name of a function is also called a function symbolfunction symbol..

Page 33: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 3333

Atomic formulas vs. compound termsAtomic formulas vs. compound terms

•• It's not easy to distinguish atomic formulas and compound tIt's not easy to distinguish atomic formulas and compound termsermsby syntactical means only:by syntactical means only:

<operator>(<list of parameters>)<<operatoroperator>(<list of parameters>)>(<list of parameters>)

termterm

<predicate>(<list of parameters>)<<predicatepredicate>(<list of parameters>)>(<list of parameters>)

formulaformula

•• Whether this syntactical entity is a Whether this syntactical entity is a termtermoror a a formulaformuladepends on the symboldepends on the symbol'magic' being an operator (denoting a function) or a pred'magic' being an operator (denoting a function) or a predicate (denoting aicate (denoting arelation):relation):

magic(1, 22, 3∗3)magic(1, 2magic(1, 222, 3, 3∗∗3)3) ??

•• 'is_professor("Rainer 'is_professor("Rainer MantheyManthey")' is an ")' is an atomicatomicformula, because it doesformula, because it doesnot contain another formula, but only a term as parametenot contain another formula, but only a term as parameter.r.

•• However, 'product(3, 42)' is a However, 'product(3, 42)' is a compoundcompoundterm, because it contains termsterm, because it contains termsas parameters.as parameters.

Page 34: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 3434

Meaning of terms and formulasMeaning of terms and formulas

termtermterm formulaformulaformula

aa bb

bb

aa

fatherfather

functionfunction relationrelation

is_father_ofis_father_of

father(a)father(a)is_father_of(a,b)is_father_of(a,b)truetermterm

formulaformula

objectobjectdenoterdenoter

statementstatementabout objectsabout objects

Page 35: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 3535

Relational AlgebraRelational Algebra

Relational AlgebraRelational AlgebraRelational Algebra

−− 2.3 2.3 −−

ππππππππσσσσσσσσ

∅∅∅∅∅∅∅∅

∪∪∪∪∪∪∪∪××××××××

Page 36: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 3636

Relational Relational algebraalgebra: : OverviewOverview

•• We We will will see that the operators see that the operators of of set theory are set theory are a good a good basis for manipulating basis for manipulating relationsrelations((as they are sets indeedas they are sets indeed), ), but that they have but that they have deficienciesdeficienciesand and thus have thus have to to be be amendedamendedand and extendedextended..

•• Already Already in his in his seminal paper introducing seminal paper introducing relational relational databases Codd introduced databases Codd introduced a a choice choice of of operators particularly tailored for dealing with operators particularly tailored for dealing with relations. relations. This This was was the the basisbasisof of the the formal formal language called the language called the relational relational algebraalgebratodaytoday..

•• Relational Relational algebra is algebra is a a mathematical language mathematical language and and thus not particularly thus not particularly useruser--friendlyfriendly. . ButBut its operators have been incorporated into most its operators have been incorporated into most of of the query languathe query langua--ges ges for for relational relational databases databases in in use todayuse today(e.g., in SQL). (e.g., in SQL). ThusThus, , it is important it is important to to know about know about them.them.

•• MoreoverMoreover, , relationalrelationalalgebra is used internally by algebra is used internally by a DBMS a DBMS for evaluating queries for evaluating queries writtenwritten in SQL (in SQL (or other languagesor other languages). SQL ). SQL queries are compiled into queries are compiled into relational relational algebra expressionsalgebra expressionsand and then transformed into equivalent formulations which then transformed into equivalent formulations which can be evaluated more efficientlycan be evaluated more efficiently((„„ query optimizationquery optimization““ ).).

Page 37: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 3737

AlgebraAlgebra

What is an algebra ?What is What is an an algebra algebra ??

•• An An algebraalgebraisis a system of a system of operatorsoperatorsmanipulating objects manipulating objects in a in a particular particular carrier setcarrier set,,i.e. i.e.

•• all all input parameters are taken from this setinput parameters are taken from this set, and, and•• the result after applying the operators is contained the result after applying the operators is contained in in the carrier as the carrier as well.well.

•• consequenceconsequence: Operators : Operators can be applied can be applied to to results results of of previous operator applicaprevious operator applica--tionstions, i.e., , i.e., nestingnestingof of operators is possibleoperators is possible..

•• e.g.e.g..: .: arithmeticarithmetic((numbersnumbers, + / , + / ∗∗ / / −−), ), propositional logicpropositional logic((truthtruth valuesvalues, , oror / and / / and / notnot ), ), set algebraset algebra(power (power set set of a of a setset, , ∪∪ / / ∩∩ / / −−))

•• The The relational relational algebraalgebra(RA) (RA) isis a a specialspecialvariantvariantof of set algebraset algebra, , the carrier set the carrier set ofofwhich consists which consists in in particular particular of relations of relations rather than arbitrary objectsrather than arbitrary objects. Arguments. Argumentsof RAof RA--operators as operators as well well as their results are as their results are relations. relations.

Page 38: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 3838

Set Set operators for operators for relationsrelations

•• Relations Relations areare((specialspecial) ) setssets, and , and thusthusoperatorsoperatorsof of set algebra are applicable set algebra are applicable totorelations, relations, tootoo::

union R ∪∪∪∪ S difference R−−−− S

intersection R∩∩∩∩ S product R×××× S

unionunion RR ∪∪∪∪∪∪∪∪ SS differencedifference RR −−−−−−−− SS

intersectionintersection RR ∩∩∩∩∩∩∩∩ SS productproduct RR ×××××××× SS

•• IntersectionIntersectioncancanbebeexpressedexpressedviavia differencedifference:: R R ∩∩∩∩∩∩∩∩ S = S = R R −−−−−−−− (R (R −−−−−−−− S)S)

•• AttentionAttention!! Even Even ifif all all inputinput parametersparametersof of oneoneof of thesetheseoperatorsoperatorsarearerelations, relations, itit isis byby no no meansmeansguaranteedguaranteedthatthat thetheresultsresultsarearerelations, relations, tootoo. . ItIt maymaywell well bebethatthatapplyingapplyinga a setsetoperatoroperatorto relations to relations returnsreturns„„ justjust““ an an ordinaryordinarysetset, , butbut notnota a relationrelation! !

•• ThusThus, , not everynot every application application of of set operators set operators in RA in RA is is defineddefined! !

Page 39: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 3939

Set Set operatorsoperatorsforfor relations (2)relations (2)

(1,2,3)(1,2,3)(a,b)(a,b)

(2,4,5)(2,4,5)(c,d)(c,d)

(3,6,9)(3,6,9) (x,y)(x,y)

11 2 32 322 4 54 533 6 96 9

RR SSa ba bc dc dx yx y

Union of Union of twotwo inhomogeneousinhomogeneousrelations . . .relations . . .

. . . . . . results results in a in a setset, . . ., . . .

. . . . . . butbut not not in ain a relationrelation!!

RR ∪∪∪∪∪∪∪∪ SS

?

Page 40: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 4040

•• OnlyOnly „„ similarsimilar““ relations relations can becan beunitedunited, , intersectedintersectedoror subtractedsubtracted. For . For productproduct, , howeverhowever, , similarity is not requiredsimilarity is not required..

•• Relations Relations the union the union of of which is which is a a relation againrelation again, , are called are called

•• „„ SimilaritySimilarity““ of relations of relations cancanbebedefineddefinedin in variousvariouswaysways, a minimal , a minimal requirementrequirementbeingbeing

•• identical identical arityarity•• identityidentity of of typestypesof all of all columnscolumns. .

•• In In additionaddition, , identity identity of of namesnamesof all of all columnscolumnsin in bothbothrelations relations is often requiredis often required..

•• IdentityIdentity of of namesnamescancanbebereachedreachedbyby systematicsystematicrenamingrenamingof of columnscolumns. RA has. RA hasan an „„ auxiliaryauxiliary““ operator operator ρρρρρρρρ ((griechgriech. . rhorho) ) for denoting renamingsfor denoting renamings, e.g.:, e.g.:

ρρρρρρρρAA←←←←←←←←BB(R)(R) [In [In relationrelationR, R, columncolumnA A is renamed into is renamed into B.]B.]

Union Union compatibilitycompatibility

union compatible.unionunioncompatiblecompatible..

Page 41: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 4141

•• In In set theoryset theory, , thetheproductproductof of twotwo relations relations isis alwaysalwaysa a binarybinaryrelationrelation, , the elethe ele--ments ments of of which are pairs which are pairs of of tuplestuples..

•• If If e.g. e.g. tupletuple(a,b) (a,b) is is an an elementelementof of thethebinarybinaryrelationrelationA and A and tupletuple(1,2,3) (1,2,3) isis ananelementelementof of thetheternaryternaryrelationrelation, , then the product then the product of A and B of A and B contains the contains the pair pair ( (a,b) , (1,2,3) ).( (a,b) , (1,2,3) ).

•• In relational In relational algebraalgebra, , howeverhowever, , the product operator is defined the product operator is defined in a in a slightly slightly ((but but distinctivelydistinctively) different ) different mannermanner: : TuplesTuplesfromfrom bothbothoperandoperandrelations relations areareconcateconcate--natednatedintointo a a singlesingletupletuplebeforebeforebeingbeingenteredenteredintointo thetheproductproductrelationrelation::

Product Product of of two two relationsrelations

A A ×××××××× B B == { { ( a, b )( a, b )|| a a ∈∈ A and b A and b ∈∈ B B }}

(a,b)(a,b)

(1,2,3)(1,2,3)

(a,b,1,2,3) (a,b,1,2,3)

•• ThusThus, in RA , in RA thetheproductproductof an nof an n--aryary and an mand an m--aryary relation isrelation isan (n+m)an (n+m)--ary ary ((butbut notnot a a binarybinaryrelationrelation) !) !

Page 42: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 4242

Renaming while building productsRenaming while building products

NameNamePersNrPersNrProfProf MatrNrMatrNr NameNameStudStud

PersNrPersNr Prof.Prof.NameName Stud.Stud.NameNameMatrNrMatrNr

××××××××

•• If constructing the product If constructing the product of of two two relations, relations, renamingrenamingof of columns may be columns may be necessarynecessaryin order to in order to ensure that ensure that all all columns columns of of the result relation have the result relation have differentdifferentnamesnames. .

•• In order to In order to resolveresolveambiguitiesambiguities, , oneoneoftenoftenusesusesthethenamenameof of of of the origin relationthe origin relationof a of a column as column as prefixprefix for attributes for attributes in in the result relationthe result relation: :

Page 43: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 4343

Special Special operators for sets operators for sets of of tuplestuples

•• Basic Basic operatorsoperatorsfor tuple sets for tuple sets (i.e. relations) (i.e. relations) arearetwotwo unaryunaryoperatorsoperatorsforforextractingextracting. . .. . .

-- . . .. . . certaincertaincolumnscolumns::

-- . . .. . . certaincertainrowsrows((tuplestuples):):

projectionprojectionprojection

selectionselectionselection

ππ

σσ

•• In In addition addition to to thetheset operatorsset operators((adaptedadaptedto relations) RA to relations) RA offersoffersanotheranotherselecselec--tiontion of of specialspecialoperatorsoperators, , defined for sets defined for sets of of tuples onlytuples only..

•• Apart Apart from thesefrom these, , there are variousthere are variousderivedderivedoperatorsoperatorsbasedbaseduponuponprojection projection andandselectionselection(in (in combinationcombinationwithwith setsetoperatorsoperators):):

•• The various forms The various forms of of the the joinjoin operator are variantsoperator are variantsof of productproduct: : inner/inner/outer joinouter join, , natural joinnatural join

•• A A veryvery specialspecialform of form of differencedifferenceisis veryvery helpfulhelpful forfor formalisingformalisingcertaincertainvariantsvariantsof universal of universal quantificationquantificationin in setsettheorytheory: : divisiondivision

Page 44: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 4444

ProjectionProjection

ππ (R)(R)AA11, ... ,, ... ,AAkk

AAkk+1+1AA11 . . .. . . . . .. . .AAkk

•• TheTheprojection operatorprojection operatorππ „„ officiallyofficially ““ has has oneonerelational relational parameter onlyparameter only, , but but additionally needs one or more columns additionally needs one or more columns of of the operand relation as the operand relation as a a kind kind ofof„„ auxiliary parametersauxiliary parameters““ indicating indicating on on which columns which columns to to projectproject. .

•• In In principleprinciple, , one ought one ought to to say that there are very many say that there are very many different different projection projection operators insteadoperators insteadof just of just oneone: Per : Per combination combination of of columns columns on on which which to to project project there should be one there should be one such such operatoroperator. For . For simplicitysimplicity‘‘ s s sakesake, , howeverhowever, a , a „„ comprocompro--mise notationmise notation““ is usedis used::

•• All All columnscolumnsnotnot appearing asappearing asan an indexindexof of ππ areareeliminatedeliminatedbyby projectionprojection..

Page 45: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 4545

ProjectionProjectionand and duplicate eliminationduplicate elimination

•• While applying projection it may happen that the result relationWhile applying projection it may happen that the result relationcontains contains duplicatesduplicates–– tuplestuplesoccurringoccurringmoremorethanthanonceonce....

•• ThisThis maymayevenevenbebethethecasecaseifif thethe input relationinput relationitselfitself was was free free of of duplicatesduplicates((whichwhich oughtoughtto to bebethethecasecaseforfor eacheachproper proper relationrelation, , asasa a setset, , anywayanyway!).!).

A B CA B C

11 2 32 322 3 43 41 2 51 2 5

ππA,BA,B

A B A B

11 2 2 22 3 3

•• In order to In order to be able be able to to return return a a relation againrelation again, , it may be necessary it may be necessary to to eliminate eliminate duplicatesduplicates((whichwhich maymaybebean an expensiveexpensivetask fortask forlargelargerelations).relations).

•• ProjectionProjectionisis thetheonlyonly basic operationbasic operationof RA of RA requiringrequiringduplicateduplicateeliminationelimination..

Page 46: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 4646

SelectionSelection

σσcondcond(R)(R)AAnnAA11 . . .. . .

•• Selection conditionsSelection conditionsconsistconsistof of column namescolumn namesof R, of R, constantsconstants, , comparisoncomparisonoperators operators ( =, ( =, ≠≠ ,, << , , ≤≤ , , >> , , ≥≥ ) and ) and logical connectiveslogical connectives( ( ∧∧ , , ∨∨ , , ¬¬ ).).

•• The selection operatorThe selection operatorσσ –– which is unary which is unary in in principleprinciple, , tootoo –– needsneedsan an „„ auxiliary auxiliary parameterparameter““ asaswell. well.

•• A condition in A condition in thethesyntaxsyntaxof of propositionalpropositionallogiclogic, , composedcomposedof of comparisonscomparisonsofof

column valuescolumn values, , calledcalledselectionselectionconditionconditionis addedis addedto to σσ . All . All tuplestuplesof of thetheinputinput relationrelationnotnot satisfying thissatisfying thiscondition condition areareeliminatedeliminated. .

Page 47: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 4747

SelectionSelection(2)(2)

A B C A B C

11 a 3 a 3 8 a 48 a 47 b 67 b 66 x 136 x 13

Example for using the selection operatorExample for using the selection operator::

Find all Find all tuples tuples in in relation relation R, R, the the AA--fieldfield of of which is bigger than thewhich is bigger than theCC--fieldfield, and , and the the BB--fieldfield of of which is not which is not ‚‚ bb‘‘ ! !

the only tuple the only tuple satisfying this satisfying this

conditioncondition

σσA > C A > C ∧∧∧∧∧∧∧∧ B B ≠≠≠≠≠≠≠≠ ‚‚ bb‘‘ (R)(R)

RR

FormulationFormulationof of this query this query in relational in relational algebraalgebra::

Page 48: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 4848

Natural joinNatural join

•• TheThe„„ fullfull ““ productproductof of twotwo relations relations is not very usefulis not very usefulin in most situationsmost situations. . Very often Very often a a product is immediately product is immediately reducedreducedby eliminating rows by eliminating rows and and columnscolumns....

•• TheThemostmostfrequentlyfrequentlyused used such such variant results variant results in in two tables being connected viatwo tables being connected viaone or more one or more of of their columns their columns on on identical valuesidentical valuesin in each each of of these columnsthese columns, e.g.:, e.g.:

A B CA B C1 2 31 2 3

B C DB C D22 3 43 42 3 52 3 5

•• TheThemostmostnaturalnaturalway of way of buildingbuilding a table a table containing containing all such all such „„ connectionsconnections““ is byis bylooking for identical values looking for identical values in in columns with columns with identical nameidentical nameand and then then to to concateconcate--nate the tuples thus linked nate the tuples thus linked ((similarly similarly to to building building a a product relationproduct relation). ). Due Due to to the the identical valuesidentical values, , howeverhowever, , it is sufficientit is sufficientto to keep keep only one copyonly one copyof of the joinedthe joinedcolumns columns ((rather than two as rather than two as in a in a productproduct):):

RR SS

RR SSnatural joinnaturalnatural joinjoin

Page 49: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 4949

Natural joinNatural join(2)(2)

500150012612026120

500150012755027550

405240522755027550

CourseNrCourseNrMatrNrMatrNrstudentstudent

1010JohnJohn2612026120

1212EveEve2755027550

2811728117

MatrNrMatrNr

2727BillBill

SemesterSemesterNameNamecoursecourse

1212

1212

1010

SemesterSemester

50015001JohnJohn2612026120

50015001EveEve2755027550

2755027550

MatrNrMatrNr

40524052EveEve

CourseNrCourseNrNameName

studentstudent coursecourse

ExampleExampleof a of a natural joinnatural join: : Instead Instead of of the the 9 9 tuples tuples of a of a full productfull product, , only only 3 3 „„ meaningfulmeaningful““ combinationscombinationsof of tuples are kepttuples are kept!!

common join columncommoncommonjoin columnjoin column

Page 50: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 5050

NaturalNaturaljoinjoin (3)(3)

RR SS= = ππAA11, ... ,A, ... ,Amm, R.B, R.B11, ... ,R.B, ... ,R.Bk k ,C,C11, ... ,, ... ,CCnn

( ( σσR.BR.B11= S.B= S.B11 ∧∧ ... ... ∧∧ R.BR.Bkk= S.B= S.Bkk

( R ( R ×××××××× S ))S ))

. . .. . .AAmm CC11. . .. . . BBkkAA11 . . .. . . CCnnBB11

attrattr(R) (R) –– attrattr(S)(S) attrattr(R) (R) ∩∩ attrattr(S)(S) attrattr(S) (S) –– attrattr(R)(R)

•• TheThenaturalnaturaljoinjoin isis a a derivedderivedoperatoroperatorin RA in RA asasitsits effecteffectcould as could as well well be reached be reached by combining projectionby combining projection, , selection selection and and productproduct::

•• HereHere, , attrattr(R)(R) denotes the set denotes the set of all of all column names column names ((attributesattributes) in R. ) in R.

•• ObviouslyObviously, , usingusingthethespecialspecialoperatoroperatorresultsresultsin in muchmuchhigherhigherreadibilityreadibility of of thetheexpressionexpression. .

joinjoincolumnscolumns

Page 51: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 5151

Inner Inner joinjoin

•• ItIt isis notnot alwaysalwaysclearclearthatthatconcatenationconcatenationof of tuplestuplesbasedbasedon on identityidentity isis indeedindeedintendedintended. For . For expressingexpressingexplicit joinexplicit join conditionsconditionstherethereisis thethe inner inner joinjoin::•• nono automaticautomaticselectionselectionof of tuplestupleswithwith identicalidenticalfieldsfields•• nono automaticautomaticprojectionprojectionon "relevant" on "relevant" columnscolumns

•• ExampleExampleof an inner of an inner joinjoin:: R S mit R S mit ΘΘ = (R.A = (R.A ≤≤ S.C S.C ∧∧ S.B S.B >> 0)0)ΘΘ

RRA BA B

1 a1 a3 b3 b2 a2 a

B C B C

5 25 20 10 11 11 1

SS A R.B S.B CA R.B S.B C

1 a 5 21 a 5 21 a 1 11 a 1 12 a 5 22 a 5 2

•• The The joinjoin conditionconditionΘΘ is syntactically constructed like is syntactically constructed like a a selection selection condition. condition.

•• In Access, In Access, only theonly theinner inner join is supportedjoin is supported(no (no natural joinnatural join!).!).

Page 52: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 5252

Outer joinOuter join

RRA BA B

1 a1 a3 b3 b2 a2 a

B C B C

5 25 20 10 11 11 1

SS A R.B S.B CA R.B S.B C

11 a 5 2a 5 21 a 1 11 a 1 122 a 5 2a 5 233 b b NullNull NullNull

NullNull Null Null 0 0 11

R S mit R S mit ΘΘ = (R.A = (R.A ≤≤ S.C S.C ∧∧ S.B S.B >> 0)0)ΘΘ

•• There is There is an an outer joinouter joinas as well, well, which extends the which extends the inner inner join by maintaining thejoin by maintaining theinformation about information about nonnon--matching tuples matching tuples in in both input both input relations relations by by „„ joiningjoining““ themthemwith special with special „„ null null valuesvalues““ representing the fact that there is representing the fact that there is no no matchmatch..

•• ExampleExampleof an of an outer joinouter join::

•• If only If only nonnon--matching tuples from one matching tuples from one of of the partner the partner relations relations are are to to be filled be filled upupwith with Null, Null, left or left or right right outer joinouter joinis is to to be usedbe used: resp.: resp.

Page 53: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 5353

DivisionDivision

•• principleprincipleof of divisiondivision: : Which Which AA--values appear values appear in R in R combined with combined with allall SS--tuplestuples??

A BA B

a 1 a 1 a 2a 2a 3a 3b 2b 2b 3b 3

BB

1122

RR SS R R ÷÷ SS AA

a a

OnlyOnly 'a' 'a' appears appears in R in R combined combined withwith allall SS--tuplestuples !!

•• preciseprecisedefinitiondefinition ofof divisiondivision ((nearlynearlyunreadableunreadableforfor normal normal usersusersagainagain)): :

R R ÷÷÷÷÷÷÷÷ S :S :======== ππππππππ attrattr (R) (R) −−−−−−−− attrattr (S) (S) (R) (R) −−−−−−−− ππππππππ attrattr (R) (R) −−−−−−−− attrattr (S) (S) ((((ππππππππ attrattr (R) (R) −−−−−−−− attrattr (S) (S) (R)(R) ×××××××× S) S) −−−−−−−− R)R)

•• Most Most sophisticatedsophisticated, , but but also also quite useful operator quite useful operator of RA: of RA: divisiondivision

•• formal formal notationnotationlike like in in arithmeticsarithmetics: : RR ÷÷÷÷÷÷÷÷ SS

•• general ideageneral idea: : algebraic counterpart algebraic counterpart to universal to universal quantification quantification in in logic logic ((for for all)all)

Page 54: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 5454

Relational Relational algebraalgebra: : SummarySummary

base operatorsbase operators derivable operatorsderivable operators

unionunion intersection intersection differencedifferenceproductproduct joinjoinprojectionprojection divisiondivisionselectionselection

•• The following operators are comprised by theThe following operators are comprised by therelational relational algebraalgebra::

in different in different variantsvariants::inner, inner, outerouter, , naturalnatural

•• Query Query languages ablelanguages ableto express at least (to express at least (the effect the effect of) of) each each RARA operator operator are called are called relationally completerelationally complete. . ThusThus, RA , RA serves as serves as a a measure for the measure for the expressive power of DB expressive power of DB query languagesquery languages..

•• SQL SQL –– to to be presented be presented in in the next chapter the next chapter –– is is a a relationally complete languagerelationally complete language, , but exceeds but exceeds RA in RA in expressivityexpressivity..

Page 55: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 5555

Relational calculusRelational calculus

Relational calculusRelational calculusRelational calculus

−− 2.4 2.4 −−

∀∀∀∀∀∀∀∀∀∀∀∀∃∃∃∃∃∃∃∃∃∃∃∃

¬¬¬¬¬¬¬¬¬¬¬¬

Page 56: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 5656

Relational calculi: OverviewRelational calculi: Overview

tuple relational calculus (TRC)tupletuplerelational calculus (TRC)relational calculus (TRC)

domain relational calculus (DRC)domain relational calculus (DRC)domain relational calculus (DRC)

(short:(short:tupletuplecalculus)calculus)

(short: domain calculus)(short: domain calculus)

•• There is a logical alternative to relational algebra in There is a logical alternative to relational algebra in itsits role as a formalrole as a formalfoundafounda--tiontion of relational query languages: Formulating queries by means of of relational query languages: Formulating queries by means of terms andterms andformulas rather than set theoretic expressions.formulas rather than set theoretic expressions.

•• main differencemain difference: Logical languages are more : Logical languages are more ""descriptivedescriptive"" than algebraic ones;than algebraic ones;the order of processing is usually not visible in a formuthe order of processing is usually not visible in a formula. la.

•• InIn Codd'sCodd'sseminal paper, seminal paper, twotwo logical languages logical languages (called: calculi) for relational (called: calculi) for relational databases have been introduced, still forming the basis ofdatabases have been introduced, still forming the basis ofmost relational querymost relational querylanguages today:languages today:

Page 57: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 5757

Relational calculi: ComparisonRelational calculi: Comparison

Which students have been studying for more than 9 semesters ?Which students have been studying for more than 9 semesters ?Which students have been studying for more than 9 semesters ?

{ [ s.Name ] | Student(s) ∧∧∧∧s.Semester >>>> 9 }

{ [ s.Name ] | { [ s.Name ] | Student(s) Student(s) ∧∧∧∧∧∧∧∧s.Semester s.Semester >>>>>>>> 9 }9 }

{ [ n ] | ∃∃∃∃ nr, sem:Student(nr, n, sem) ∧∧∧∧sem >>>> 9 }

{ [ n ] | { [ n ] | ∃∃∃∃∃∃∃∃ nr,nr, semsem::Student(nr, n,Student(nr, n, semsem) ) ∧∧∧∧∧∧∧∧semsem >>>>>>>> 9 }9 }

tupletuplecalculuscalculus::•• nonnon--positionalpositional•• variables for entirevariables for entiretuplestuples•• attributes as attributes as

function symbolsfunction symbols•• relation names as relation names as

set typesset types

domain calculusdomain calculus::•• positionalpositional•• variables for variables for values in a domainvalues in a domain•• no attributes, no attributes,

just positionsjust positions•• DBDB--relation names as relation names as

relation symbols of the calculusrelation symbols of the calculus

tupletuple variablevariable domain variablesdomain variables

Page 58: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 5858

TRC: Formal definitionTRC: Formal definition

A TRC-query has the format{ v | F }

or the format{ [t 1, . . ., tn] | G },

where v is a tuple variable, F is a TRC-formula with free variable v, t1, . . ., tn is a TRC-term, and G is a TRC-formula with the same free variables as in t1, . . ., tn.

A A TRCTRC--queryqueryhas the formathas the format{ v | F }{ v | F }

or the formator the format{ [t{ [t 11, . . .,, . . .,ttnn] | G }] | G },,

where where vv is ais atupletuplevariable, variable, FF is a TRCis a TRC--formula with free variable v, formula with free variable v, tt11, . . .,, . . .,ttnn is a TRCis a TRC--term, and term, and GG is a TRCis a TRC--formula with the same free variables as in tformula with the same free variables as in t11, . . .,, . . .,ttnn..

ExamplesExamples:: { v | city(v) { v | city(v) ∧∧ v.Inhabitants v.Inhabitants >> 100 }100 }

{ v | major_city(v) { v | major_city(v) ∨∨ million_city(v) }million_city(v) }

{ [v.Name, v.Inhabitants] | city(v) { [v.Name, v.Inhabitants] | city(v) ∧∧ v.Inhabitants v.Inhabitants >> 100 }100 }

{ [v.Name] | major_city(v) { [v.Name] | major_city(v) ∨∨ million_city(v) }million_city(v) }

target listtarget list

qualification partqualification part

Page 59: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 5959

TRC: Formal definition (2)TRC: Formal definition (2)

TRC-terms:• constants and variables• attribute terms of the form v.A (v variable, A attribute)

TRCTRC--termsterms::•• constants and variablesconstants and variables•• attribute termsattribute termsof the form of the form v.Av.A (v variable, A attribute)(v variable, A attribute)

Attributes thus are Attributes thus are functions in TRCfunctions in TRC, written in , written in postfix notationpostfix notation: v.A instead of A(v): v.A instead of A(v)

TRC-formulas:• type formulas of the form R(v)

(v variable, R relation name)• comparison formulas of the form t1 Θ t2

(t1, t2 TRC-terms, Θ comparison operators)• If F is a formula, than so are

¬ F, (F) , ∃ v: (F) , ∀ v: (F) , too.• If F1 and F2 are formulas, then so are

(F1 ∧ F2) , (F1 ∨ F2) , (F1 ⇒ F2) , too.

TRCTRC--formulasformulas::•• type formulastype formulasof the form of the form R(v)R(v)

(v variable, R relation name)(v variable, R relation name)•• comparison formulascomparison formulasof the form of the form tt11 ΘΘ tt22

(t(t11, t, t22 TRCTRC--terms, terms, ΘΘ comparison operators)comparison operators)•• If F is a formula, than so are If F is a formula, than so are

¬¬ FF, , (F)(F) , , ∃∃ v: (F)v: (F) , , ∀∀ v: (F) v: (F) , too., too.•• If FIf F11 and Fand F22 are formulas, then so areare formulas, then so are

(F(F11 ∧∧ FF22)) , , (F(F11 ∨∨ FF22)) , , (F(F11 ⇒⇒ FF22)) , too., too.

Brackets can be omittedBrackets can be omitted, if, ifthe order of brackets is unique.the order of brackets is unique.

Page 60: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 6060

DRC: Formal definitionDRC: Formal definition

A DRC-query has the format{ [t 1, . . ., tn] | F },

where t1, . . ., tn are DRC-terms and F is a DRC-formula with the same free variables as in t1, . . ., tn.

A A DRCDRC--queryqueryhas the formathas the format{ [t{ [t 11, . . .,, . . .,ttnn] | F }] | F },,

where where tt11, . . .,, . . .,ttnn are DRCare DRC--terms and terms and FF is a DRCis a DRC--formula with the same free variables as in tformula with the same free variables as in t11, . . .,, . . .,ttnn..

ExamplesExamples:: { [x{ [x 11,x,x22,x,x33] | city(x] | city(x11,x,x22,x,x33) ) ∧∧ xx33 >> 100 }100 }

{ [x{ [x 11,x,x22,x,x33] | major_city(x] | major_city(x11,x,x22,x,x33) ) ∨∨ million_city(xmillion_city(x11,x,x22,x,x33) }) }

target listtarget list

qualification partqualification part

{ [x{ [x 11,x,x33] | ] | ∃∃ xx22:: ((city(xcity(x11,x,x22,x,x33) ) ∧∧ xx33 >> 100) }100) }

{ [x{ [x 11] | ] | ∃∃ xx22,x,x3 3 :: ((major_city(xmajor_city(x11,x,x22,x,x33) ) ∨∨ million_city(xmillion_city(x11,x,x22,x,x33)) })) }

free variablesfree variables

DRCDRC--terms and terms and ––formulas can be formally defined as in TRC (omitted here). formulas can be formally defined as in TRC (omitted here).

Page 61: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 6161

Relational algebra expressed in relational calculusRelational algebra expressed in relational calculus

•• All expressions of All expressions of relational algebrarelational algebracan be can be equivalently equivalently expressed by meansexpressed by meansof queries in each of the two calculi, too. of queries in each of the two calculi, too.

•• ProjectionProjection::

•• SelectionSelection::

•• UnionUnion (Intersection, difference and product analogously):(Intersection, difference and product analogously):

πA(R) {[v.A] | R(v) } { [a] | ∃∃∃∃ b, c, . . . : R(a, b, c, . . .)}ππAA(R)(R) {[v.A] | R(v) } { [a] | {[v.A] | R(v) } { [a] | ∃∃∃∃∃∃∃∃ b, c, . . . : R(a, b, c, . . .)}b, c, . . . : R(a, b, c, . . .)}

σCond(R) { v | R(v) ∧∧∧∧ Cond’} { [a1, . . , an] | R(a1, . . , an) ∧∧∧∧ Cond}σσCondCond(R)(R) { v | R(v) { v | R(v) ∧∧∧∧∧∧∧∧ CondCond’’ } { [a} { [a11, . . , a, . . , ann] | R(a] | R(a11, . . , a, . . , ann)) ∧∧∧∧∧∧∧∧ CondCond}}

R ∪ S { v | R(v) ∨∨∨∨ S(v) } { [a1, . . , an] | R(a1, . . , an) ∨∨∨∨ S(a1, . . , an)}R R ∪∪ SS { v | R(v) { v | R(v) ∨∨∨∨∨∨∨∨ S(v) } { [aS(v) } { [a11, . . , a, . . , ann] | R(a] | R(a11, . . , a, . . , ann) ) ∨∨∨∨∨∨∨∨ S(aS(a11, . . , a, . . , ann)})}

RA TRC RA TRC DRCDRC

Page 62: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 6262

Relational algebra expressed in relational calculus (2)Relational algebra expressed in relational calculus (2)

•• Join Join (here natural join of R(A,B) and S(B,C), as an example):(here natural join of R(A,B) and S(B,C), as an example):

•• Division Division (here with R(A,B) and S(B):(here with R(A,B) and S(B):

R S {[v.A, v.B, w.C] | R(v) ∧ S(w) ∧ v.B = w.B }

{ [a, b, c] | R(a, b) ∧ S(b, c) }

R S R S {[v.A, v.B, w.C] | R(v) {[v.A, v.B, w.C] | R(v) ∧∧ S(w) S(w) ∧∧ v.B = w.B } v.B = w.B }

{ [a, b, c] | R(a, b) { [a, b, c] | R(a, b) ∧∧ S(b, c) }S(b, c) }

RARA TRCTRC

DRCDRC

R ÷ S {[v.A] | R(v) ∧ ∀ w: ( S(w) ⇒∃ v': ( R(v') ∧

v'.B = w.B ∧ v'.A = v.A ) )}

{ [a] | R(a, b) ∧ ∀ b': ( S(b') ⇒ R(a, b') ) }

R R ÷÷ S S {[v.A] | R(v) {[v.A] | R(v) ∧∧ ∀∀ w: ( S(w) w: ( S(w) ⇒⇒∃∃ v': ( R(v') v': ( R(v') ∧∧

v'.B = w.B v'.B = w.B ∧∧ v'.A = v.A ) )}v'.A = v.A ) )}

{ [a] | R(a, b) { [a] | R(a, b) ∧∧ ∀∀ b': ( S(b') b': ( S(b') ⇒⇒ R(a, b') ) }R(a, b') ) }

RARA TRCTRC

DRCDRC

Page 63: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 6363

Relational calculi Relational calculi ↔↔ relational algebra: Comparisonrelational algebra: Comparison

RARA--expressionsexpressions

TRCTRC-- (or DRC(or DRC--)expressions)expressions

?? notnot expressexpressiibleble in RAin RA

equivalent representationsequivalent representationsof RAof RA--expressionsexpressions

•• One could assume that vice versa One could assume that vice versa eacheachexpression in a calculus isexpression in a calculus isexpressableexpressablein RA as well in RA as well –– but this is not the case!but this is not the case!

•• In each calculus, there are queries In each calculus, there are queries notnot expressableexpressablein RA !in RA !

Page 64: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 6464

Unsafe expressions in relational calculusUnsafe expressions in relational calculus

•• „„ Uncontrolled" usage of Uncontrolled" usage of negationnegationand and disjunctiondisjunctionmay lead tomay lead toexpresssionsexpresssionsdenoting denoting infinitely large answer relations infinitely large answer relations ((respresp., in answer relations not constructible from DB., in answer relations not constructible from DB--relations alone).relations alone).Such expressions are called Such expressions are called unsafeunsafe..

•• ExamplesExamples: unsafe queries in TRC : unsafe queries in TRC

{ v | { v | ¬¬ city(v) }city(v) }

{ [f.Name, s.Name] | { [f.Name, s.Name] | river(f) river(f) ∨∨ city(s) }city(s) }

{ [x{ [x 11, x, x22, x, x33] | ] | ¬¬ city(xcity(x11, x, x22, x, x33) }) }

{ [y{ [y 11, x, x11] | ] | ∃∃ yy22, y, y33, , xx22, x, x33: : river(river(yy11, y, y22, y, y33) ) ∨∨ city(xcity(x11, x, x22, x, x33) }) }

•• But this one is But this one is safesafe: : { [x.Name] |{ [x.Name] | riverriver(x)(x) ∨∨ citycity(x)(x) } }

•• analogouslyanalogously: unsafe queries in DRC: unsafe queries in DRC

Find all (Find all (imaginableimaginable)) tuplestuplesnotnot occurring inoccurring inrelation relation ‚‚ citycity‘‘ ! , ! ,

If the If the disjunctiondisjunctionis satisfied by a riveris satisfied by a river--tupletuple, than s can be replace by an , than s can be replace by an arbitraryarbitrarytupletuple (or vice versa) !(or vice versa) !

So, logic may be dangerous for you!Take care!

So, logic may be So, logic may be dangerous for you!dangerous for you!Take care!Take care!

Page 65: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 6565

Relational completenessRelational completeness

•• The The expressive power of relational algebraexpressive power of relational algebrais taken as a measure for the qualityis taken as a measure for the qualityof a relational query language: of a relational query language:

A language is called A language is called relationally completerelationally complete, if it is able to offer an , if it is able to offer an equivalent query for each expression of RA. equivalent query for each expression of RA.

•• Both Both relational calculirelational calculi(TRC and DRC) are (TRC and DRC) are relationally completerelationally complete..(as we have shown by example before)(as we have shown by example before)

•• If TRC and DRC are restricted to safe queries, the resultingIf TRC and DRC are restricted to safe queries, the resultingsublanguagessublanguagesareareequivalent, and equivalent with RA, too:equivalent, and equivalent with RA, too:

relational algebrarelational algebrarelational algebra

safe TRCsafe TRCsafe TRC

safe DRCsafe DRCsafe DRC

Page 66: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 6666

Weaknesses of relationally complete languagesWeaknesses of relationally complete languages

"Relationally complete" languages are notas complete as they seem to be . . . !"Relationally complete" languages are "Relationally complete" languages are notnot as complete as they seem to be . . . !as complete as they seem to be . . . !

•• NeverthelessNevertheless,, there is a number of there is a number of reasonable queriesreasonable queriesnotnot expressible in RAexpressible in RA(and thus not in the safe calculi either). (and thus not in the safe calculi either).

•• To cover these case as well, soTo cover these case as well, so--called relationally complete languagescalled relationally complete languagesstillstillhave to be have to be extendedextended..

•• Extensions by Extensions by arithmeticarithmeticand and aggregate functionsaggregate functions(including grouping(including grouping--and sorting operators) are inevitable for practical applicand sorting operators) are inevitable for practical applications.ations.

•• Boolean queriesBoolean queries(yes/no queries) cannot at all be expressed in RA without(yes/no queries) cannot at all be expressed in RA withoutextending it by comparisons of RA expressions with the empextending it by comparisons of RA expressions with the empty relation.ty relation.

•• Another important drawback of RA is the lack of rAnother important drawback of RA is the lack of recursive queriesecursive queriesable toable torefer to a subset of its answer as many times as necessaryrefer to a subset of its answer as many times as necessary, e.g. for , e.g. for determdeterm--iningining all paths of arbitrary length in a traffic network. Deductive dall paths of arbitrary length in a traffic network. Deductive databasesatabasesare able to compensate for this deficiency.are able to compensate for this deficiency.

Page 67: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 6767

Relational query languagesRelational query languages

Relational query languagesRelational query languagesRelational query languages

−− 2.5 2.5 −−

∀∀∀∀∀∀∀∀∀∀∀∀ππππππππ

←←←←←←←←

Page 68: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 6868

OverviewOverview

•• in this brief sectionin this brief section::•• classificationclassificationof relational query languages used in of relational query languages used in „„ real lifereal life““

with respect to algebra and calculi with respect to algebra and calculi •• short introduction short introduction toto two moretwo morerelational languagesrelational languages

(in addition to (in addition to SQLSQL and and QBEQBE): ): QUEL andQUEL andDatalogDatalog

•• Each of these four Each of these four „„ concrete" languages (SQL, QBE, QUEL,concrete" languages (SQL, QBE, QUEL,DatalogDatalog) is) isrelationally completerelationally complete..

•• Most of these languages exceed RA inMost of these languages exceed RA inexpressivityexpressivity. They have . . .. They have . . .•• . . . arithmetic and aggregate functions. . . arithmetic and aggregate functions•• . . . grouping and sorting. . . grouping and sorting•• . . . various "built. . . various "built--inin„„ operators (particularly in SQL)operators (particularly in SQL)•• . . . recursive queries (in SQL:1999 and. . . recursive queries (in SQL:1999 andDatalogDatalog) )

•• QUEL (based on TRC) andQUEL (based on TRC) andDatalogDatalog(based(basedon DRC) are on DRC) are „„ purepure““ languages. languages. SQL (TRC and RA) and QBESQL (TRC and RA) and QBE(DRC and TRC) in comparison are (DRC and TRC) in comparison are „„ hybridhybrid““languages, blending elements of different styles. languages, blending elements of different styles.

Page 69: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 6969

SQL and relational algebra SQL and relational algebra

•• SQLSQL: : „„ History" and principles will be treated extensively in chapter 3History" and principles will be treated extensively in chapter 3..

•• Basic concept of SQL is the 'SELECTBasic concept of SQL is the 'SELECT--FROMFROM--WHEREWHERE‘‘ block, which can beblock, which can beseen as "syntactic sugar" for the most common kind of RAseen as "syntactic sugar" for the most common kind of RA expression compoexpression compo--sedsedfrom projection, selection and join:from projection, selection and join:

SELECT Name, InhabitantsFROM city, countryWHERE Inhabitants >= 1000 AND Name=Capital ;

SELECTSELECT Name, InhabitantsName, InhabitantsFROMFROM city, countrycity, countryWHEREWHERE Inhabitants Inhabitants >=>= 1000 AND Name=Capital ;1000 AND Name=Capital ;

ππππππππName,InhabitantsName,Inhabitants

( ( σσσσσσσσInhabitants Inhabitants ≥≥≥≥≥≥≥≥ 1000 1000 ∧∧∧∧∧∧∧∧ Name Name == Capital Capital

( city ( city ×××××××× country ))country ))

•• another another "RA"RA--inheritance":inheritance": Operators UNION, INTERSECT and MINUS forOperators UNION, INTERSECT and MINUS forcombining SFWcombining SFW--blocks and more complex queriesblocks and more complex queries

SQLSQL

RARA

Page 70: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 7070

SQL and TRCSQL and TRC

•• But SQL may as well be called a But SQL may as well be called a TRCTRC--style languagestyle language..

•• correspondencecorrespondencebetweenbetweenTRC TRC andandSQLSQL‘‘ ss SFWSFW--blockblock::

{[x.Name, x.Inhabitants] |city(x) ∧∧∧∧ x.Einwohner≥ 1000 ∧∧∧∧(∃ y: country(y) ∧∧∧∧ x.Name = y.Capital) }

{[x.Name, x.Inhabitants] |{[x.Name, x.Inhabitants] |city(x) city(x) ∧∧∧∧∧∧∧∧ x.x.EinwohnerEinwohner≥≥ 1000 1000 ∧∧∧∧∧∧∧∧((∃∃ y: y: country(y) country(y) ∧∧∧∧∧∧∧∧ x.Name = y.Capital)x.Name = y.Capital)}}

Target listTarget listTypingTypingConditionCondition

•• The use of a The use of a quantifierquantifiershows that indeed TRC is meant and not a shows that indeed TRC is meant and not a selection condition in RA. selection condition in RA.

SELECT Name, InhabitantsFROM city, countryWHERE Inhabitants >= 1000 AND Name=Capital ;

SELECTSELECT Name, InhabitantsName, InhabitantsFROMFROM city, countrycity, countryWHEREWHERE Inhabitants Inhabitants >=>= 1000 AND Name=Capital ;1000 AND Name=Capital ;

Page 71: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 7171

QBEQBE

•• "Query by Example" (QBE)"Query by Example" (QBE)has been developed in the midhas been developed in the mid--1970s at IBM1970s at IBM(like SQL) and has been documented in a paper by Moshe(like SQL) and has been documented in a paper by MosheZloofZloof in 1975.in 1975.

•• The QBEThe QBE--style became popular style became popular bby y itsits "free "free aadaptdaptatation" iion" in n thetheMicrosoft Microsoft AccessAccessDBMS, DBMS, where it where it has has been extended by been extended by TRCTRC--like like „„ criteriacriteria““ ..

•• In In itsits originaloriginal fformorm ((alreadyalreadyin ain a graphigraphical cal notationnotation) QBE ) QBE was awas apure DRCpure DRClanguagelanguage. .

•• QBE QBE usesusespositionpositionalal ssyntaxyntaxaand nd has vhas variableariables s representingrepresentingaattributttribute e valuesvalues, e.g.;, e.g.;

ID ID Name Name Inhabitants Inhabitants

'BN' 'BN' _a_app.._n_n _b_b

citycity

_a _a << _b_b

conditionsconditionsFind Find thethennameamessof all of all citiescitiesbigger thanbigger thanBonn ?Bonn ?

output operatoroutput operator'p' (f'p' (foor 'print')r 'print') domaindomain

variablevariabless

separateseparateconditioncondition

„„ bbox"ox"

Page 72: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 7272

QUELQUEL

•• During theDuring the19701970s, SQL s, SQL had had a a seriousserious„„ competitorcompetitor" " whichwhich, , howeverhowever, , failed failed totosucceed commerciallysucceed commercially((even thougheven thoughit is regarded asit is regarded as„„ cleanercleaner" " thanthanSQSQLL))

•• QUEL QUEL has has been developedbeen developediin n thetheIngresIngrespprojerojecctt, , ananacademicacademicpprojerojecct t for thefor thedevelopment development of of thethefirstfirst relational DBMS (in relational DBMS (in competitioncompetitionwithwith IBMIBM ‘‘ s s commercialcommercialSystem RSystem R)) headed by theheaded by theDBDB--pioneerpioneerMichaelMichaelStonebrakerStonebraker). ).

•• FromFromIngresIngres,, firstfirst PostGresPostGresemerged emerged ((aa ffreewarereewareDBMSDBMS todaytoday) ) aand nd thanthanthethecommercial commercial systemsystemInformixInformix, , which Stonebraker sold which Stonebraker sold to IBM a to IBM a few years agofew years ago..

•• characharaccteristic teristic exampleexample::

•• the samethe samequeryqueryin SQL in SQL ((asascomparisoncomparison):):

RANGE OF x IS cityRANGE OF y IS countryRETRIEVE x.NameWHERE x.Name = y.Capital AND

x.Inhabitants≥ 1000

RANGE OF RANGE OF xx IS IS citycityRANGE OF RANGE OF yy IS IS countrycountryRETRIEVE RETRIEVE xx..NameNameWHERE WHERE xx.Name = .Name = yy..CapitalCapitalANDAND

xx..InhabitantsInhabitants≥≥ 10100000

pure pure TRCTRC--style languagestyle language

SELECT SELECT x.Namex.NameFROM FROM citycity AS AS xx, , countrycountryAS AS yyWHERE WHERE xx.Name = .Name = yy..CapitalCapital AND s.AND s.EinwohnerEinwohner≥≥ 101000 00

QUELQUELQUEL ((QUEQUEryry LLanguage)anguage)

Page 73: Foundations of Information Systems (WS 2004/05)Foundations ... · • The (mathematical) concept of a set is of fundamental importance for almost every area of computer science. •

©© 2004 2004 Prof. Dr. Rainer Prof. Dr. Rainer MantheyManthey Foundations Foundations of ISof IS 7373

Datalog Datalog

•• Up Up till now used till now used in anin anaaccademic ademic ccontext ontext onlyonly: : „„ purepure" " DRCDRC--languagelanguage

•• The naming goes The naming goes back to back to the the logic logic programming languageprogramming languagePrologProlog("Programming("Programmingin Logic")in Logic") which which has has beenbeendeveloped developed in in thethemidmid--19801980ss in in thetheUS (Maier,US (Maier,UllmanUllman).).

•• Main Main construct construct of of DatalogDatalog: : RRuleule (= Def. (= Def. of aof a derivedderivedrrelation; elation; view view in SQLin SQL))

e.g.:e.g.:p(X) ← q(X, Y) , r(Y, Z)p(X) p(X) ←← q(X, q(X, YY) , r() , r(YY, Z), Z)

nname ame andandstructurestructureof of thetheresult relationresult relation

cconjunonjuncctiontion

jjoinoin expressedexpressedviavia identicidenticalal

vvariableariable

DatalogDatalogDatalog

•• Datalog Datalog can be regarded ascan be regarded asa a purelypurely textutextual val versionersionofof QBE.QBE.