Chapter 6: Formal Relational Query Languages
description
Transcript of Chapter 6: Formal Relational Query Languages
![Page 1: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/1.jpg)
Chapter 6: Formal Chapter 6: Formal Relational Query Relational Query
LanguagesLanguages Relational Algebra basicsRelational Algebra basics
![Page 2: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/2.jpg)
ER for Banking EnterpriseER for Banking Enterprise
![Page 3: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/3.jpg)
Schema Diagram for the Schema Diagram for the Banking EnterpriseBanking Enterprise
![Page 4: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/20.jpg)
the borrower relationthe borrower relation
![Page 21: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/21.jpg)
the loan relationthe loan relation
![Page 22: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/22.jpg)
Result of Result of borrower borrower |X| |X| loanloan
![Page 23: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/32.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/33.jpg)
branch
branch-name
assets
branch
![Page 34: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/34.jpg)
account-number
branch-name
balance
account
![Page 35: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/35.jpg)
customer-name
account-number
depositor
![Page 36: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/36.jpg)
customer-name
customer-street
customer-city
customer
![Page 37: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/37.jpg)
customer-name
loan-number
borrower
![Page 38: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/38.jpg)
loan-number
branch-name
amount
loan
![Page 39: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/39.jpg)
customercustomer
![Page 40: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/40.jpg)
branchbranch
![Page 41: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/41.jpg)
loanloan
![Page 42: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/42.jpg)
accountaccount
![Page 43: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/43.jpg)
borrowerborrower
![Page 44: Chapter 6: Formal Relational Query Languages](https://reader033.fdocuments.net/reader033/viewer/2022061318/56813a3d550346895da2270e/html5/thumbnails/44.jpg)
depositordepositor