chapter 6: formal relational query languages

44
Chapter 6: Formal Chapter 6: Formal Relational Query Languages Relational Query Languages Relational Algebra basics Relational Algebra basics

Upload: orien

Post on 05-Jan-2016

251 views

Category:

Documents


9 download

DESCRIPTION

Chapter 6: Formal Relational Query Languages. Relational Algebra basics. ER for Banking Enterprise. Schema Diagram for the Banking Enterprise. Query Languages. Categories of languages procedural non-procedural “Pure” languages: Relational Algebra Tuple Relational Calculus - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 6:   Formal Relational  Query Languages

Chapter 6: Formal Chapter 6: Formal Relational Query Relational Query

LanguagesLanguages Relational Algebra basicsRelational Algebra basics

Page 2: Chapter 6:   Formal Relational  Query Languages

ER for Banking EnterpriseER for Banking Enterprise

Page 3: Chapter 6:   Formal Relational  Query Languages

Schema Diagram for the Schema Diagram for the Banking EnterpriseBanking Enterprise

Page 4: Chapter 6:   Formal Relational  Query Languages

Query LanguagesQuery Languages

Categories of languagesCategories of languages proceduralprocedural non-proceduralnon-procedural

““Pure” languages:Pure” languages: Relational AlgebraRelational Algebra Tuple Relational CalculusTuple Relational Calculus Domain Relational CalculusDomain Relational Calculus

Declarative languages:Declarative languages: SQLSQL

Page 5: Chapter 6:   Formal Relational  Query Languages

Relational AlgebraRelational Algebra

Procedural languageProcedural language Six basic operatorsSix basic operators

Select Select Projection Projection Union Union set difference set difference – Cartesian product Cartesian product x Rename Rename

The operators take one or more relations as The operators take one or more relations as inputs and give a new relation as a result.inputs and give a new relation as a result.

Page 6: Chapter 6:   Formal Relational  Query Languages

Select Operation – ExampleSelect Operation – Example

• 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

Page 7: Chapter 6:   Formal Relational  Query Languages

Select OperationSelect Operation Notation: Notation: pp((rr)) pp is called the is called the selection predicateselection predicate Defined as:Defined as:

pp((rr) = {) = {tt | | tt rr and and p(t)p(t)}}WhereWhere p p is a formula in propositional calculus is a formula in propositional calculus consisting of consisting of terms terms connected by : connected by : ( (andand), ), ((oror), ), ( (notnot))Each Each termterm is one of: is one of:<attribute><attribute>opop <attribute> or <constant><attribute> or <constant>

where where opop is one of: =, is one of: =, , >, , >, . <. . <.

Page 8: Chapter 6:   Formal Relational  Query Languages

Example of selectionExample of selection

branch-name=“Perryridgebranch-name=“Perryridge””((accountaccount)) Selection gives a horizontal subset of a relationSelection gives a horizontal subset of a relation

a subset of all the tuples (rows) of a relationa subset of all the tuples (rows) of a relation

branch-branch-name=“Perryridge”name=“Perryridge”((accountaccount)) ??

account

Page 9: Chapter 6:   Formal Relational  Query Languages

Project Operation – Project Operation – ExampleExample

RelationRelation r r::

A B C

10

20

30

40

1

1

1

2

A C

1

1

1

2

=

A C

1

1

2

A,CA,C ( (rr))

Page 10: Chapter 6:   Formal Relational  Query Languages

Project OperationProject Operation

Notation:Notation:

A1, A2, …,A1, A2, …, Ak Ak ( (rr))

where where AA11, A, A22 are attribute names and are attribute names and rr is a is a relation name.relation name.

The result is defined as the relation of The result is defined as the relation of kk columns columns obtained by erasing the columns that are not obtained by erasing the columns that are not listedlisted

Duplicate rows removed from result, since Duplicate rows removed from result, since relations are setsrelations are sets

Page 11: Chapter 6:   Formal Relational  Query Languages

Example of ProjectionExample of Projection

To eliminate the To eliminate the branch-namebranch-name attribute of attribute of accountaccount account-number, balanceaccount-number, balance ( (accountaccount) )

Projection gives a vertical subset of a relationProjection gives a vertical subset of a relation a subset of all the columns of a relationa subset of all the columns of a relation

account-number, balanceaccount-number, balance ((accountaccount)) ?

account

Page 12: Chapter 6:   Formal Relational  Query Languages

Union Operation – ExampleUnion Operation – Example Relations Relations r, s:r, s:

r s:

A B

1

2

1

A B

2

3

rs

A B

1

2

1

3

Page 13: Chapter 6:   Formal Relational  Query Languages

Union OperationUnion Operation

Notation: Notation: rr ss Defined as: Defined as:

rr ss = { = {tt | | tt rr or or t t ss}}

For For rr ss to be valid. to be valid.1.1. r,r, ss must have the must have the same same arityarity (same number of (same number of

attributes)attributes)

2.2. The attribute domains must be The attribute domains must be compatiblecompatible (e.g., 2nd (e.g., 2nd column of column of rr deals with the same type of values as does deals with the same type of values as does the 2nd column of the 2nd column of ss))

Page 14: Chapter 6:   Formal Relational  Query Languages

Example of UnionExample of Union Find all customers with either an account or a loanFind all customers with either an account or a loan

customer-namecustomer-name ( (depositordepositor) ) customer-namecustomer-name ( (borrower)borrower)

depositor

borrower

customer-namecustomer-name ((depositordepositor))

customer-namecustomer-name ( (borrower)borrower)

?

?

?

Page 15: Chapter 6:   Formal Relational  Query Languages

Set Difference Operation – Set Difference Operation – ExampleExample

Relations Relations r, s:r, s:

r – s:

A B

1

2

1

A B

2

3

rs

A B

1

1

Page 16: Chapter 6:   Formal Relational  Query Languages

Set Difference OperationSet Difference Operation

Notation Notation r – sr – s Defined as:Defined as:

r – sr – s = { = {tt | | tt rr andand t t ss}} Set differences must be taken between Set differences must be taken between

compatiblecompatible relations. relations. rr and and ss must have the must have the same aritysame arity attribute domains of attribute domains of r r and and s s must be must be

compatiblecompatible

Page 17: Chapter 6:   Formal Relational  Query Languages

Example of Set DifferenceExample of Set Difference Find all customers with either an account or a loanFind all customers with either an account or a loan

customer-namecustomer-name ( (depositordepositor) ) customer-namecustomer-name ( (borrower)borrower)

depositor

borrower

customer-namecustomer-name ((depositordepositor))

customer-namecustomer-name ( (borrower)borrower)

--

?

?

?

Page 18: Chapter 6:   Formal Relational  Query Languages

Cartesian-Product Operation-Cartesian-Product Operation-ExampleExample

Relations r, s:

r x s:

A B

1

2

A B

11112222

C D

1010201010102010

E

aabbaabb

C D

10102010

E

aabbr

s

Page 19: Chapter 6:   Formal Relational  Query Languages

Cartesian-Product Cartesian-Product OperationOperation

NotationNotation r r xx s s Defined as:Defined as:

rr x x ss = { = {t q t q || t t r r and and q q ss}} Assume that attributes of r(R) and s(S) are Assume that attributes of r(R) and s(S) are

disjoint. (That is, disjoint. (That is, RR S S = = ).). If attributes of If attributes of r(R)r(R) and and s(Ss(S) are not ) are not

disjoint, then renaming must be used.disjoint, then renaming must be used.

Page 20: Chapter 6:   Formal Relational  Query Languages

the borrower relationthe borrower relation

Page 21: Chapter 6:   Formal Relational  Query Languages

the loan relationthe loan relation

Page 22: Chapter 6:   Formal Relational  Query Languages

Result of Result of borrower borrower |X| |X| loanloan

Page 23: Chapter 6:   Formal Relational  Query Languages

Cartesian-Product Cartesian-Product OperationOperation

Cartesian-Product itself is usually not so Cartesian-Product itself is usually not so usefuluseful

It is often used as a “pre-processing”It is often used as a “pre-processing” Other operators such as selection and Other operators such as selection and

projection will followprojection will follow

Page 24: Chapter 6:   Formal Relational  Query Languages

Composition of OperationsComposition of Operations Can build expressions using multiple Can build expressions using multiple

operationsoperations Example: Example: A=CA=C((r x sr x s)) r x sr x s

A=CA=C((r x sr x s))

A B

11112222

C D

1010201010102010

E

aabbaabb

A B C D E

122

102020

aab

Page 25: Chapter 6:   Formal Relational  Query Languages

Rename OperationRename Operation

Allows us to refer to a relation by more Allows us to refer to a relation by more than one name. than one name. Example: Example: xx ( (EE))

returns the expression returns the expression EE under the name under the name XX

If a relational-algebra expression If a relational-algebra expression EE has has arity arity nn, then , then xx ((A1, A2, …, AnA1, A2, …, An)) ((EE))

returns the result of expression returns the result of expression EE under the name under the name XX, , and with the attributes renamed to and with the attributes renamed to AA11, A2, …., An, A2, …., An..

Page 26: Chapter 6:   Formal Relational  Query Languages

Rename OperationRename Operation Example: Example:

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

((branch-name=“Downtown”branch-name=“Downtown”((accountaccount))account

?

Page 27: Chapter 6:   Formal Relational  Query Languages

Banking ExampleBanking Example

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

customer (customer-name, customer-street, customer (customer-name, customer-street, customer-only)customer-only)

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

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

depositor (customer-name, account-number)depositor (customer-name, account-number)

borrower (customer-name, loan-number)borrower (customer-name, loan-number)

Page 28: Chapter 6:   Formal Relational  Query Languages

Example QueriesExample Queries Find all loans of over $1200Find all loans of over $1200

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

amount > 1200 (loan)

loan-number (amount > 1200 (loan))

Page 29: Chapter 6:   Formal Relational  Query Languages

Example QueriesExample Queries

Find the names of all customers who have a Find the names of all customers who have a loan, an account, or both, from the bankloan, an account, or both, from the bank

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

customer-name (borrower) customer-name (depositor)

customer-name (borrower) customer-name (depositor)

Page 30: Chapter 6:   Formal Relational  Query Languages

Example QueriesExample Queries Find the names of all customers who have a loan Find the names of all customers who have a loan

at the Perryridge branch.at the Perryridge branch.

Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank.customer-name (branch-name = “Perryridge”

(borrower.loan-number = loan.loan-number(borrower x loan))) –

customer-name(depositor)

customer-name (branch-name=“Perryridge”

(borrower.loan-number = loan.loan-number(borrower x loan)))

Page 31: Chapter 6:   Formal Relational  Query Languages

Example QueriesExample Queries Find the names of all customers who have a loan at the Find the names of all customers who have a loan at the

Perryridge branch.Perryridge branch.

Query 2

customer-name(loan.loan-number = borrower.loan-number(

(branch-name = “Perryridge”(loan)) x borrower))

Query 1

customer-name(branch-name = “Perryridge” ( borrower.loan-number = loan.loan-number(borrower x loan)))

Page 32: Chapter 6:   Formal Relational  Query Languages

Example QueriesExample Queries

Find the largest account balanceFind the largest account balance Rename Rename account account relation as relation as dd The query is:The query is:

balance(account) - account.balance

(account.balance < d.balance (account x d (account)))

Page 33: Chapter 6:   Formal Relational  Query Languages

branch

branch-name

assets

branch

Page 34: Chapter 6:   Formal Relational  Query Languages

account-number

branch-name

balance

account

Page 35: Chapter 6:   Formal Relational  Query Languages

customer-name

account-number

depositor

Page 36: Chapter 6:   Formal Relational  Query Languages

customer-name

customer-street

customer-city

customer

Page 37: Chapter 6:   Formal Relational  Query Languages

customer-name

loan-number

borrower

Page 38: Chapter 6:   Formal Relational  Query Languages

loan-number

branch-name

amount

loan

Page 39: Chapter 6:   Formal Relational  Query Languages

customercustomer

Page 40: Chapter 6:   Formal Relational  Query Languages

branchbranch

Page 41: Chapter 6:   Formal Relational  Query Languages

loanloan

Page 42: Chapter 6:   Formal Relational  Query Languages

accountaccount

Page 43: Chapter 6:   Formal Relational  Query Languages

borrowerborrower

Page 44: Chapter 6:   Formal Relational  Query Languages

depositordepositor