©Silberschatz, Korth and Sudarshan3.1Database System Concepts Calculus Objectives Tuple Calculus ...

Post on 22-Dec-2015

233 views 2 download

Tags:

transcript

©Silberschatz, Korth and Sudarshan3.1Database System Concepts

CalculusCalculus

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

©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

©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)

©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}

©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])}

©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]))}

©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]))}

©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] )) )}

©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.

©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

©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}

©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”)}

©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).

©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’

©Silberschatz, Korth and Sudarshan3.16Database System Concepts

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

©Silberschatz, Korth and Sudarshan3.17Database System Concepts

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

©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

©Silberschatz, Korth and Sudarshan3.19Database System Concepts

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

©Silberschatz, Korth and Sudarshan3.20Database System Concepts

Result of Result of borrower borrower loanloan

©Silberschatz, Korth and Sudarshan3.21Database System Concepts

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

©Silberschatz, Korth and Sudarshan3.22Database System Concepts

Result of Result of customer-namecustomer-name

©Silberschatz, Korth and Sudarshan3.23Database System Concepts

Result of the SubexpressionResult of the Subexpression

©Silberschatz, Korth and Sudarshan3.24Database System Concepts

Largest Account Balance in the BankLargest Account Balance in the Bank

©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

©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

©Silberschatz, Korth and Sudarshan3.27Database System Concepts

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

loan)loan)

©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))

©Silberschatz, Korth and Sudarshan3.29Database System Concepts

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

©Silberschatz, Korth and Sudarshan3.30Database System Concepts

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

©Silberschatz, Korth and Sudarshan3.31Database System Concepts

The The credit-infocredit-info Relation Relation

©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).

©Silberschatz, Korth and Sudarshan3.33Database System Concepts

The The pt-works pt-works RelationRelation

©Silberschatz, Korth and Sudarshan3.34Database System Concepts

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

©Silberschatz, Korth and Sudarshan3.35Database System Concepts

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

©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)

©Silberschatz, Korth and Sudarshan3.37Database System Concepts

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

©Silberschatz, Korth and Sudarshan3.38Database System Concepts

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

©Silberschatz, Korth and Sudarshan3.39Database System Concepts

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

©Silberschatz, Korth and Sudarshan3.40Database System Concepts

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

©Silberschatz, Korth and Sudarshan3.41Database System Concepts

Result of Result of employee ft-worksemployee ft-works

©Silberschatz, Korth and Sudarshan3.42Database System Concepts

Tuples Inserted Into Tuples Inserted Into loan loan and and borrowerborrower

©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

©Silberschatz, Korth and Sudarshan3.44Database System Concepts

E-R DiagramE-R Diagram

©Silberschatz, Korth and Sudarshan3.45Database System Concepts

The The branchbranch Relation Relation

©Silberschatz, Korth and Sudarshan3.46Database System Concepts

The The loan loan RelationRelation

©Silberschatz, Korth and Sudarshan3.47Database System Concepts

The The borrowerborrower Relation Relation