part vii query basics: algebra & calculus

69
Part VII Query Basics: Algebra & Calculus

Upload: others

Post on 29-Dec-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Part VII Query Basics: Algebra & Calculus

Part VII

Query Basics: Algebra & Calculus

Page 2: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus

1 Criteria for Queries

2 Query Algebras

3 Relational Algebra: Extensions

4 Query Calculus

5 Examples for the Domain Calculus

Saake Database Concepts Last Edited: April 2019 7–1

Page 3: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Criteria for Queries

Learning goals for today . . .

Understanding of formal basics ofrelational query languagesKnowledge to formalize queries withrelational algebraKnowledge to formalize calculus queries

Saake Database Concepts Last Edited: April 2019 7–2

Page 4: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Criteria for Queries

Introduction

so far:I Relation schemata with basic relations that are saved in databases

now:I "derived" relation schemata with virtual relations that are calculated

from basis relations (basis relations keep unchanged)

Saake Database Concepts Last Edited: April 2019 7–3

Page 5: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Criteria for Queries

Terms

Query: Sequence of operations that calculate a result relationfrom basis relations

I Present result relations interactively on a monitor orI further processing via program ("Embedding")

View: Sequence of operations that is long-term saved under aview name and that can be called again with this name; results ina view relationSnapshot: Result relation of a query that is stored under aSnapshot-Name, but that is never calculated twice (with changedbasis relations) (e.g., annual balance sheets)

Saake Database Concepts Last Edited: April 2019 7–4

Page 6: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Criteria for Queries

Criteria for Query Languages

Ad-Hoc-Formalization: User should be able to formalize a querywithout a need to write a complete programDescriptiveness: User should formalize "What do I want?"instead of "How do I get what I want?"Collection-based: Each operation should operate on a collectionof data at the same time, not navigating on single elements("one-tuple-at-a-time")Closure: Result is again a relation that can be used as input forthe next query

Saake Database Concepts Last Edited: April 2019 7–5

Page 7: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Criteria for Queries

Criteria for Query Languages /2

Adequacy: All constructs of the underlying data model aresupportedOrthogonality: Language constructs are in similar situations alsosimilar applicableOptimizability: Language consists of few operations for whichoptimization rules existEfficiency: Any operation is efficient executable (in the relationmodel each operation has a complexity O(n2), n number oftuples of the relation).

Saake Database Concepts Last Edited: April 2019 7–6

Page 8: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Criteria for Queries

Criteria for Query Languages /3

Safety: No query, that is syntactically correct, may result in anendless loop or give an infinite resultLimitation: (comes from Safety, Optimizability, Efficiency) Querylanguage should not be a complete programming languageCompleteness: The language must at least being able to expressthe queries of a standard language (such as the to be introducedrelation algebra of this chapter or the safe relation calculus)

Saake Database Concepts Last Edited: April 2019 7–7

Page 9: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Query Algebra

Math: Algebra defined with range of values and on this definedoperatorsFor database queries: Contents of the database are values, andoperators define functions for the calculation of query results

I Relational AlgebraI Algebra Extensions

Saake Database Concepts Last Edited: April 2019 7–8

Page 10: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Relational Algebra

Hide Columns: Projection ⇡

Search Rows: Selection �

Joining Tables: Join onUnion of Tables: Union [Subtract Tables from each other: Difference �Rename Columns: Renaming �(important for on and [,�)

Saake Database Concepts Last Edited: April 2019 7–9

Page 11: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Relational Algebra: Overview

a1 b2

a2 b2

b2 c3

b3 c4

a2 b3 b4 c5

a1 b2

a2 b2

a2 b3

c3

c3

c4

Join

Selection Projection

Saake Database Concepts Last Edited: April 2019 7–10

Page 12: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Projection

Syntax⇡AttributeSet (Relation)

Semantics⇡X(r) := {t(X) | t 2 r}

For r(R) and X ✓ R attribute set in R

Property for Y ✓ X ✓ R

⇡Y(⇡X(r)) = ⇡Y(r)

Attention: ⇡ removes duplicates (Set Semantics)

Saake Database Concepts Last Edited: April 2019 7–11

Page 13: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Projection: Example

⇡Region(PRODUCER)

Region

South AustraliaKalifornienBordeauxHessen

Saake Database Concepts Last Edited: April 2019 7–12

Page 14: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Projection: Example 2

⇡District,Region(PRODUCER)

District Region

Barossa Valley South AustraliaNapa Valley KalifornienSaint-Emilion BordeauxPomerol BordeauxRheingau Hessen

Saake Database Concepts Last Edited: April 2019 7–13

Page 15: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Selection

Syntax�Condition(Relation)

Semantics (for A 2 R)

�A=a(r) := {t 2 r | t(A) = a}

Saake Database Concepts Last Edited: April 2019 7–14

Page 16: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Selection Conditions

Constant SelectionAttribute ✓ Constant

boolean predicate ✓ is = or 6=, for linear ordered range of valuesalso , <, � or >Attribute Selection

Attribute1 ✓ Attribute2

logic connection of multiple Constant- or Attribute-Selections with^,_ or ¬

Saake Database Concepts Last Edited: April 2019 7–15

Page 17: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Selection: Properties

Commutativity

�A=a(�B=b(r)) = �B=b(�A=a(r))

when A 2 X, X ✓ R

⇡X(�A=a(r)) = �A=a(⇡X(r))

Distributivity respect. [, \, �

�A=a(r [ s) = �A=a(r) [ �A=a(s)

Saake Database Concepts Last Edited: April 2019 7–16

Page 18: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Selection: Example

�Vintage>2000(WINES)

WineID Name Color Vintage Vineyard

2168 Creek Shiraz Red 2003 Creek3456 Zinfandel Red 2004 Helena2171 Pinot Noir Red 2001 Creek4961 Chardonnay White 2002 Bighorn

Saake Database Concepts Last Edited: April 2019 7–17

Page 19: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Join

Syntax of the natural joinRelation1 on Relation2

Semantics

r1 on r2 := {t | t(R1 [ R2) ^[8i 2 {1, 2}9ti 2 ri : ti = t(Ri)]}

Join links tables over equally named columns at equal attributevalues

Saake Database Concepts Last Edited: April 2019 7–18

Page 20: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Join: Properties

Schema for r(R) on r(S) is union of the attribute sets RS = R [ S

from R1 \ R2 = {} follows r1 on r2 = r1 ⇥ r2

Commutativity: r1 on r2 = r2 on r1

Associativity: (r1 on r2) on r3 = r1 on (r2 on r3)

thus allowed:onp

i=1 ri

Saake Database Concepts Last Edited: April 2019 7–19

Page 21: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Join: Example

WINES on PRODUCER

WineID Name . . . Vineyard District Region

1042 La Rose Grand Cru . . . Ch. La Rose Saint-Emilion Bordeaux2168 Creek Shiraz . . . Creek Barossa Valley South Australia3456 Zinfandel . . . Helena Napa Valley Kalifornien2171 Pinot Noir . . . Creek Barossa Valley South Australia3478 Pinot Noir . . . Helena Napa Valley Kalifornien4711 Riesling Reserve . . . Müller Rheingau Hessen4961 Chardonnay . . . Bighorn Napa Valley Kalifornien

Saake Database Concepts Last Edited: April 2019 7–20

Page 22: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Renaming

Syntax�new old(Relation)

Semantic�B A(r) := {t

0 | 9t 2 r : t0(R � A) = t(R � A) ^ t

0(B) = t(A)}changes attribute names from old to new

�Name LastName (CRITIC)

with renaming now possibleI Join, where Cartesian products were applied (different attributes

get equal naming),I Cartesian products, where Joins were applied (equal attributes get

different naming),I Set operations

Saake Database Concepts Last Edited: April 2019 7–21

Page 23: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Calculation of the Cross Product

Natural Join degenerates to a cross product, when no sharedattributes existEnforce by renaming

I Example: R1(A,B,C) and R2(C,D)

R1 ⇥ R2 ⌘ R1 on �E C(R2)

Cross product + selection simulates natural joinR1 on R2 ⌘ �R1.C=R2.C(R1 ⇥ R2)

Saake Database Concepts Last Edited: April 2019 7–22

Page 24: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Set Operations: Semantics

formal for r1(R) and r2(R)I Union r1 [ r2 := {t | t 2 r1 _ t 2 r2}I Intersection r1 \ r2 := {t | t 2 r1 ^ t 2 r2}I Difference r1 � r2 := {t | t 2 r1 ^ t 62 r2}

Intersection \ is superfluous as r1 \ r2 = r1 � (r1 � r2)

Saake Database Concepts Last Edited: April 2019 7–23

Page 25: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Algebras

Independence and Completeness

Minimal relational algebra:⌦ = ⇡, �, on, �, [ and �

independence: no operator can be left off without loosingcompletenessother independent set: on and � replaced by ⇥relational Completeness: every other set of operators with sameexpressive power as ⌦

strict relational completeness: for any expression with operatorsout of ⌦ there is an equivalent expression also with the other setof operations

Saake Database Concepts Last Edited: April 2019 7–24

Page 26: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Relational Algebra: Extensions

further Join operationsDivisionGrouping and nested grouping relations. . .

Saake Database Concepts Last Edited: April 2019 7–25

Page 27: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Join Variants

for L(AB), R(BC), S(DE)

Equi-Join: Equality condition over explicit specified and possiblydifferent attributes

r(R) onC=D r(S)

Theta-Join (✓-join): arbitrary join condition

r(R) onC>D r(S)

Semi-Join: only attributes of one operand appear in the result

r(L)n r(R) = ⇡L(r(L) on r(R))

Outer Join

Saake Database Concepts Last Edited: April 2019 7–26

Page 28: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Outer Join

Adoption of "dangling tuples" into the result and fill up with nullvaluesFull Outer Join takes all tuples of both operands

r A./@ s

Left Outer Join takes all tuples of the left operand

r A./ s

Right Outer Join takes all tuples of the right operand

r ./@ s

Saake Database Concepts Last Edited: April 2019 7–27

Page 29: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Outer Join /2

LEFT A B1 22 3

RIGHT B C3 44 5

on A B C2 3 4

A./@ A B C1 2 ?2 3 4? 4 5

A./ A B C1 2 ?2 3 4

./@ A B C2 3 4? 4 5

left outer join full outer joinright outer joinnatural join

Saake Database Concepts Last Edited: April 2019 7–28

Page 30: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Problem: Quantifiers

Express universal quantification (allquantor) in relational algebra,even though quantification in the selection condition is not allowedDivision (can be derived from ⌦)r1(R1) and r2(R2) given with R2 ✓ R1, R

0 = R1 � R2. Than is

r0(R0) = {t | 8t2 2 r29t1 2 r1 : t1(R

0) = t ^ t1(R2) = t2}= r1 ÷ r2

Division of r1 by r2

r1 ÷ r2 = ⇡R0(r1)� ⇡R0((⇡R0(r1) on r2)� r1)

Saake Database Concepts Last Edited: April 2019 7–29

Page 31: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Division Example I

WINE_RECOMMENDATION Wine Critic

La Rose Grand Cru ParkerPinot Noir ParkerRiesling Reserve ParkerLa Rose Grand Cru ClarkePinot Noir ClarkeRiesling Reserve Gault-Millau

GUIDES1 Critic

ParkerClarke

GUIDES2 Critic

ParkerGault-Millau

Saake Database Concepts Last Edited: April 2019 7–30

Page 32: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Division Example II

Division with first table

WINE_RECOMMENDATION÷ GUIDES1

givesWine

La Rose Grand CruPinot Noir

Division with second critics table

WINE_RECOMMENDATION÷ GUIDES2

givesWine

Riesling Reserve

Saake Database Concepts Last Edited: April 2019 7–31

Page 33: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Term Division

Analogy to the arithmetic operation of integer division

The integer division is in this sense the inverse to the multiplicationby giving the result of the biggest number for which the multiplicationwith the divisor is smaller than the dividend.Analogously holds: r = r1 ÷ r2 is the biggest relation for whichr on r2 ✓ r1 is.

Saake Database Concepts Last Edited: April 2019 7–32

Page 34: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Division in SQL

Simulation of the Allquantor (Division)with double Negation:

select distinct Winefrom WINE_RECOMMENDATION w1where not exists (

select * from GUIDES2 gwhere not exists (

select * from WINE_RECOMMENDATION w2where g.Critic = w2.Critic and w1.Wine = w2.Wine))

I "Gives all wines, thus no wine exists that is not recommended by allcritics in the relation GUIDES2".

I (We use the relation GUIDES2, as the in the textbook stated resultrelation refers to this comparison relation.)

Saake Database Concepts Last Edited: April 2019 7–33

Page 35: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Grouping

Grouping Operator �:

�f1(x1),f2(x2),...,fn(xn);A(r(R))

I Extends the attribute scheme of r(R) by new attributes thatcorresponds with the function application f1(x1), f2(x2), . . . , fn(xn)

I Application of the function fi(xi) on the subset of the tuples of r(R)which have the same attribute values for the attributes A

select f1(x1), f2(x2), ..., fn(xn); Afrom Rgroup by A

Saake Database Concepts Last Edited: April 2019 7–34

Page 36: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Relational Algebra: Extensions

Semantics of the Grouping Operator

empty attribute set A = ;:

�F(X);;(r(R)) = r(R)⇥ r(R)F(X)

with r(R)F(X) is relation with attribute F(X) and a tuple as value ofF(X) on r(R)

without function:

�;;;(r(R)) = r(R)

general case:

�F(X);A(r(R)) =[

t2R

�F(X);;(�A=t.A(r(R)))

Saake Database Concepts Last Edited: April 2019 7–35

Page 37: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Query Calculus

Calculus: A formal logic language to formalize statementsGoal: The use of such a calculus to formalize database queriesLogic-based approach:

I Database contents match an assignment of a predicate logicI Query: derived predicates

Saake Database Concepts Last Edited: April 2019 7–36

Page 38: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

A General Calculus

Motivation: Mathematical Notation

{x2 | x 2 N ^ x

3 > 0 ^ x3 < 1000}

Query has the form{f (x) | p(x)}

I x denotes set of free variables

x = {x1 : D1, . . . , xn : Dn}

Saake Database Concepts Last Edited: April 2019 7–37

Page 39: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

A General Calculus /2

Function f denotes result function over x

I Important special cases: Declaration of a variable itself (f is herethe identity function) and tuple construction (result of type tuple of)

p selection predicate over free variables x

I Terms of variables, constants, and function applicationsI Predicates of data types, such as , <, >, �, ...

! atomic formulas over termsI Relation to current database ! database predicates, e.g., relation

name in the RMI predicate logical operators ^, _, ¬, 8, 9

! formulas

Saake Database Concepts Last Edited: April 2019 7–38

Page 40: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Result Declaration of a Query

x = {x1 : D1, . . . , xn : Dn}

1 Declare all assignments of free variables in x, for which thepredicate p gets true.

2 Apply function f on the values given by this assignment.

Under which circumstances do calculus queries give infinite results?

! Safety of queries

Saake Database Concepts Last Edited: April 2019 7–39

Page 41: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Relational Calculus

Domain Calculus: Variables take values of elementary data type(domains)Tuple calculus: Variables vary over tuple values (according to thelines of a relation)

Saake Database Concepts Last Edited: April 2019 7–40

Page 42: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Tuple Calculus

Basics of SFW-Queries in SQLVariables are tuple valuedExample:

{w | w 2 WINES ^ w.Color = ’Red’}

Saake Database Concepts Last Edited: April 2019 7–41

Page 43: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Tuple Calculus: Example

constructed tuple

{hw.Name,w.Vineyardi | w 2 WINES ^ w.Color = ’Red’}

Join

{he.Vineyardi | e 2 PRODUCER^w 2 WINES^e.Vineyard = w.Vineyard}

Nesting

{hw.Name,w.Vineyardi | w 2 WINES^9e 2 PRODUCER(w.Vineyard = e.Vineyard^e.Region = ’Bordeaux’)}

Saake Database Concepts Last Edited: April 2019 7–42

Page 44: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Motivation: The Language QBE

"Query by Example"Queries in QBE: Entries in table frameworksIntuition: Example entries in tablesPrecursor of several table-based query interfaces of commercialsystemsBased on logical calculus with domain variables

Saake Database Concepts Last Edited: April 2019 7–43

Page 45: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Queries in QBE: Selection and Projection

Query: "All rock-albums of the years before 2006"

Album ANr Title Year Genre Price MNrP. <2006 Rock

{t | Album(_, t, j, ’Rock’, _, _) ^ j < 2006}

Saake Database Concepts Last Edited: April 2019 7–44

Page 46: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Queries in QBE: JoinQuery: "All Rock albums of German musicians"

Album ANr Title Year Genre Price MNrP. Rock _musician

Musician MNr Name Country_musician Germany

{t | Album(_, t, _, ’Rock’, _,m)

^ Musician(m, _, ’Germany’)}Saake Database Concepts Last Edited: April 2019 7–45

Page 47: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Queries in QBE: Self-Join

Query: "Countries with two or more musicians"

Musician MNr Name Country_one P. _country¬ _one _country

{l | Musician(x, _, l) ^ Musician(y, _, l) ^ x 6= y}

Saake Database Concepts Last Edited: April 2019 7–46

Page 48: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

QBE in MS-Access

MS-Access: Database program for WindowsI Basis relations with keysI Foreign keys with graphical statement of relationsI Graphical definition of queries (SQL-similar)I Interactive definition of forms and reports

Support of QBE

Saake Database Concepts Last Edited: April 2019 7–47

Page 49: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Access: Projection and Selection

Saake Database Concepts Last Edited: April 2019 7–48

Page 50: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Access: Join

Saake Database Concepts Last Edited: April 2019 7–49

Page 51: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Domain Calculus

Terms:I Constants, such as 42 or ’MZ-4’I Variables for data types, such as x

Data type declaration usually takes place implicitly and is notdeclared explicitly!

I Function application f (t1, . . . , tn): Function f , terms ti, such asplus(12, x) resp. in infix notation 12 + x

Atomic formulas:I Predicate application ⇥(t1, . . . , tn), ⇥ 2 {<,>,,�, 6=,=, . . . }

data-type predicate, terms ti

Binary predicates are usually in infix-notation.Example: x = y, 42 > x or 3 + 7 = 11.

Saake Database Concepts Last Edited: April 2019 7–50

Page 52: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Domain Calculus /2

Atomic formulas (advanced):I Predicate application for database predicates noted as R(t1, . . . , tn)

for a relation name R

Providing: n must be the arity of the relation R and all ti must be ofthe same typeExample: PRODUCER(x, ’Hessen’, z)

Formula as usual with ^, _, ¬, 8 and 9

Saake Database Concepts Last Edited: April 2019 7–51

Page 53: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Domain Calculus /3

Query: {x1, . . . , xn | �(x1, . . . , xn)}I � is formula over the variables contained in the result list x1 to xn

I Result is set of tuplesI Tuple construction comes implicitly from the values of the variables

in the result list

Example

{x | PRODUCER(x, y, z) ^ z = ’Hessen’}

Saake Database Concepts Last Edited: April 2019 7–52

Page 54: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Basic Domain Calculus

Restriction of the domain calculus:I Range of values: IntegerI Data type predicates are restricted to equivalence and elementary

comparison operations as in relational algebraI Function applications are not allowed; besides domain variables

only constants can be used as terms

used for theoretical investigations, e.g., proves of properties

Saake Database Concepts Last Edited: April 2019 7–53

Page 55: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Safety

Safe Queries (also semantic safe queries):

Queries that give a finite result for each database state �(R)

Example for unsafe queries:

{x, y | ¬R(x, y)}

Example for safe queries:

{x, y | R(x, y)}

Saake Database Concepts Last Edited: April 2019 7–54

Page 56: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Safe Queries /2

Further example for safe queries:

{x, y | y = 10 ^ x > 0 ^ x < 10}

Safety comes directly from the rules of arithmetics.

Semantic safety is in general not decidable!

Saake Database Concepts Last Edited: April 2019 7–55

Page 57: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Syntactically Safe Queries

Syntactically Safe Queries: Queries that are subject to syntacticrestrictions to enforce semantic safety.Basic idea:

Any free variable xi must be bound to a finite domain anywhere in�(x1, . . . ) by a positive occurrence xi = t or R(. . . , xi, . . . ).

Binding to finite domains must hold for the whole condition, inparticular for all branches of an disjunction

Saake Database Concepts Last Edited: April 2019 7–56

Page 58: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Query Calculus

Safe Queries Overview

syntactic safe queries

safe queries

queries

Saake Database Concepts Last Edited: April 2019 7–57

Page 59: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Examples Domain Calculus

Query: "All vineyards of producers in Hessen."

{x | PRODUCER(x, y, z) ^ z = ’Hessen’}

Simplified notation: Each unbound variable (here y and z) in thecondition part existentially bound with 9Complete version:

{x | 9y9zPRODUCER(x, y, z) ^ z = ’Hessen’}

Saving of domain variables, by using constants as parameters ofthe predicate:

{x | PRODUCER(x, y, ’Hessen’)}

Saake Database Concepts Last Edited: April 2019 7–58

Page 60: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Examples Domain Calculus /2

Abbreviation for arbitrary, different existentially bound variables isthe _ symbol:

{x | PRODUCER(x, _, z) ^ z = ’Hessen’}

Different appearances of the symbol _ stand for pairwise differentvariables

Saake Database Concepts Last Edited: April 2019 7–59

Page 61: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Examples Domain Calculus /3

Query: "Regions with more than two vineyards."

{z | PRODUCER(x, y, z) ^ PRODUCER(x0, y0, z) ^ x 6= x

0}

Query shows a join binding on the third attribute of thePRODUCER-relationJoin binding can easily come from the use of the same domainvariables as parameter in different relation predicates

Saake Database Concepts Last Edited: April 2019 7–60

Page 62: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Examples Domain Calculus /4

Query: "From which region are which wines with the vintagebefore 1970 in the supply?"’

{y, r | WINES(x, y, z, j,w) ^ PRODUCER(w, a, r) ^ j < 1970}

Join over two relations

Saake Database Concepts Last Edited: April 2019 7–61

Page 63: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Examples Domain Calculus /5

Query: "From which regions are red wines?"’

{z | PRODUCER(x, y, z)^9a9b9c9d(WINE(a, b, c, d, x)^c = ’Red’)}

Use of a existentially bound subquerySuch subqueries could be dissolved due to the rules of predicatelogic as follows:

{z | PRODUCER(x, y, z) ^ (WINE(a, b, c, d, x) ^ c = ’Red’)}

Saake Database Concepts Last Edited: April 2019 7–62

Page 64: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Examples Domain Calculus /6

Query: "Which vineyard has only wines of the vintage after 1995in its supply?"’

{x | PRODUCER(x, y, z)^8a8b8c8d(WINE(a, b, c, d, x) =) d > 1995)}

Universally bound subformulas cannot be dissolved

Saake Database Concepts Last Edited: April 2019 7–63

Page 65: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Expressiveness of the Domain Calculus

Domain calculus is strict relational complete, i.e. to any term ⌧ of therelational algebra there is an equivalent (safe) term ⌘ of the domaincalculus.

Saake Database Concepts Last Edited: April 2019 7–64

Page 66: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Implementation of Relational OperationsGiven: Relation schema R(A1, . . . ,An) and S(B1, . . . ,Bm)

Union (for n = m)

R [ S =̂ {x1 . . . xn | R(x1, . . . , xn) _ S(x1, . . . , xn)}

Difference (for n = m)

R � S =̂ {x1 . . . xn | R(x1, . . . , xn) ^ ¬S(x1, . . . , xn)}

Natural Join

R on S =̂ {x1 . . . xnxn+1 . . . xn+m�i | R(x1, . . . , xn) ^S(x1, . . . , xi, xn+1, . . . , xn+m�i)}

Assumption: the first i attributes of R and S are the join attributes,thus Aj = Bj for j = 1 . . . i

Saake Database Concepts Last Edited: April 2019 7–65

Page 67: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Implementation of Relational Operations /2

Projection

⇡A(R) =̂ {y1 . . . yk | 9x1 . . . 9xn(R(x1, . . . , xn) ^ y1 = xi1

^ · · · ^ yk = xik)}

Attribute list of the projection: A = (Ai1 , . . . ,Aik)

Selection��(R) =̂ {x1 . . . xn | R(x1, . . . , xn) ^ �0}

�0 comes from �, by insertion of the variable xi at the place of theattribute names Ai

Saake Database Concepts Last Edited: April 2019 7–66

Page 68: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Summary

Formal models for queries in database systemsRelational algebra

I Operational approachI Queries as nesting of operations on relations

Query calculusI Logic-based approachI Queries as derived predicatesI see Book: sections 4.2.3, 4.2.4 and 9.3

Saake Database Concepts Last Edited: April 2019 7–67

Page 69: Part VII Query Basics: Algebra & Calculus

Query Basics: Algebra & Calculus Examples for the Domain Calculus

Control Questions

Which meaning do equivalence,independence and completeness have inthe relational algebra?How can the semantic of the extendedSQL-operations be expressed in relationalalgebra?What is the difference between relationalalgebra and relational query calculus?What is the role of safety in queries?

Saake Database Concepts Last Edited: April 2019 7–68