©silberschatz, korth and sudarshan3.1database system concepts calculus objectives tuple calculus ...

47
©Silberschatz, Korth and Sudarsha 3.1 Database System Concepts Calculus Calculus Objectives Tuple Calculus Domain Calculus QBE & SQL Related Examples

Post on 22-Dec-2015

240 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.1Database System Concepts

CalculusCalculus

Objectives Tuple Calculus

Domain Calculus

QBE & SQL

Related Examples

Page 2: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.2Database System Concepts

Tuple Relational CalculusTuple Relational Calculus

A nonprocedural query language, where each query is of the form

{t | P (t) }

It is the set of all tuples t such that predicate P is true for t

t is a tuple variable, t[A] denotes the value of tuple t on attribute A

t r denotes that tuple t is in relation r

P is a formula similar to that of the predicate calculus

Page 3: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.3Database System Concepts

Predicate Calculus FormulaPredicate Calculus Formula

1. Set of attributes and constants

2. Set of comparison operators: (e.g., , , , , , )

3. Set of connectives: and (), or (v)‚ not ()

4. Implication (): x y, if x is true, then y is true

x y x v y

5. Set of quantifiers: t r (Q(t)) ”there exists” a tuple in t in relation r

such that predicate Q(t) is true

t r (Q(t)) Q is true “for all” tuples t in relation r

Page 4: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.4Database System Concepts

Banking ExampleBanking Example

branch (branch-name, branch-city, assets)

customer (customer-name, customer-street, customer-city)

account (account-number, branch-name, balance)

loan (loan-number, branch-name, amount)

depositor (customer-name, account-number)

borrower (customer-name, loan-number)

Page 5: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.5Database System Concepts

Example QueriesExample Queries

Find the loan-number, branch-name, and amount for loans of over $1200

Find the loan number for each loan of an amount greater than $1200

Notice that a relation on schema [loan-number] is implicitly defined by the query

R={t | s loan (t[loan-number] = s[loan-number]

s [amount] 1200)}

R={t | t loan t [amount] 1200}

Page 6: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.6Database System Concepts

Example QueriesExample Queries

Find the names of all customers having a loan, an account, or both at the bank

R={t | s borrower( t[customer-name] = s[customer-name]) u depositor( t[customer-name] = u[customer-name])}

Find the names of all customers who have a loan and an account

at the bank

R={t | s borrower( t[customer-name] = s[customer-name]) u depositor( t[customer-name] = u[customer-name])}

Page 7: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.7Database System Concepts

Example QueriesExample Queries

Find the names of all customers having a loan at the Perryridge branch

T={t | s borrower( t[customer-name] = s[customer-name] u loan(u[branch-name] = “Perryridge” u[loan-number] = s[loan-number])) not v depositor (v[customer-name] = t[customer-name]) }

Find the names of all customers who have a loan at the Perryridge branch, but no account at any branch of the bank

T={t | s borrower(t[customer-name] = s[customer-name] u loan(u[branch-name] = “Perryridge” u[loan-number] = s[loan-number]))}

Page 8: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.8Database System Concepts

Example QueriesExample Queries

Find the names of all customers having a loan from the Perryridge branch, and the cities they live in

T={t | s loan(s[branch-name] = “Perryridge” u borrower (u[loan-number] = s[loan-number]

t [customer-name] = u[customer-name]) v customer (u[customer-name] = v[customer-name]

t[customer-city] = v[customer-city]))}

Alternate

T={t | s borrower( t[customer-name]=s[customer-name]

u loan(u[branch-name]= “Perryridge”

s[loan-number]=u[loan-number])

y customer(y[customer-name]=s[customer-name]

t[customer-city]=y[customer-city]))}

Page 9: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.9Database System Concepts

Example QueriesExample Queries

Find the names of all customers who have an account at all branches located in Brooklyn (Example of Division):

R={t | c customer (t[customer-name] = c[customer-name])

s branch(s[branch-city] = “Brooklyn” u account ( s[branch-name] = u[branch-name] d depositor ( t[customer-name] = d[customer-name] d [account-number] = u[account-number] )) )}

Page 10: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.10Database System Concepts

Safety of ExpressionsSafety of Expressions

It is possible to write tuple calculus expressions that generate infinite relations.

For example, {t | t r} results in an infinite relation if the domain of any attribute of relation r is infinite

To guard against the problem, we restrict the set of allowable expressions to safe expressions.

An expression {t | P(t)} in the tuple relational calculus is safe if every component of t appears in one of the relations, tuples, or constants that appear in P NOTE: this is more than just a syntax condition.

E.g. { t | t[A]=5 true } is not safe --- it defines an infinite set with attribute values that do not appear in any relation or tuples or constants in P.

Page 11: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.11Database System Concepts

Domain Relational CalculusDomain Relational Calculus

A nonprocedural query language equivalent in power to the tuple relational calculus

Each query is an expression of the form:

{ x1, x2, …, xn | P(x1, x2, …, xn)}

x1, x2, …, xn represent domain variables

P represents a formula similar to that of the predicate calculus

Page 12: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.12Database System Concepts

Example QueriesExample Queries

Find the loan-number, branch-name, and amount for loans of over $1200

T={ c, a | l ( c, l borrower b( l, b, a loan

b = “Perryridge”))}

Or T={ c, a | l, b ( c, l borrower l, b, a loan

b = “Perryridge”)}

Or T= { c, a | l ( c, l borrower l, “Perryridge”, a loan)}

Find the names of all customers who have a loan from the Perryridge branch and the loan amount:

R={ c | l, b, a ( c, l borrower l, b, a loan a > 1200)}

Find the names of all customers who have a loan of over $1200

S={ l, b, a | l, b, a loan a > 1200}

Page 13: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.13Database System Concepts

Example QueriesExample Queries

Find the names of all customers having a loan, an account, or both at the Perryridge branch:

R={ c | s, n ( c, s, n customer)

x,y,z( x, y, z branch y = “Brooklyn”) a,b( a, x, b account c,a depositor)}

Find the names of all customers who have an account at all branches located in Brooklyn:

R={ c | l, b,a ( c, l borrower l, b, a loan b = “Perryridge”) a,b,n( c, a depositor a, b, n account b = “Perryridge”)}

Page 14: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.14Database System Concepts

Safety of ExpressionsSafety of Expressions

{ x1, x2, …, xn | P(x1, x2, …, xn)}

is safe if all of the following hold:

1. All values that appear in tuples of the expression are values from dom(P) (that is, the values appear either in P or in a tuple of a relation mentioned in P).

2. For every “there exists” subformula of the form x (P1(x)), the subformula is true if and only if there is a value of x in dom(P1) such that P1(x) is true.

3. For every “for all” subformula of the form x (P1 (x)), the

subformula is true if and only if P1(x) is true for all values x from dom (P1).

Page 15: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.15Database System Concepts

QBE & SQLQBE & SQL QBE (Query By Examples)

Visual method for building queries

SQL Command line method for

building queries

Exercise: Get a list of customer names who are Borrowers in customer city ‘Dhahran’

Page 16: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.16Database System Concepts

Result of Result of branch-name = branch-name = “Perryridge”“Perryridge” ( (loanloan))

Page 17: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.17Database System Concepts

Loan Number and the Amount of the LoanLoan Number and the Amount of the Loan

Page 18: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.18Database System Concepts

Names of All Customers Who Have Names of All Customers Who Have Either a Loan or an AccountEither a Loan or an Account

Page 19: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.19Database System Concepts

Customers With An Account But No LoanCustomers With An Account But No Loan

Page 20: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.20Database System Concepts

Result of Result of borrower borrower loanloan

Page 21: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.21Database System Concepts

Result of Result of branch-name = branch-name = “Perryridge” “Perryridge” ((borrower borrower loan) loan)

Page 22: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.22Database System Concepts

Result of Result of customer-namecustomer-name

Page 23: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.23Database System Concepts

Result of the SubexpressionResult of the Subexpression

Page 24: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.24Database System Concepts

Largest Account Balance in the BankLargest Account Balance in the Bank

Page 25: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.25Database System Concepts

Customers Who Live on the Same Street and In the Customers Who Live on the Same Street and In the Same City as SmithSame City as Smith

Page 26: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.26Database System Concepts

Customers With Both an Account and a Loan Customers With Both an Account and a Loan at the Bankat the Bank

Page 27: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.27Database System Concepts

Result of Result of customer-name, loan-number, amountcustomer-name, loan-number, amount ((borrower borrower

loan)loan)

Page 28: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.28Database System Concepts

Result of Result of branch-namebranch-name((customer-city = customer-city =

“Harrison”“Harrison”((customercustomer account depositor))account depositor))

Page 29: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.29Database System Concepts

Result of Result of branch-namebranch-name((branch-city = branch-city = “Brooklyn”“Brooklyn”(branch))(branch))

Page 30: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.30Database System Concepts

Result of Result of customer-name, branch-namecustomer-name, branch-name((depositor account)depositor account)

Page 31: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.31Database System Concepts

The The credit-infocredit-info Relation Relation

Page 32: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.32Database System Concepts

Result of Result of customer-name, (limit – credit-balance) customer-name, (limit – credit-balance) as as credit-credit-

availableavailable(credit-info).(credit-info).

Page 33: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.33Database System Concepts

The The pt-works pt-works RelationRelation

Page 34: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.34Database System Concepts

The The pt-works pt-works Relation After GroupingRelation After Grouping

Page 35: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.35Database System Concepts

Result of Result of branch-name branch-name sumsum(salary) (salary) (pt-works)(pt-works)

Page 36: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.36Database System Concepts

Result of Result of branch-name branch-name sumsum salary, salary, max(max(salarysalary) as ) as max-max-

salary salary (pt-works)(pt-works)

Page 37: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.37Database System Concepts

The The employeeemployee and and ft-works ft-works Relations Relations

Page 38: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.38Database System Concepts

The Result of The Result of employee ft-worksemployee ft-works

Page 39: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.39Database System Concepts

The Result of The Result of employeeemployee ft-worksft-works

Page 40: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.40Database System Concepts

Result of Result of employee ft-works employee ft-works

Page 41: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.41Database System Concepts

Result of Result of employee ft-worksemployee ft-works

Page 42: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.42Database System Concepts

Tuples Inserted Into Tuples Inserted Into loan loan and and borrowerborrower

Page 43: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.43Database System Concepts

Names of All Customers Who Have a Names of All Customers Who Have a Loan at the Perryridge BranchLoan at the Perryridge Branch

Page 44: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.44Database System Concepts

E-R DiagramE-R Diagram

Page 45: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.45Database System Concepts

The The branchbranch Relation Relation

Page 46: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.46Database System Concepts

The The loan loan RelationRelation

Page 47: ©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives  Tuple Calculus  Domain Calculus  QBE & SQL  Related Examples

©Silberschatz, Korth and Sudarshan3.47Database System Concepts

The The borrowerborrower Relation Relation