chapter 3 relational model 3.1 an informal look at the relational modelan informal look at the...
TRANSCRIPT
Chapter 3 relational model
3.1 An Informal Look at the Relational Model 3.2 The Relational Algebra 3.3 Extended Relational-Algebra Operation 3.4 Relational Calculus 3.5 The Tuple Relational Calculus 3.6 The Domain Relational Calculus 3.7 Modification of the Database 3.8 View Definition 3.9 Exercises
3.1 An Informal Look at the Relational Model
Relational system are based on a formal foundation, or theory, called the relational model of data. Intuitively, what this statement means is that in such a system:
1. Structural aspect: The data in the database is perceived by the user as tables, and nothing but tables.
2. Integrity aspect: Those table satisfy certain integrity constraints.
3. Manipulative aspects: The operators available to the user for manipulating those tables—e.g. for purposes of data retrieval—are operators that derive tables from tables of those operators, three particular important ones are restrict, project, and join.
3.1.1 Basic Structure
The relational model can be regarded as having three principal parts, having to do with data structure, data integrity, and data manipulation.
tuple, cardinality, attribute, degree, domain, primary key. INTEGER: operations: + - * / = … substr Briefly, if we think of a relation as a table, then a tuple corres
ponds to a row of such a table and an attribute to a column; the number of tuples is called the cardinality and the number of attribute is called the degree; and a domain is a pool of values, from which the values of specific attributes of specific relation are taken.
3.1.1 Basic Structure
S# NAME STATUE CITY Paris…
relation
primary key
degree
tuple
attributecardinality
S# S# SNAME NAME STATUS STATUS CITY CITY
S1
S2
S3
S4
Smith
Jones
Blake
Adams
20
10
30
20
London
Paris
Athens
Paris
3.1.2 Database Schema
Difference:
database schema logical design of the database,
database instance snapshot of the data in the database at a given instant in time
relation schema relation relation instance
int i i i=2
3.1.2 Database Schema
We adopt the convention of using lowercase names for relations, and names beginning with an uppercase letter for relation schemas.
Example:
we use Account-Schema to denote the relation schema for relation account Account-schema=(branch-name, account-number, balance)
we denote the fact that account is a relation on Account-schema by account(Account-schema)
3.1.3 key and schema diagram
Foreign key: referencing relation referenced relation
referenced relation
referencing relation
referencing relation
Foreign key
account
account-number
branch-name
balance
branch-name
branch-city
assets
branch
loan-number
branch-name
amount
loan
3.1.4 query language
query language
procedural language nonprocedural language
relation algebra tuple relation calculus domain relation calculus
3.2 The Relational Algebra
The relational algebra is a procedural query language. The consist of a set of operations that take one or two relations as input and produce a new relation as their result.
The original algebra consisted of eight operators, two groups of four each:
1. The traditional set operators union, intersection, difference, and Cartesian product.
2. The special relational operators restrict (as known as select), project, join and divide.
select
3.2.1 Fundamental Operations
1.The Select Operation ( ) Select: Returns a relation containing all tuples from a specified relation that satisfy a specified condition. the select operation selects tuples that satisfy a given predicate.
C
cf
Ab
b
a cb
d
Relation R
a
B a b c
c b d
B=b(R)
A B C
Example: B=b(R)
3.2.1 Fundamental Operations
Let relation A have attributes X and Y, and let be an operator-typically” =” ,” >” , etc, --such that the condition X Y is well defined and, given particular values for X and Y, evaluates to a truth value(true or false), Then the --restriction of relation A on attributes X and Y—is a relation with the same heading as A and with body consisting of all tuples t of A such that the condition X Y evaluates to true for that tuple t .
F(A) X > Y(A) X=1(A) [1] > [2](A)
amount<1000(loan)
3.2.1 Fundamental Operations
② select those tuples in which the amount lent is less than $ 1000
branch-name=“perryridge”(loan)
Examples:
① select those tuples of the loan relation where the branch is “Perryridge”
loan-number branch-name amount
L-11 Round Hidd 900
L-14 Downtown 1500
L-15 Perryridge 1500
L-16 Perryridge 1100
L-17 Downtown 1000
loanloan-number branch-name amount
L-15 Perryridge 1500
L-16 Perryridge 1100
loan-number branch-name amount
L-11 Round Hidd 900
3.2.1 Fundamental Operations
③ select those tuples pertaining to loans of more than $ 1200 made by the Perryridge branch.
branch-name=“perryridge” amount>1200∧ (loan)
loan-number branch-name amount
L-11 Round Hidd 900
L-14 Downtown 1500
L-15 Perryridge 1500
L-16 Perryridge 1100
L-17 Downtown 1000
loanloan-number branch-name amount
L-15 Perryridge 1500
3.2.1 Fundamental Operations
④ find all customers who have the same name as their loan officer
customer-name=banker-name(loan-officer)
loan-number customer-name banker-name
L-11 Curry Adams
L-14 Jones Jackson
L-15 Smith Smith
Loan-officer
loan-number customer-name banker-name
L-15 Smith Smith
project
3.2.1 Fundamental Operations
2. The Project Operation pi ( ∏ )
Project: Returns a relation containing all tuples that remain in a specified relation after specified attributes have been removed.
Example:
C
cf
Ab
b
a cb
d
Relation R
a
B
f
c
d
A C∏A 、 C(R)
b
a
c
3.2.1 Fundamental Operations
① List all loan numbers and the amount of the loan
∏loan-number,amount(loan)
loan-number branch-name amount
L-11 Round Hidd 900
L-14 Downtown 1500
L-15 Downtown 1500
L-16 Perryridge 1300
L-17 Downtown 1000
loan loan-number amount
L-11 900
L-14 1500
L-15 1500
L-16 1300
L-17 1000
3.2.1 Fundamental Operations
Notice:
⑴ No attribute can be mentioned more than once in the attribute name commalist.
⑵ If the attribute name commalist mentions all of the attributes of A, the projection is an identity projection.
⑶ A projection of the form A{}—I.e., one in which the attribute name conmmalist is empty—is legal. It represents a nullary projection.
3.2.1 Fundamental Operations
3. Composition of Relational Operations
relational—algebra operations can be composed together into a relational—algebra expression.
∏customer-name( customer-city=“Harrison”(customer))Example: Find those customers who live in “Harrison” .
customer-name
Brooks
customercustomer-citycustomer-street
Adams
Brooks
Glenn
Hayes
Spring
Senator
North
Walnut
Pittsfield
Harrison
Rye
Woodside
customer-name
3.2.1 Fundamental Operations
4. The Union Operation ( ∪ )
Union: Returns a relation containing all tuples that appear in either or both of two specified relations.
Definition: Given two relations A and B of the same type, the union of those two relations, A B∪ , is a relation of the same type, with body consisting all tuples t such that t appears in A or in B or in both.
Example:
BA C
R ∪ S
abcb
babg
cfda
C
cf
Ab
b
a cb
d
Relation R
a
B
b
A B CRelation S
b g a
a f
3.2.1 Fundamental Operations
① Find the names of all bank customers who have either an account or a loan or both.
∏customer-name(borrower) ∏∪ customer-name(depositor)customer-name loan-number
Hayes A-102
Johnson A-101
borrower
customer-name account-number
Hayes L-16
Curry L-93
depositor
customer-name
Hayes
Johnson
Curry
3.2.1 Fundamental Operations
For a union operation r s to be valid, we require that ∪two conditions hold:
⑴ The relations r and s must be of the same arity, that is, they must have the same number of attributes.
⑵ The domains of the ith attribute of r and the ith attribute of s must be the same, for all i.
3.2.1 Fundamental Operations
5. The set Difference Operation ( - )
Difference: Returns a relation containing all tuples that appear in the first and not the second of two specified relations.
Definition: Given two relations A and B of the same type, then, the difference between those two relations, A-B, is a relation of the same type, with body consisting of all tuples t such that t appears in A are not in B.
R-S
A B Ca b c
c b d
C
cf
Ab
b
a cb
d
Relation R
a
B
b
A B CRelation S
b g a
a f
Example:
3.2.1 Fundamental Operations
① Find all customers of the bank who have an account but not a loan.
∏customer-name(depositor) -∏ customer-name(borrower)
customer-name loan-number
Hayes A-102
Johnson A-101
borrower
customer-name account-number
Hayes L-16
Curry L-93
depositor customer-name
Curry
3.2.1 Fundamental Operations
Notice:
① For a difference operation A-B to be valid, we require that the relation
② A and B be of the same arity, and that the domains of the ith attribute of A and the ith attribute of B be the same.
3.2.1 Fundamental Operations
6. The Cartesian-Product Operation ( × )
Cartesian-Product: In mathematics, the Cartesian-Product of two sets is the set of all ordered pairs such that, in each pair, the first element comes from the first set and the second element comes fromthe second set. Thus, the Cartesian-Product of two relations would be a set of ordered pair of tuples
We write the Cartesian-Product of relations r1 and r2 as r1× r2
3.2.1 Fundamental Operations
C
cf
Ab
b
a cb
d
Relation R
a
B
b
D E FRelation S
b g a
a f
Example:
a b c
a b c
b a f
b a f
c d d
c d d
b g a
b g a
b g a
b a f
b a f
b a f
R×S
A B C D E F
3.2.1 Fundamental Operations
Example: r=borrower×loan
the relation schema:(customer-name, borrower.loan-number, branch-name, loan.loan-number, branch-name,amount)
×customer-name loan-number branch-name
borrower loanloan-number branch-name amount
customer-name loan-number branch-name loan-number branch-name amount
Problem: A problem will therefore arise if those two headings have any attribute name in common. How to distinguish between then. We do so here by attaching to an attribute the name of the relation from which the attribute originally name. For those attributes that appear in only one of the two schemas, we shall usually drop the relation-name prefex.
3.2.1 Fundamental Operations
Example:
Find the names of all customers who have a loan at the Perryride branch.
borrowercustomer-name loan-number
Hayes A-102
Johnson A-103
Smith A-101
loan loan-number branch-name amount
A-103 Round Hidd 900
A-102 Perryridge 1500
A-101 Perryridge 1400
customer-name
borrower.loan-number
loan.loan-number
branch-name amount
3.2.1 Fundamental Operations
∏customer-name( branch-name=“Perryridge” (borrower×loan))
Hayes A-102 A-103 Round Hidd 900A-102 Perryridge 1500
A-101 Perryridge 1400
Hayes A-102
Hayes A-102 Johnson A-103
Johnson A-103Johnson A-103
A-103 Round Hidd 900
A-102 Perryridge 1500A-101 Perryridge 1400
Smith A-101Smith A-101
Smith A-101
A-103 Round Hidd 900
A-102 Perryridge 1500
A-101 Perryridge 1400
×
3.2.1 Fundamental Operations
∏customer-name( borrower.loan-number=loan.loan-number(
branch-name=“Perryridge” (borrower×loan)))
customer-name
borrower.loan-number
loan.loan-number branch-name amount
A-102 Perryridge 1500
A-102 Perryridge 1500A-101 Perryridge 1400
Hayes A-102Hayes A-102Johnson A-103Johnson A-103 A-101 Perryridge 1400Smith A-101Smith A-101
A-102 Perryridge 1500A-101 Perryridge 1400
customer-name
Hayes
Smith
3.2.1 Fundamental Operations
7. The Rename Operation ( ) ① The rename operator, denoted by the lower-case Greek letter rh
o(). Given a relational-algebra expression E, the expression x(E) returns the result of expression E under the name X.
② A second form of the rename operation is as follows:
Assume that a relational algebra expression E has arity n, the ex
pression x(A1,A2,……An)(E) returns the result of expression E under the name X, and with the attributes renamed to A1,A2,……An.
3.2.1 Fundamental Operations
Examples:
① Find the largest account balance in the bank.
×
branch-name account-number balance
R Hidd L-101 300
Perry L-102 500
Perry L-103 450
Account
d branch-name account-number balance
R Hidd L-101 300
Perry L-102 500
Perry L-103 450
3.2.1 Fundamental Operations
R Hidd
R Hidd
R Hidd
Perry
Perry
Perry
Perry
Perry
Perry
L-101
L-101
L-101
L-102
L-102
L-102
L-103
L-103
L-103
300
300
300
500
500
500
450
450
450
R Hidd
Perry
Perry
R Hidd
Perry
Perry
R Hidd
Perry
Perry
L-101
L-102
L-103
L-101
L-102
L-103
L-101
L-102
L-103
300
500
450
300
500
450
300
500
450
account.branch-name
account.account-number
account.balance branch-name
d. d.account-number
d.balance
3.2.1 Fundamental Operations
-
∏balance(account) - ∏ account.balance( account.balance<d.balance
(account× d(account)))
R Hidd
R Hidd
Perry
L-101
L-101
L-103
300
300
450
Perry
Perry
Perry
L-102
L-102
L-102
500
450
500
account.branch-name
account.account-number
account.balance branch-name
d. d.account-number
d.balance
account.balance
300
500
450
account.balance
300
450
account.balance
500
3.2.1 Fundamental Operations
∏customer.customer-name(customer.customer-street=smith-addr.street∧
customer.customer-city=smith-addr.city (customer× smith-addr(st
reet,city)(∏customer-street,customer-city (customer-name=“Smith(customer)))))
② Find the names of all customers who live on the same street and in the same city as Smith.
3.2.2 Additional Operations
1. The Set-Intersection Operation ( ∩ ) Suppose that we wish to find all customers who
have both a loan and an account, using set intersection
we can write.
∏customer-name(borrower)∩∏customer-name(depositor) Notice: We can rewrite any relation algebra expression using set intersection by replacing the intersection operation with a pair of set-difference operations as follows:
r∩s=r - (r - s)
3.2.2 Additional Operations
Example:
A B C
a
e
g
b
d
h
c
f
i
R
b
d
k
A B C
a
e
i
c
f
l
S
a b c
e d f
A B C
R∩S
R - (R -S)
3.2.2 Additional Operations
2. The Natural-Join Operation
Typically, a query that involves a Cartesian Product includes a selection operation on the result of the Cartesian Product .
The natural join is a binary operation that allows us to combine certain selections and a Cartesian Product into one operation. It is denoted by the “join ” symbol
3.2.2 Additional Operations
The natural join operation include three steps:
r s= ∏R S ∪ ( r.A1=s.A1 r.A2=s.A2… r.An=s.An∧ ∧ (r×s))①Cartesian Product
Where R∩S={A1,A2, …An} ② Select tuples
③ Remove duplicate attributes
Consider two relations r(R) and s(S). The natural join of r and s, denoted by r s is a relation on schema R S formally defined as ∪following:
② Select those tuples which satisfy the requirements R.Ai=S.Ai
③ Remove duplicate attributes.
① From a Cartesian Product R×S
3.2.2 Additional Operations
Example:
①
2
5
8
A B C
1
4
7
3
6
9
Relation R1B C D
2
5
9
3
6
8
2
3
5
Relation S1
2
5
A B C D
1
4
3
6
2
3
R1 S1
3
6
A B C B C D
1
4
2
5
3
6
2
5
2
3
R1 S1[3]=[2]
A B C B C D
1
1
1
4
2
2
2
5
3
3
3
6
2
5
9
9
3
6
8
8
2
3
5
5
R1 S1A<D
3.2.2 Additional Operations
② Find the name of all customers who have a loan at the bank, and find the amount of the loan.
∏customer-name,loan-number,amount(borrower loan)
Loan-schema=(loan-number, branch-name,amount)
Borrower-schema=(customer-name, loan-number)
∏customer-name(borrower depositor)
③ Find all customers who have both a loan and an account at the bank.
Depositor-schema=(customer-name, account-number)
Borrower-schema=(customer-name, loan-number)
3.2.2 Additional Operations
④ Find the names of all branches with customers who have an account in the bank and who live in Harrison.
∏branch-name(customer-city=“Harrison”(customer account depositor)
Customer-schema=(customer-name, customer-street, customer-city)
Account-schema=(branch-name, account-number, balance)
Depositor-schema=(customer-name, account-number)
1 2
3.2.2 Additional Operations
3. The Division Operation ÷ Let r(R) and s(S) be relations, and let SR, that is every attrib
ute of schema S is also in schema R. The relation r÷s is a relation on schema R - S—that is, on the schema containing all attributes of schema R that are not in schema S.
r÷s R1 2 3 4
a1 b1 c1 d1a1 b1 c2 d2a1 b1 c3 d3a2 b2 c1 d1
a2 b2 c2 d2
a3 b3 c1 d1
3 4c1 d1c2 d2
S
a1 b1a2 b2
Example: find all customers who have an account at all the branches located in Brooklyn.
∏customer-name,branch-name(depositor account) ÷
∏branch-name(branch-city=“Brooklyn” (branch))
3.2.2 Additional Operations
customer-name
Johnson
customer-name branch-name
Hayes
Johnson
Johnson
Turner
Perryridge
Downtown
Brighton
Brighton
branch-name
Downtown
Brighton
3.2.2 Additional Operations
r÷s= ∏R - S(r)-∏ R - S((∏R - S(r) ×s) -∏ R - S,S (r))
A tuple t is in r÷s if and only if both of two conditions hold:
① t is in ∏R - S(r) ② For every tuples ts in s, there is a tuple tr in r satisfying both of t
he following:
a. tr[S]= ts[S]
b. tr[R - S]=t
Given a division operation and the schemas of the relations, we can, in fact, define the division operation in terms of the fundament operations, let r(R) and s(S) be given, with S R
R 1 2 3 4a1 b1 c1 d1a1 b1 c2 d2
3 4c1 d1c2 d2
S r÷s1 2a1 b1
3.2.2 Additional Operations
r÷s= ∏R - S(r)-∏ R - S((∏R - S(r)×s) -∏ R - S,S (r))
V P
V2 P4
V
V2-
P
P1
P2
P4
P2
P3
P2
P4
V
V1
V1
V1
V2
V2
V3
V3
RP
P2
P4
S
V
V1
V2
V3
×
P
P2
P4
P2
P4
P2
P4
V
V1
V1
V2
V2
V3
V3
-
P
P1
P2
P4
P2
P3
P2
P4
V
V1
V1
V1
V2
V2
V3
V3
R
V
V1
V2
V3
V
V1
V3
3.2.2 Additional Operations
4. The Assignment Operation ← It is convenient at times to write a relational-algebra expression i
n parts using assignment to a temporary relation variable. The assignment operation denoted by ←.
Example: ∏R-S(r) -∏R-S(∏R-S(r)×s) - r)temp1 ← ∏R-S(r)temp2 ←∏R-S(temp1×s) - r)result =temp1-temp2
For relation-algebra quires, assignment must always be made to a temporary relation variable. Assignments to permanent relations constitute a database modification.
3.3 Extended Relational-Algebra Operation
allow arithmetic operations as part of projection outer-join
aggregate operations
• Generalized-projection ∏F1,F2,…Fn(E)Example: Suppose we have a relation credit-info which lists the credit limit and expenses so far. If we want to find how much more each person can spend, how to write the expression?
∏customer-name,(limit-credit-balance) as credit-available(credit-info)
customer-name credit-available
curry 250
customer-name limit credit-balance
curry 1200 1750
credit-info
3.3 Extended Relational-Algebra Operation
Aggregate functionsum avg min max count
Examples: ① Find the total salary of all the part workers.
employee-name branch-name salary
Admas Perryridge 1500 Brown Perryridge 1300 Gopal Perryridge 5300 Johnson Downtown 1500 Loreena Downtown 1300 Petrson Downtown 2500 Rao Austin 1500 Sato Austin 1600
pt-works
Gsum(salary)(pt-works)sum of salary
16500
② Find the number of the branch in the pt-works relation.
Gcount-distinct(branch-name)(pt-works)count of branch-name
3
3.3 Extended Relational-Algebra Operation
Group
branch-nameGsum(salary)(pt-works)
employee-name branch-name salary
Admas Perryridge 1500 Brown Perryridge 1300 Gopal Perryridge 5300 Johnson Downtown 1500 Loreena Downtown 1300 Petrson Downtown 2500 Rao Austin 1500 Sato Austin 1600
pt-works
branch-name sum of salary
Perryridge 8100
Downtown 5300 Austin 3100
3.3 Extended Relational-Algebra Operation
Aggregation operation
G1,G2,…,GnGF1(A1), F2(A2)…, Fm(Am)(E)
branch-nameGsum(salary) as sum-salary, max(salary) as max-salary(pt-works)
branch-name sum-salary max-salary
Austin 3100 1600
Downtown 5300 2500Perryridge 8100 5300
3.3 Extended Relational-Algebra Operation
Outer-joinProblem:
employeeemployee-name street city
Coyote Toon Hollywood Rabbit Tunnel Carrotville
Smith Revolver Deach ValleyWilliams Seaview Seattle
employee-name branch-name salary
Coyote Mesa 1500 Rabbit Mesa 1300 Gates Redmond 5300 Williams Redmond 1500
ft-works
employee-name street city branch-name salary
Coyote Toon Hollywood Mesa 1500
Rabbit Tunnel Carrotville Mesa 1300 Williams Seaview Seattle Redmond 1500
3.3 Extended Relational-Algebra Operation
Right outer join
employee ft-works
employee-name street city branch-name salary
Coyote Toon Hollywood Mesa 1500
Rabbit Tunnel Carrotville Mesa 1300 Williams Seaview Seattle Redmond 1500 Smith Revolver DeathValley null null
employee ft-works
employee-name street city branch-name salary
Coyote Toon Hollywood Mesa 1500 Rabbit Tunnel Carrotville Mesa 1300 Williams Seaview Seattle Redmond 1500
Gates null null Redmond 5300
Left outer join
3.3 Extended Relational-Algebra Operation
employee ft-works
employee-name street city branch-name salary
Coyote Toon Hollywood Mesa 1500 Rabbit Tunnel Carrotville Mesa 1300
Williams Seaview Seattle Redmond 1500
Smith Revolver DeathValley null null Gates null null Redmond 5300
Full outer join
3.4 Relational Calculus
The manipulative part of the relational model was based on the relational algebra; however, we might equally well have said it was based on the relational calculus instead. In other words, the algebra and the calculus are alternatives to each other. The principal distinction between them is a follows: whereas the algebra provides a collection of explicit operators—join, union, project, etc. —that can be used to tell the system how to construct some desired relation from given relations, the calculus merely provides a notation for stating the definition of that desired relation in terms of those given relations.
3.4 Relational Calculus
We might say that—at lease superficially—the calculus formulation is descriptive, while the algebraic one is prescriptive; The calculus simply describes what the problem is, the algebra prescribes a procedure for solving that problem, or very informally: The algebra is procedural; the calculus is nonprocedural
3.4 Relational Calculus
However, we stress the point that the foregoing distinctions are only superficial. The fact is, the algebra and the calculus are logically equivalent: For every algebra expression there is an equivalent calculus one, for every calculus expression there is an equivalent algebraic one. There is a one—to—one correspondence between two. Thus, the difference between them is really just a difference of style: The calculus is arguably closer to natural language, the algebra is perhaps move like a programming language.
3.5 The Tuple Relational Calculus
The tuple relational calculus, is a nonprocedural query language. It describes the desired information without giving a specific procedure for obtaining that information.
A query in the tuple relational calculus is expressed as
{t ︱ p(t)} It is the set of all tuples t such that predicate p is tru
e for t. We use t[A] to denote the value of tuple t on attribute A, and we use t r ∈ to denote that tuple t is in relation r.
loan-number branch-name amount
L-11 Round Hidd 900
L-14 Downtown 1500
L-15 Downtown 1500
L-16 Perryridge 1300
L-17 Downtown 1000
loan
loan-number branch-name amount
L-14 Downtown 1500
L-15 Downtown 1500
L-16 Perryridge 1300
3.5 The Tuple Relational Calculus
Example: ① Find the branch-name, loan-number, and amount for loans of ove
r $1200
{t ︱ t loan t[∈ ∧ amount] >1200}
3.5 The Tuple Relational Calculus
① Find the loan number for each loan of an amount greater than$1200
{t ︱ s∈loan(t[loan-number]=s[loan-number] ∧s[amount] >1200}
There exists t r(Q(t))∈
t r(Q(t))∈ means there exists a tuple t in relation r such predicate Q(t) is true
Example:
Loan-schema=(loan-number, branch-name,amount)
3.5 The Tuple Relational Calculus
{t ︱ s∈borrower(t[customer-name]=s[customer-name] ∧ u ∈ loan(u[loan-number]=s[loan-number] u[branch-name]=“∧ perryridge”))}
② Find the names of all customers who have a loan from the perryridge branch.
Loan-schema=(loan-number, branch-name,amount)
Borrower-schema=(customer-name, loan-number)
3.5 The Tuple Relational Calculus
③ Find all customers who have a loan, an account, or both at the bank
{t ︱ s∈borrower(t[customer-name]=s[customer-name] ) ∨u ∈ depositor(t[customer-name]=u[customer-name])}
④ Find all customers who have both a loan and an account at the bank
{t ︱ s∈borrower(t[customer-name]=s[customer-name] ) ∧u ∈ depositor(t[customer-name]=u[customer-name])}
Depositor-schema=(customer-name, account-number)
Borrower-schema=(customer-name, loan-number)
3.5 The Tuple Relational Calculus
① Find all customers who have an account at the bank but do not have loan from the bank.
{t ︱ u depositor(t[customer-name]=∈ u[customer-name] ) ∧¬ s∈
borrower(t[customer-name]=s[customer-name])}
Not ¬
the formula PQ means”P implies Q”. That is “if p is true, then Q must be true”
Implies
Example:
3.5 The Tuple Relational Calculus
① Find all customers who have an account at all branches located in Brooklyn.
{t ︱ r customer(r[customer-name]=t[customer-name]) ∈ ∧
( u ∈branch(u[branch-city]=“Brooklyn” s ∈depositor(t[customer-name]=s[customer-name] ∧w ∈account(w[account-number]=s[account-number] ∧w[branch-name]=u[branch-name])))}
Example:
For all t r(Q(t))∈ t r(Q(t)) ∈ Q is true for all tuples t in relation r
Account-schema=(branch-name, account-number, balance)
Depositor-schema=(customer-name, account-number)
Branch-schema=(branch-name, branch-city,assets)
3.5 The Tuple Relational Calculus
Formal Definition
A tuple-relation-calculus expression is of the form {t ︱ p(t)} where p is a formula. A tuple variable is said to be a free variable unless it is quantified by a or .
Example:
t loan ∈ ∧ s customer(∈ t[branch-name]=s[branch-name])
t is a free variable. s is a bound variable.
3.5 The Tuple Relational Calculus
A tuple-relational-calculus formula is built up out of atoms. An atom has one of the following forms:
② s[x] u[y], where s and u are tuple variables. X is an attribute on which s is defined, y is an attribute on which u is defined, and is a comparison operator ( < ,≤, = ,≠, > ,≥).
③s[x]c, where s is a tuple variable, x is an attribute on which s is defined, is a comparison operator, and c is a constant in the domain of attribute x.
① s r∈ , where s is a tuple variable and r is a relation.
3.5The Tuple Relational Calculus
We build up formula from atoms using the following rules:
① An atom is a formula
② If p1 is a formula, then so are ¬p1 and (p1)
③ If p1 and p2 are formula, then so are p1 p2∨ , p1 p2∧ and p1p2
④ If p1(s) is a formula containing a free tuple variable s, and r is a relation, then s r(p1(s))∈ and s r(p1(s)∈ ) are also formula
In the tuple relational calculus, these equivalences include the following three rules:
① p1 p2 ∧ ¬ (¬p1 ¬p12)∨
② t r(p1(t)) ∈ ¬ t r(∈ ¬p1 (t))
③ p1p2 ¬p1 p2∨
3.5 The Tuple Relational Calculus
Example:
A1 A2 A3
R
1
3
4
2
a
a
c
b
1
5
4
0
A2A1 A3
S
1
7
9
0
a
f
e
c
1
8
9
5
A2A1 A3
R1
3
4
2
a
c
b
5
4
0
A1 A2 A3
R2
1
3
a
a
1
5
A1 A2 A3
R3
1
3
4
a
c
a
1
4
5
A1A3
R4
1
5
4
0
1
3
4
2
① R1={t ︱ R(t) ∧ ¬ S(t)}
② R2={t ︱ R(t) t[2]=a}∧
③ R3={t ︱ (u)( R(t) S(u) t[1] ∧ ∧ < u[3] t[2] ≠b)}∧
④ R4={t ︱ (u)( R(t) t[1]=u[3] t[2] ∧ ∧ = u[1])}
3.5 The Tuple Relational Calculus
① Union R S={t∪ ︱ R(t) ∨S(t)}
② Difference R - S={t ︱ R(t) ∧ ¬ S(t)}
③ Cartesian Product
R×S={t(k1+k2) ︱ ( U(k1) ( V(k2) (R(u) S(v) t[1]=∧ ∧ u[1] … ∧t[k1]=∧ u[k1] t[k1+1]=∧ v[1] …t[k1+k2]=∧ v[k2]}
(t=t(k1+k2), u=u(k1), v=v(k2))
④ Project
∏i1,i2…ik(k)={t(k) ︱ ( u)(R(u) t[1]=∧ u[i1] t[2]=∧ u[i2] … ∧ ∧t[k]=u[ik]} (t=t(k))
⑤ Select F(R)={t ︱ R(t) F’}∧
3.6 The Domain Relational Calculus
There is a second form of relational calculus called domain relational calculus. This form uses domain variables that take on values from an attribute’s domain, rather than values for an entire tuple.
1. Formal Definition
An expression in the domain relational calculus is of the form
{ < x1,x2…xn >︱ p(x1,x2…xn)} where x1,x2…xn represent domain variables. P represents a formula composed of atoms. An atom in the domain relational calculus has one of the following forms:
3.6 The Domain Relational Calculus
① < x1,x2…xn > ∈ r, where r is a relation on n attributes and x1,x2…xn are domain variables or domain constants.
② xy, where x and y are domain variables and is a comparison operator( < , ≤, = , ≠, > , ≥)
③ x c,where x is a domain variable, is a comparison operator, and c is a constant in the domain of the attribute for which x is a domain variable.
We build up formula from atoms using the following rules:
① An atom is a formula
② If p1 is a formula, then so are ¬p1 and (p1)
③ If p1 and p2 are formula, then so are p1 p2, p1 p2 and p1∨ ∧ p2
④ If p1(x) is a formula in x, where x is a domain variable, x(p1(x)) and xp1(x)) are also formula
3.6 The Domain Relational Calculus
As a notational shorthand, we write
a,b,c(p(a,b,c)) for a(b(c(p(a,b,c))))
B A C
R
5 4 1
bac
168
B A C
S
5
5
2
b
d
c
6
3
4
E D F
W
2
5
4
a
b
c
d
e
f
B A C
R1
4
1
a
c
6
8
B A C
R2
5
4
1
5
b
a
c
d
1
6
8
3
B E A
R3
a
b
c
a
b
c
a
a
a
c
c
c
4
4
4
1
1
1
① R1={XYZ ︱ R(xyz) Z∧ > 5 Y=a}∨
R2={XYZ ︱ R(xyz) (S(xyz) X=5 Z≠6)}∨ ∧ ∧
R3={VYX ︱ (Z)(U)(R(XYZ) W(uvt) Z∧ ∧ > U)}
Example Queries:
3.6 The Domain Relational Calculus
② Find the branch name, loan number and amount for loans of over $1200
{ < b,l,a >︱ < b,l,a > ∈ loan∧a > 1200}
{ < b >︱ l,a( < b,l,a > ∈ loan∧a > 1200)}
③ Find all loan numbers for loans with an amount greater than $1200
Loan-schema=(loan-number, branch-name,amount)
Loan-schema=(branch-name, loan-number,amount)
3.6 The Domain Relational Calculus
{ < c,a >︱ l( < c,l > ∈ borrower ∧ b( < b,l,a > ∈ loan ∧b=“Perryridge”)) }
④ Find the names of all customers who have a loan from the Perryridge branch and find the loan amount
Borrower-schema=(customer-name, loan-number)
3.6 The Domain Relational Calculus
⑤ Find the names of all customers who have a loan, an account, or both at the Perryridge branch
{ < c >︱ l( < c,l > ∈ borrower
∧ b,a( < b,l,a > ∈ loan b=“Perridge”)∧ )
∨ a( < c,a > ∈ depositor
∧ b,n( < b,a,n > ∈ account b=“Perryridge”))∧ }
Account-schema=(branch-name, account-number, balance)
Depositor-schema=(customer-name, account-number)
Loan-schema=( branch-name, loan-number ,amount)
Borrower-schema=(customer-name, loan-number)
3.6 The Domain Relational Calculus
{ < c >︱ n ( < c,n > ∈ customer) ∧
x,y,z( < x,y,z > ∈ branch) ∧ y=“Brooklyn”
a,b( < x,a,b > ∈ account ∧ < c,a > ∈ depositor)}
⑥ Find the names of all customers who have an account at all the branches located in Brooklyn.
Account-schema=(branch-name, account-number, balance)
Depositor-schema=(customer-name, account-number)
Branch-schema=(branch-name, branch-city,assets)
3.7 Modification of the Database
1. Deletion We can delete only whole tuples; we can not delete values on only particular attrib
utes. An insertion is expressed by: r←r - E where r is a relation and E is a relational-algebra expression.
depositor←depositor - customer-name=“smith”(depositor)
loan←loan - amount≥0and amount≤50(loan)
② Delete all loans with amount in the range 0 to 50
Example:
① Delete all of smith’s accounts
Loan-schema=( branch-name, loan-number ,amount)
Depositor-schema=(customer-name, account-number)
3.7 Modification of the Database
r2← ∏branch-name,account-number,balance(r1)
account←account-r2
r1← branch-city=“Needham”(account branch)
③ Delete all accounts at branches located in Needham
Account-schema=(branch-name, account-number, balance)
Branch-schema=(branch-name, branch-city,assets)
3.7 Modification of the Database
2. Insertion The attribute values for inserted tuples must be members of th
e attribute’s domain. The tuples inserted must be of the correct arity. An insertion is expressed by: r←r E∪ where r is a relation and E is a relational-algebra expression.
account←account {∪ (“Perryridge”,A-973,1200)}
depositor←depositor {∪ (“smith”,A-973)}
Example:
① Insert the fact that smith has $1200 in account A-937 at the Perryridge branch.
Account-schema=(branch-name, account-number, balance)
Depositor-schema=(customer-name, account-number)
3.7 Modification of the Database
② We want to provide as a gift for all loan customers of the Perryridge branch, a new $200 saving account. Let the loan number serve as the account number for this saving account.
r2 ←∏branch-name,loan-number(r1)
account←account∪(r2×{(200)})
depositor←depositor∪∏customer-name,loan-number(r1)
r1←(branch-name=“Perryridge”(borrow loan))
Account-schema=(branch-name, account-number, balance)
Depositor-schema=(customer-name, account-number) Loan-schema=( branch-name, loan-number ,amount)
Borrower-schema=(customer-name, loan-number)
3.7 Modification of the Database
3. Updating
If we want to select some tuples from r and to update only them, we can use the following expression; here p denotes the selection condition that chooses which tuples to update:r← ∏F1,F2…Fn(p(r)) (r∪ - p(r))
account ← ∏branch-name,account-number, balance*1.05(account)
Example:
① Suppose that interest payments are being made, and that all balances are to be increased by 5 percent
Account-schema=(branch-name, account-number, balance)
3.7 Modification of the Database
② Suppose that accounts with balances over $10,000 receive 6-percent interest, whereas all other receive 5 percent.
account ← ∏branch-name,amount-number,balance*1.06( account >
10000(account)) ∪ ∏branch-name,amount-number,balance*1.05( account≤10000(account))
Account-schema=(branch-name, account-number, balance)
3.8 View Definition
Any relation that is not part of the logical model but is made visible to a user as a virtual relation, is called a view.
1. View Definition We define a view using the create view statement. The form of
the create view statement is create view v as < query expression > where < query expression > is any legal relation-algebra query
expression. The view name is represented by v.
3.8 View Definition
Example:
① Assume that we wish this view to be called all-customer, we define this view as follows:
∏branch-name,customer-name(depositor account) ∪
∏branch-name,customer-name(borrower loan)
create view all-customer as
Account-schema=(branch-name, account-number, balance)
Depositor-schema=(customer-name, account-number)
Loan-schema=( branch-name, loan-number ,amount)
Borrower-schema=(customer-name, loan-number)
3.8 View Definition
Using the view all-customer, we can find all customers of the Perryridge branch by writing
∏customer-name( branch-name=“Perryridge”(all-customer))
View names may appear in any place that a relation name may appear, so long as no update operations are executed on the views.
3.8 View Definition
② View definition differs from the relational-algebra assignment operation. Define relation r1 as follows:
r1← ∏branch-name,customer-name(depositor account)
∪ ∏branch-name,customer-name(borrower loan)
At any given time, the set of tuples in the view relation is defined as the result of evaluation of the query expression that defines the view at that time.when a view is defined, the database system stores the definition of the view itself, rather than the result of evaluation of the relational-algebra expressions that defines the view. Wherever a view relation is used in a query, it is replaced by the stored query expression. Thus, whenever the query is evaluated, the view relation is recomputed.
3.8 View Definition
2. Updates Through View and Null Values
Although views are a useful tool for queries, they present significant problems if updates, insertions, or deletions are expressed with them. The difficulty is that a modification to the database expressed in terms of a view must be translated to a modification to the actual relations in the logical model of the database.
Problems:
① Consider a clerk who needs to see all loan data in the loan relation, except loan-amount, but branch-name be the view given to the clerk. Define this view as:
3.8 View Definition
This insertion must be represented by an insertion into the relation loan, since loan is the actual relation from which the view branch-loan is constructed. However, to insert a tuple into loan, we must have some value for amount.
create view branch-loan as
∏branch,loan-number(loan)
The clerk can write: branch-loan←branch-loan {(∪ “Perryridge”,L-37)}
ⅰ. Reject the insertion, and return an error message to the user
ⅱ. Insert a tuple (“Perryridge”, L-37, null) into the loan relation
Loan-schema=( branch-name, loan-number ,amount)
3.8 View Definition
② Consider another problem resulting from modification of the database through views:
∏customer-name,amount(borrower loan)
create view loan-info as
Consider the following insertion through this view:
loan-info←loan-info {(∪ “Johnson”,1900)}
Borrower (“Johnson”,null)
loan (null,null,1900)
Loan-schema=( branch-name, loan-number ,amount)
Borrower-schema=(customer-name, loan-number)
3.8 View Definition
3.Views Defined Using Other Views One view may be used in the expression defining another view.
Example:
we can define the view Perryridge-customer as follows:
Where all-customer is itself a view relation. A view relation v1 is said to depend directly on a view relation v2 if v2 is used in the expression defining v1.
create view perryridge-customer as
∏customer-name(branch-name=“Perryridge”(all-customer))
3.8 View Definition
View expansion is one way to define the meaning of views defined in terms of other views.
View expansion of an expression repeats the replacement step as follows:
Repeat
Find any view relation vi in e1
Replace the view relation vi by the expression defining vi
Until no more view relations are present in e1
3.8 View Definition
Example:
customer-name=“John”(Perryridge-customer)
customer-name=“John”(∏customer-name (branch-name=“Perryridge”(all-customer))
customer-name=“John”(∏customer-name (branch-name=“Perryridge”(∏bran
ch-name,customer-name(depositor account) ∪ ∏branch-name,custome
r-name(borrower loan))))
The view-expansion procedure initially generates
It the generates
Exercises:
① Relational Model:
Relation S(S#, SNAME, CITY) KEY=S#
Relation P(P#, PNAME, COLOR, WEIGHT) KEY=P#
Relation J(J#, JNAME, CITY) KEY=J#
Relation SPJ(S#, P#, J#, QTY) KEY=S# P# J#
Consider the relational model, for each of the following queries, given an expression in the relational algebra, the tuple-relational-calculus, the domain-relational-calculus and the SQL:
a. Find the suppliers’ numbers s# of all suppliers who offer the parts to both projects J1 and J2.
b. Find the suppliers’ numbers s# of all suppliers who offer the parts to any projects of either ShangHai or BeiJing.
Exercises:
c. Find the suppliers’ numbers s# of all suppliers of ShangHai who offer the parts to the projects of their own places.
d. Find the suppliers’ numbers s# of all suppliers of BeiJing who never offer the red parts.
e. Find the project numbers J# which use the parts offered by S1 at least.
f. Find the names of all suppliers who offer all parts.
g. Find the suppliers’ numbers s# of all suppliers who offer the all parts that S1 offered.
Exercises:
② Relational Model: Relation S(S#, SNAME, AGE, SEX) KEY=S# Relation C(C#,CNAME, TEACHER) KEY=C# Relation SC(S#, C#, SCORE) KEY=S# C# Consider the relational model, for each of the following queries,
given an expression in the relational algebra, the tuple-relational-calculus, the domain-relational-calculus and the SQL:
a. Find the numbers and scores of all students whose course number is K5.
b. Find the numbers and names of all students whose course number is K8
Exercises:
f. Find the names and ages of all students who have not chosen the K8 course.
e. Find the numbers of all students whose course number is K5 and K1.
g. Find the names of all students who have chosen all course.
h. Find the numbers of all students who have chosen the courses including all courses that NO.1 students has chosen
d. Find the numbers of all students whose course number is K5 or K1.
c. Find the numbers and names of all students whose course name is “C language”.
Exercises:
③ Transform the domain-relational-calculus expression {xy ︱ R(xy) (∧ z)(¬ S(xy) ∧ ¬ S(yz))} equivalent to the following expressions:
a. English query sentence.
b. Tuple-relational-calculus expression
c. Relational algebra expression
④ Suppose relation R and S are binary relation, transform the tuple-relational-calculus expression {t ︱ R(t) ∧ (u)(S(u)
u[1]=t[2])} equivalent to the following expressions:∧a. English query sentence.
b. Relational algebra expression
c. Domain–relational-calculus expression
Exercises:
⑤ Suppose relation R is a relation of three attributes and S is a relation of two attributes, transform the relational algebra expression
∏[1][5]([2]=[4] [3]=[4]∨ (R×S)) equivalent to the following expressions
a. English query sentence.
b. Tuple-relational-calculus expression
c. Domain–relational-calculus expression