35
Dr. Alexandra I. Cristea http://www.dcs.warwick.ac.uk/ ~acristea/ CS 319: Theory of Databases: C6

CS 319: Theory of Databases: C6

  • Upload
    duke

  • View
    34

  • Download
    2

Embed Size (px)

DESCRIPTION

CS 319: Theory of Databases: C6. Dr. Alexandra I. Cristea http://www.dcs.warwick.ac.uk/~acristea/. … previous RA . Content. Generalities DB Integrity constraints (FD revisited) Relational Algebra (revisited) Query optimisation Tuple calculus Domain calculus Query equivalence - PowerPoint PPT Presentation

Citation preview

Page 1: CS 319: Theory of Databases: C6

Dr. Alexandra I. Cristeahttp://www.dcs.warwick.ac.uk/~acristea/

CS 319: Theory of Databases: C6

Page 2: CS 319: Theory of Databases: C6

2

… previous

RA

Page 3: CS 319: Theory of Databases: C6

3

Content1. Generalities DB2. Integrity constraints (FD revisited)3. Relational Algebra (revisited)4. Query optimisation5. Tuple calculus6. Domain calculus7. Query equivalence8. Temporal Data9. No Nulls10. The Askew Wall

Page 4: CS 319: Theory of Databases: C6

4

Tuple Calculus• non-procedural query language• A query in TC is expressed as: {t|P(t)}where:t – set of tuples (also called free or range variable)P – predicate that is true for t (also called formula)Also: t[A] – value of tuple t on attribute A tr – means the tuple t is in relation r

Page 5: CS 319: Theory of Databases: C6

5

Example TC language• QUEL (used by INGRES DBMS)• SQL (to some extent)

Page 6: CS 319: Theory of Databases: C6

6

Examples TC1. {t | t loan ^ t[amount] > 1200}2. {t | s loan (t[loan-number] = s[loan-

number] ^ s[amount] > 1200)}

Page 7: CS 319: Theory of Databases: C6

7

Satefy of expressions: Tuple Calculus (& DC)

• A TC expression {t | P(t)} may give an infinite relation, e.g.,

{ t | (t loan) }• Define dom(P) as the set of all values

referenced by P (e.g. literals or relations)• A TC expression {t | P(t)} is safe when all

values appearing in the result are values from dom(P).

Page 8: CS 319: Theory of Databases: C6

8

Queries in TC & DCTranslate the following questions (for the bank

database) to the tuple & domain calculus:1. Give the name of customers that have a loan

with a branch where they also have an account.2. Give the name of customers who have a loan at

a branch where they do not have an account.3. Give the name of customers who have a loan at

every branch where they have an account.4. Give the name of customers who have loans

only at branches where they have an account.

Page 9: CS 319: Theory of Databases: C6

9

1. Give the name of customers that have a loan with a branch where they also have an account.

TC: {t| bborrower(t[c_n]=b[c_n] ^ lloan(l[l_n]=b[l_n] ^ aaccount(a[b_n]=l[b_n] ^

ddepositor(a[a_n]=d[a_n] ^ d[c_n]=b[c_n] ))))}

Page 10: CS 319: Theory of Databases: C6

10

2. Give the name of customers who have a loan at a branch where they do not have an account.

TC: {t| bborrower(t[c_n]=b[c_n] ^ lloan(l[l_n]=b[l_n] ^ aaccount(a[b_n]=l[b_n] ^

ddepositor(a[a_n]=d[a_n] ^ d[c_n]=b[c_n] ))))}

Page 11: CS 319: Theory of Databases: C6

11

3. Give the name of customers who have a loan at every branch where they have an account.

TC: {t| ddepositor(t[c_n]=d[c_n] ^ d2depositor((d2[c_n]=d[c_n] ^

aaccount(a[a_n]=d2[a_n])) => ( lloan(l[b_n]=a[b_n] ^ bborrow(b[l_n]=l[l_n] ^ b[c_n]=d2[c_n])))))}

This is necessary for the safety of the expression, to reduce the search space

Page 12: CS 319: Theory of Databases: C6

12

4. Give the name of customers who have loans only at branches where they have an account.

TC: {t| dborrow(t[c_n]=d[c_n] ^ d2borrow((d2[c_n]=d[c_n] ^ aloan(a[l_n]=d2[l_n]) => ( laccount(l[b_n]=a[b_n] ^

bdepositor(b[a_n]=l[a_n] ^ b[c_n]=d2[c_n]))))))}

Page 13: CS 319: Theory of Databases: C6

13

4. Give the name of customers who have loans only at branches where they have an account.

TC: {t| bborrow(t[c_n]=b[c_n] ^ b2borrow((b2[c_n]=b[c_n] ^ lloan(l[l_n]=b2[l_n])) => ( aaccount(a[b_n]=l[b_n] ^

ddepositor(d[a_n]=a[a_n] ^ d[c_n]=n2[c_n])))))}

With more intuitive attrs naming.

Page 14: CS 319: Theory of Databases: C6

14

Library TC/DC questions1. Give the name and department of borrowers,

and the ISBN-number of the books of which they were the first borrower.

2. Give the name and department of borrowers who have never borrowed the most recently purchased copy of a book.

3. Give the title of books that are “present” in the library, reserved by someone, but not yet borrowed (by anyone).

Page 15: CS 319: Theory of Databases: C6

15

1. Give the name and department of borrowers, and the ISBN-number of the books of which they were the first borrower.TC: {t | bborrow( t[name]=b[name] ^

t[department]=b[department] ^ ccopy(b[barcode]=c[barcode] ^ t[ISBN]=c[ISBN] ^ c2copy(c2[ISBN]=c[ISBN] ^

b2borrow(b2[barcode]=c2[barcode] ^ b2[from]<b[from])))) }

Page 16: CS 319: Theory of Databases: C6

16

2. Give the name and department of borrowers who have never borrowed the most recently purchased copy of a book.

TC {t | bborrow( t[name]=b[name] ^ t[department]=b[department] ^ b2borrow((b2[name]=b[name] ^ b2[department]=b[department] ^ c2copy (c2[barcode]=b2[barcode])) =>

(c3copy(c3[ISBN]=c2[ISBN] ^ c3[cpYear]>c2[cpYear] ^ b2[from] c3[cpYear])))}

Page 17: CS 319: Theory of Databases: C6

17

3. Give the title of books that are “present” in the library, reserved by someone, but not yet borrowed (by anyone).TC: {t| bbook(t[title]=b[title] ^ rreservation(r[ISBN]=b[ISBN] ^ ccopy(c[ISBN]=b[ISBN] ^ c[present] = “true” ^ c2copy(c2[ISBN]=c[ISBN] ^

bborrow(b[barcode]=c2[barcode])))))}

Page 18: CS 319: Theory of Databases: C6

18

Beer Database• visits(drinker, bar)• serves(bar, beer)• likes(drinker, beer).

Page 19: CS 319: Theory of Databases: C6

19

Beer questions with a difference1. Give all drinkers that visit bars that don’t serve any

beer they like 2. Give all drinkers that only visit bars that serve a

beer they like3. Give all drinkers that only visit bars that serve no

beer they like4. Give all drinkers that only visit bars that serve all

beers they like (and maybe other beers as well)5. Give all drinkers that only visit bars that only serve

beers they like (and thus serve nothing else)

Page 20: CS 319: Theory of Databases: C6

20

Content1. Generalities DB2. Integrity constraints (FD revisited)3. Relational Algebra (revisited)4. Query optimisation5. Tuple calculus6. Domain calculus7. Query equivalence8. Temporal Data9. No Nulls10. The Askew Wall

Page 21: CS 319: Theory of Databases: C6

21

Domain calculus• Uses domain variables to take values from an

attributes domain, instead of tuple values• is related to QBE language• An expression in DC is of the form:{<x1,x2,…,xn> | P(x1,x2,…,xn)}where: x1,…xn are domain variablesP is a formula

Page 22: CS 319: Theory of Databases: C6

22

Example DC language• QBE• Others: ILL, FQL, DEDUCE

Page 23: CS 319: Theory of Databases: C6

23

Example DC1. {<l,b,a> | <l,b,a> loan ^ a > 1200}2. {<l> | b,a (<l, b, a>loan ^ a > 1200)}

Page 24: CS 319: Theory of Databases: C6

24

Queries in TC & DCTranslate the following questions (for the bank

database) to the tuple & domain calculus:1. Give the name of customers that have a loan

with a branch where they also have an account.2. Give the name of customers who have a loan at

a branch where they do not have an account.3. Give the name of customers who have a loan at

every branch where they have an account.4. Give the name of customers who have loans

only at branches where they have an account.

Page 25: CS 319: Theory of Databases: C6

25

1. Give the name of customers that have a loan with a branch where they also have an account.

TC: {t| bborrower(t[c_n]=b[c_n] ^ lloan(l[l_n]=b[l_n] ^ aaccount(a[b_n]=l[b_n] ^

ddepositor(a[a_n]=d[a_n] ^ d[c_n]=b[c_n] ))))}

DC: {<cn>| ln (<cn,ln>borrower ^ bn,a(<ln,bn,a>loan ^

an,b(<an,bn,b>account ^ <cn,an>depositor)))}

Page 26: CS 319: Theory of Databases: C6

26

2. Give the name of customers who have a loan at a branch where they do not have an account.

TC: {t| bborrower(t[c_n]=b[c_n] ^ lloan(l[l_n]=b[l_n] ^ aaccount(a[b_n]=l[b_n] ^

ddepositor(a[a_n]=d[a_n] ^ d[c_n]=b[c_n] ))))}

DC: {<cn>| ln (<cn,ln>borrower ^ bn,a(<ln,bn,a>loan ^

an,b(<an,bn,b>account ^ <cn,an>depositor)))}

Page 27: CS 319: Theory of Databases: C6

27

3. Give the name of customers who have a loan at every branch where they have an account.

TC: {t| ddepositor(t[c_n]=d[c_n] ^ d2depositor((d2[c_n]=d[c_n] ^

aaccount(a[a_n]=d2[a_n]) => ( lloan(l[b_n]=a[b_n] ^ bborrow(b[l_n]=l[l_n] ^ b[c_n]=d2[c_n]))))))}

DC: {<cn>| an(<cn,an>depositor ^ an2 (( <cn,an2>depositor ^ bn,b(<an2,bn,b>account) => (ln,a(<ln,bn,a>loan ^ <cn,ln>borrow)))))}

This is necessary for the safety of the expression, to reduce the search space

Page 28: CS 319: Theory of Databases: C6

28

4. Give the name of customers who have loans only at branches where they have an account.

TC: {t| dborrow(t[c_n]=d[c_n] ^ d2borrow((d2[c_n]=d[c_n] ^ aloan(a[l_n]=d2[l_n]) => ( laccount(l[b_n]=a[b_n] ^

bdepositor(b[a_n]=l[a_n] ^ b[c_n]=d2[c_n]))))))}

DC: {<cn>| an(<cn,an>borrow ^ an2 (( <cn,an2>borrow ^ bn,b(<an2,bn,b>loan) => (ln,a(<ln,bn,a>account ^ <cn,ln>depositor)))))}

Page 29: CS 319: Theory of Databases: C6

29

4. Give the name of customers who have loans only at branches where they have an account.

TC: {t| bborrow(t[c_n]=b[c_n] ^ b2borrow((b2[c_n]=b[c_n] ^ lloan(l[l_n]=b2[l_n]) => ( aaccount(a[b_n]=l[b_n] ^

ddepositor(d[a_n]=a[a_n] ^ d[c_n]=n2[c_n]))))))}

DC: {<cn>| ln(<cn,ln>borrow ^ ln2 (( <cn,ln2>borrow ^ bn,a(<ln2,bn,a>loan) => (an,b(<an,bn,b>account ^ <cn,an>depositor)))))}

With more intuitive attrs naming.

Page 30: CS 319: Theory of Databases: C6

30

Library TC/DC questions1. Give the name and department of borrowers,

and the ISBN-number of the books of which they were the first borrower.

2. Give the name and department of borrowers who have never borrowed the most recently purchased copy of a book.

3. Give the title of books that are “present” in the library, reserved by someone, but not yet borrowed (by anyone).

Page 31: CS 319: Theory of Databases: C6

31

1. Give the name and department of borrowers, and the ISBN-number of the books of which they were the first borrower.TC: {t | bborrow( t[name]=b[name] ^

t[department]=b[department] ^ ccopy(b[barcode]=c[barcode] ^ t[ISBN]=c[ISBN] ^ c2copy(c2[ISBN]=c[ISBN] ^

b2borrow(b2[barcode]=c2[barcode] ^ b2[from]<b[from])))) }

DC: {<n,d,ISBN> | b,f,t( <n,b,d,f,t>borrow ^ Y,p,d1(<b,ISBN,d1,Y,p>copy ^ b2,d2,Y2,p2(<b2,ISBN,d2,Y2,p2>copy ^

n2,f2,t2,d3(<n2,b2,d3,f2,t2>borrow ^ f2<f)))) }

Page 32: CS 319: Theory of Databases: C6

32

2. Give the name and department of borrowers who have never borrowed the most recently purchased copy of a book.

TC {t | bborrow( t[name]=b[name] ^ t[department]=b[department] ^ b2borrow((b2[name]=b[name] ^ b2[department]=b[department] ^ c2copy (c2[barcode]=b2[barcode])) =>

(c3copy(c3[ISBN]=c2[ISBN] ^ c3[cpYear]>c2[cpYear] ^ b2[from] c3[cpYear])))}

DC {<n, d> | b,f,t (<n,b,d,f,t>borrow ^ b2,f2,t2((<n,b2,d,f2,t2>borrow ^ ISBN,d2,Y2,p2(<b2,ISBN,d2,Y2,p2>copy]) =>

(b3,d3,Y3,p3(<b3,ISBN,d3,Y3,p3>copy ^ Y3>Y2 ^ f2 Y3)))))}

Page 33: CS 319: Theory of Databases: C6

33

3. Give the title of books that are “present” in the library, reserved by someone, but not yet borrowed (by anyone).TC: {t| bbook(t[title]=b[title] ^ rreservation(r[ISBN]=b[ISBN] ^ ccopy(c[ISBN]=b[ISBN] ^ c[present] = “true” ^ c2copy(c2[ISBN]=c[ISBN] ^

bborrow(b[barcode]=c2[barcode])))))}DC: {<title>| ISBN,p,y(<ISBN,title,p,y>book ^

n,d,date,c(<n,d,ISBN,date,c>reservation ^ b2,d2,Y2,p2(<b2,ISBN,d2,Y2,p2>copy ^ p2 = “true”

^ b3,d3,Y3,p3(<b3,ISBN,d3,Y3,p3>copy ^ n4,d4,f4,t4(<n4,b3,d4,f4,t4>borrow)))))}

Page 34: CS 319: Theory of Databases: C6

34

Summary• We have learned TC & DC• We have learned to perform simple and

more complex queries in TC & DC

Page 35: CS 319: Theory of Databases: C6

35

… to follow

Query equivalence