Chapter 6: Formal Chapter 6: Formal Relational Query Relational Query
LanguagesLanguages Relational Algebra basicsRelational Algebra basics
ER for Banking EnterpriseER for Banking Enterprise
Schema Diagram for the Schema Diagram for the Banking EnterpriseBanking Enterprise
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
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.
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
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: =, , >, , >, . <. . <.
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
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))
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
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
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
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))
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)
?
?
?
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
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
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)
--
?
?
?
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
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.
the borrower relationthe borrower relation
the loan relationthe loan relation
Result of Result of borrower borrower |X| |X| loanloan
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
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
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..
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
?
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)
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))
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)
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)))
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)))
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)))
branch
branch-name
assets
branch
account-number
branch-name
balance
account
customer-name
account-number
depositor
customer-name
customer-street
customer-city
customer
customer-name
loan-number
borrower
loan-number
branch-name
amount
loan
customercustomer
branchbranch
loanloan
accountaccount
borrowerborrower
depositordepositor
Top Related