Relational Languages: Algebra, Tuple Calculus, Domain Calculus, and Datalog, Summaries of Algebra

express a database query in Relational Algebra, involving the basic operators (selection, projection, cross product, renaming, set union, intersection,.

Typology: Summaries

2021/2022

Uploaded on 09/07/2022

nabeel_kk
nabeel_kk ๐Ÿ‡ธ๐Ÿ‡ฆ

4.6

(65)

1.3K documents

1 / 48

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Unit 5 1
Laks V.S. Lakshmanan,; Based partly on Ramakrishnan & Gehrke, DB Management Systems
Unit 5
Formal Relational Languages
Text: Chapters 4 & 24
Relational Algebra (Ch. 4: 4.1โ€”4.2)
Tuple Relational Calculus X
Domain Relational Calculus X
Datalog (Ch. 24)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30

Partial preview of the text

Download Relational Languages: Algebra, Tuple Calculus, Domain Calculus, and Datalog and more Summaries Algebra in PDF only on Docsity!

Unit 5 Laks V.S. Lakshmanan,;^ Based^ partly on^ Ramakrishnan^ &^ Gehrke, DB Management Systems 1

Unit 5

Formal Relational Languages

Text: Chapters 4 & 24

Relational Algebra (Ch. 4: 4.1โ€”4.2)

Tuple Relational Calculus X

Domain Relational Calculus X

Datalog (Ch. 24)

Learning Goals

Given a database (a set of tables ) you will be

able to

๏ฎ express a database query in Relational Algebra, involving the basic operators (selection, projection, cross product, renaming, set union, intersection, difference ), join, division and assignment

๏ฎ rewrite RA expressions (queries) using a subset of the operators with expressions using another subset

๏ฎ show that two RA queries are/arenโ€™t equivalent

๏ฎ express a DB query in Datalog

๏ฎ translate RA queries to Datalog; translate queries from a fragment of Datalog to RA.

Relational Algebra (RA)

๏ฎ Procedural language

๏ฎ Basic operations:

๏ƒ˜ Selection - Selects a subset of rows from relation. ๏ƒ˜ Projection - Deletes unwanted columns from relation. ๏ƒ˜ Cross-product - Allows us to combine two relations. ๏ƒ˜ Set-difference - Tuples in reln. 1, but not in reln. 2. ๏ƒ˜ Union - Tuples in reln. 1 and tuples in reln. 2. ๏ƒ˜ Rename โ€“ Assigns a(nother) name to a relation

๏ฎ Additional operations:

๏ƒ˜ intersection, join, division, assignment: not essential, but very useful

๏ฎ The operators take one or two relations as inputs and give a new relation as a result.

๏ฎ Operations can be composed. (Algebra is โ€œclosedโ€.)

Datalog (Lite)

๏ฎ Will see Datalog (full) later.

๏ฎ Rule-based: head ๏ƒŸ body.

๏ฎ head is of the form ๐‘ ๐‘‹ 1 , โ€ฆ , ๐‘‹๐‘›. Here, ๐‘‹๐‘– are

variables. Think of ๐‘(โ€ฆ ) as a โ€œcollectorโ€ of

answers to the query expressed by the body.

๏ฎ body is a conjunction (i.e., AND) of atoms of

two kinds:

๏ƒ˜ ๐‘Ÿ(๐‘Œ 1 , โ€ฆ , ๐‘Œ๐‘˜) where ๐‘Ÿ is a database relation. ๏ƒ˜ ๐‘‹ ๐‘œ๐‘ ๐‘ or ๐‘‹ ๐‘œ๐‘ ๐‘Œ where ๐‘‹, ๐‘Œ are variables and ๐‘ is a constant.

๏ฎ Variables are bound to values appearing in the

DB when query is evaluated.

๏ฎ Constants can be numerical, string, etc.

Selection

๏ฎ Notation: ๏ณ p(r)

๏ฎ p is called the selection predicate

Set of tuples of r that satisfy p

๏ฎ Defined as:

๏ณ p ( r ) = { t | t ๏ƒŽ r and p(t) }

Where p is a formula in propositional calculus consisting of predicates connectives : ๏ƒ™ ( and ), ๏ƒš ( or ), ๏ƒ˜ ( not ) A predicate is one of: op or op where op is one of: =, ๏‚น, >, ๏‚ณ, <, ๏‚ฃ. ๏ถ Result schema is same as rโ€™s schema

Selection Example 1

  • Relation r A B C D

๏ก ๏ก ๏ข ๏ข

๏ก ๏ข ๏ข ๏ข

1 5 12 23

7 7 3 10

โ€ข ๏ณA=B ^ D > 5 (r)

A B C D

๏ก ๏ข

๏ก ๏ข

1 23

7 10

RA (^) Datalog

attribute

variable

constant

Selection Example 3

๏ฎ Find customers in Cust with rating less than 9

and salary more than 80.

cid cname rating salary 21 Y. Yuppy 5 95 50 B. Rusty 10 65 55 S. Sneezy 8 70

Cust

Projection

๏ฎ Notation:

where ๐ด 1 , โ€ฆ , ๐ด๐‘˜ are attributes (the projection

list) and ๐‘Ÿ is a relation.

๏ฎ The result = relation over the k attributes

๐ด 1 , ๐ด 2 , โ€ฆ , ๐ด๐‘˜ obtained from ๐‘Ÿ by erasing the

columns that are not listed and eliminating

duplicate rows.

๏ฎ Remember: relations are sets!

Projection Example 2

cname rating R. Rudy 9 G. Grumpy 8 S. Sneezy 5 R. Rusty 10

( )

, Cust cname rating

salary 95 55

( Cust )

salary

( ))?

7 ( , Cust cname rating rating ๏€พ

cid cname rating salary 38 R. Rudy 9 95 32 G. Grumpy 8 55 51 S. Sneezy 5 95 78 R. Rusty 10 55 Cust

๐‘Ž๐‘›๐‘  ๐ถ, ๐‘… โ† ๐‘๐‘ข๐‘ ๐‘ก ๐ผ, ๐ถ, ๐‘…, ๐‘† , ๐‘… > 7.^1

Union, Intersection, Set-

Difference

๏ฎ Notation: r ๏ƒˆ s r ๏ƒ‡ s r โ€“ s

๏ฎ Defined as:

r ๏ƒˆ s = {t | t ๏ƒŽ r or t ๏ƒŽ s}

r ๏ƒ‡ s ={ t | t ๏ƒŽ r and t ๏ƒŽ s }

r โ€“ s = {t | t ๏ƒŽ r and t ๏ƒ s}

๏ฎ For these operations to be well-defined:

1. r, s must have the same arity (same number of

attributes)

2. The attribute domains must be compatible

(e.g., 2nd column of r has same domain of values

as the 2nd column of s)

๏ฎ What is the schema of the result?

Unit 5 16

Union,Int., Diff. Examples

cid cname rating salary

22 J. Justin 7 65 31 R. Rubber 8 85 58 N. Nusty 10 85 44 G. Guppy 5 70 28 Y. Yuppy 9 95

C 1 ๏ƒˆ C 2

cid cname rating salary 31 R.Rubber 8 85 58 N. Nusty 10 85

C 1 ๏ƒ‡ C 2

cid cname rating salary 58 J. Justin 7 65

C 1 ๏€ญ C 2

cid cname rating salary

22 J. Justin 7 65 31 R. Rubber 8 85 58 N. Nusty 10 85

cid cname rating salary 28 Y. Yuppy 9 95 31 R. Rubber 8 85 44 G. Guppy 5 70 58 N. Nusty 10 85

C 1 C 2

How do you write these queries in Datalog?

Cartesian (or Cross)-Product

๏ฎ Notation: r x s

๏ฎ Defined as:

r x s = { t q | t ๏ƒŽ r and q ๏ƒŽ s}

๏ฎ Assume that attributes of r(R) and s(S) are

disjoint.

(That is, R ๏ƒ‡ S = ๏ƒ†).

๏ฎ If r and s have common attributes, they must

be renamed in the result.

Unit 5 19

Cartesian-Product Example 2

cid cname rating salary 22 J. Justin 7 65 31 R. Rubber 8 85 58 N. Nusty 10 85

cid iid day qty

22 101 10/10/0 6 10 58 103 11/12/0 6 5

Customer (^) Order

conflicting Customer x Order names

Customer .cid

sname rating salary Order. cid

iid day qty

22 J. Justin 7 65 22 101 10/10/96 10 22 J. Justin 7 65 58 103 11/12/96 5 31 R. Rubber 8 85 22 101 10/10/96 10 31 R. Rubber 8 85 58 103 11/12/96 5 58 N. Nusty 10 85 22 101 10/10/96 10 58 N. Nusty 10 85 58 103 11/12/96 (^52)

Attribute names present no issues for Datalog.

Unit 5 20

Rename

๏ฎ Allows us to name results of relational-algebra expressions. ๏ฎ Allows us to assign more names to a relation. ๏ฎ Allows us to rename attributes of a relation. ๏ฎ Notation

๏ฒ x (E)

returns the expression E under the name X

๏ฎ If E has arity n, then

๏ฒ x ( A1, A2, โ€ฆ, An) (E)

returns the result of expression E under the name X,

and with the attributes renamed to A 1 , A2, โ€ฆ., An.

๏ฎ ๏ฒ x (B1โ†’ A1, โ€ฆ, Bkโ†’ Ak) (E)

is as before, but it only renames attributes B1,โ€ฆ,Bk of E to A1,โ€ฆ,Ak.