database systems
DESCRIPTION
Database Systems. Major Content & Grade. Introduction* The Relational Model *** SQL**** Transaction Management*** Database Design (E-R)*** Database Design (Normalization)***. Unit 2 The Relational Model. Relational Model Relational Algebra( 关系代数) - PowerPoint PPT PresentationTRANSCRIPT
Database SystemsDatabase Systems
DataBase System
Haichang Gao , Software School , Xidian University 2
Major Content & GradeMajor Content & Grade Introduction * The Relational Model
*** SQL **** Transaction Management *** Database Design (E-R) *** Database Design (Normalization) ***
DataBase System
Haichang Gao , Software School , Xidian University 3
Unit 2 Unit 2 The Relational ModelThe Relational ModelRelational ModelRelational Algebra(关系代数 )Relational Calculus(关系演算 )
DataBase System
Haichang Gao , Software School , Xidian University 4
Relational ModelRelational ModelLaid down in 1969-1970 by E.F.CODD
“ A Relational Model of Data for Large Shared Data Bank” C ACM 1970
Mathematical Bases ( Relational Theory) Developed in 1980s Most commercial DBMS are Relational
DataBase System
Haichang Gao , Software School , Xidian University 5
Mathematical RelationsMathematical Relations Formally, given sets D1, D2, …. Dn, a relation r is a subset of
Cartesian product(笛卡尔积 ) D1 × D2 × … × Dn
Thus a relation( 关系 ) is a set of n-tuples (a1, a2, …, an) where ai Di
Example: if
customer-name = {Jones, Smith, Curry, Lindsay} customer-street = {Main, North, Park} customer-city = {Harrison, Rye, Pittsfield}
Then r = {(Jones, Main, Harrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)}
is a relation over customer-name × customer-street × customer-city.
DataBase System
Haichang Gao , Software School , Xidian University 6
Mathematical RelationsMathematical Relations Each attribute of a relation has a name
The set of allowed values for each attribute is called the domain (域) of the attribute
Attribute values are (normally) required to be atomic; that is, indivisible Note: multivalued attribute( 多值属性 ) values are not
atomic Note: composite attribute ( 组合属性 ) values are not atomic
The special value null is a member of every domain The null value causes complications in the definition of
many operations
DataBase System
Haichang Gao , Software School , Xidian University 7
Mathematical RelationsMathematical Relations A1, A2, …, An are attributes
R = (A1, A2, …, An ) is a relation schema(关系模式 )
E.g. Customer-schema = (customer-name, customer-street, customer-city)
r(R) is a relation on the relation schema R
E.g. customer (Customer-schema)
DataBase System
Haichang Gao , Software School , Xidian University 8
Mathematical RelationsMathematical Relations The current values (relation instance, 关系实例 ) of a
relation are specified by a table An element t of r is a tuple(元组 ), represented by a row in a
table Order of tuples is irrelevant ( 元组是无序的 tuples may be
stored in an arbitrary order)
JonesSmithCurryLindsay
customer_name
MainNorthNorthPark
customer_street
HarrisonRyeRyePittsfield
customer_city
customer
attributes(or columns)
tuples(or rows)
DataBase System
Haichang Gao , Software School , Xidian University 9
Relational DatabaseRelational Database A Relational database consists of multiple relations Information about an enterprise is broken up into parts, with
each relation storing one part of the information Example:
The customer table stores information about customers
DataBase System
Haichang Gao , Software School , Xidian University 10
Relational DatabaseRelational Database
(b) The account table(c) The depositor table
The account table stores information about accounts.
The depositor table containing information about which customer owns which account
Storing all information as a single relation is NOT a good
idea!
DataBase System
Haichang Gao , Software School , Xidian University 11
KeyKey Let K R K is a superkey(超码 ) of R if values for K are sufficient to
identify a unique tuple of each possible relation r(R).
Example: {customer_id, customer_name} and {customer_id} are both superkeys of Customer.
by “possible r” we mean a relation r that could exist in the enterprise we are modeling.
DataBase System
Haichang Gao , Software School , Xidian University 12
KeyKey Let K R K is a superkey(超码 ) of R if values for K are sufficient to
identify a unique tuple of each possible relation r(R). K is a candidate key(候选码 ) if K is minimal.
Example: {customer_id} is a candidate key for Customer, since it is a superkey, and no subset of it is a superkey.
{customer_name} is a candidate key also, assuming no two customers can possibly have the same name.
DataBase System
Haichang Gao , Software School , Xidian University 13
KeyKey Primary key(主码 ): The candidate key that is selected to
identify tuples uniquely within the relation. And the others candidate key is called Alternate Keys(替换码 ). Example: for relation
customer (customer_id, customer_name, customer_street, customer_city) Candidate key : {customer_id}, {customer_name} Primary key: {customer_id}
DataBase System
Haichang Gao , Software School , Xidian University 14
KeyKey For the banking database, we have several relations to store data:
Foreign Key(外码 ): An attribute, or set of attributes,within one relation R1 that matches the candidate key of some(possibly the same) relation R2. Example: Attribute customer_name in table depositor is a
Foreign Key references to customer_name in customer.
DataBase System
Haichang Gao , Software School , Xidian University 15
KeyKey Usually, Primary Key and Foreign Key was indicated using
undirected line and dashed line. Example:
Find out PK and FK of the following schema:
Student(sno, sname, ssex, sbirth, sdept)
Course(cno, cname, cpno, ccredit)
SC(sno, cno, grade)
DataBase System
Haichang Gao , Software School , Xidian University 16
Integrity of Relational ModelIntegrity of Relational Model Entity integrity(实体完整性约束 ): In a base relation, NO
attribute of a primary key can be null.
Referential integrity(参照完整性约束 ): If a foreign key exists in a relation, either the foreign key value must match a candidate key value of some tuple in its home relation(被参照关系 ) or the foreign key value must be wholly null. Example:
customer (customer_name, customer_street, customer_city)
depositor (customer_name, account_number)
account (account_number, branch_name, balance)
DataBase System
Haichang Gao , Software School , Xidian University 17
Integrity of Relational ModelIntegrity of Relational Model Null(空值 ): Represents a value for an attribute that is
currently unknown or is not applicable for this tuple.
It is possible for tuples to have a null value, denoted by null,
for some of their attributes
The result of any arithmetic expression involving null is null.
Aggregate functions simply ignore null values (as in SQL)
For duplicate elimination and grouping, null is treated like
any other value, and two nulls are assumed to be the same
(as in SQL)
Null value is NOT 0 of integer, NOT empty string , it is
JUST NULL.
DataBase System
Haichang Gao , Software School , Xidian University 18
Integrity of Relational ModelIntegrity of Relational Model Comparisons with null values return the special truth value:
unknown If false was used instead of unknown, then not (A < 5)
would not be equivalent to A >= 5 Three-valued logic using the truth value unknown:
OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown
AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown
NOT: (not unknown) = unknown Result of select predicate is treated as false if it evaluates to
unknown
DataBase System
Haichang Gao , Software School , Xidian University 19
Unit 2 Unit 2 The Relational ModelThe Relational ModelRelational ModelRelational Algebra(关系代数 )Relational Calculus(关系演算 )
DataBase System
Haichang Gao , Software School , Xidian University 20
Manipulation of Relational ModelManipulation of Relational Model Query Language is a Language in which user requests
information from the database. Categories of languages
Procedural( 过程化的 ) Non-procedural, or declarative( 非过程化的,声明性的 )
Mathematical languages: Relational algebra( 关系代数 ) Tuple relational calculus( 元组演算 ) Domain relational calculus ( 域演算 )
Characteristics of Relational languages all set-at-a-time ( 一次一集合 )
operands & results are whole tables Closure property (封闭性)
the output from one operation can become input to another.
DataBase System
Haichang Gao , Software School , Xidian University 21
Relational AlgebraRelational Algebra Relational algebra is a Procedural language
Basic operators select( 选择 ): project( 投影 ): π union( 并 ): ∪ set difference( 集合差 ): – Cartesian product( 笛卡尔积 ): × rename:
The operators take two or more relations as inputs and give a new relation as a result.
DataBase System
Haichang Gao , Software School , Xidian University 22
Basic operatorsBasic operators Select Operation(选择操作 )
Notation: p(r)
p is called the selection predicate( 条件谓词 ) Defined as:
p(r) = {t | t r p(t)}
Where p is a formula in propositional calculus (命题演算 ) consisting of terms connected by : (and), (or), (not)Each term is one of:
<attribute> op <attribute> (or <constant>)
where op is one of: =, , >, , <, (comparison operator)
Example of selection: branch_name=“Perryridge”(account)
DataBase System
Haichang Gao , Software School , Xidian University 23
Basic operatorsBasic operators
• Relation r A B C D
15
1223
773
10
A=B D > ‘5’ (r)A B C D
123
710
Select Operation(选择操作 )
DataBase System
Haichang Gao , Software School , Xidian University 24
Basic operatorsBasic operators Project Operation(投影操作 )
Notation: π A1, A2, …, Ak (r)
where A1, A2 are attribute names and r is a relation name.
The result is defined as the relation of k columns obtained by erasing the columns that are not listed
Duplicate rows removed from result, since relations are sets Example: For relation
account (account_number, branch_name, balance)
To eliminate the branch_name attribute of account
π account_number, balance (account)
DataBase System
Haichang Gao , Software School , Xidian University 25
Basic operatorsBasic operators Project Operation(投影操作 )
Relation r: A B C
10203040
1112
A C
1112
=
A C
112
π A,C (r)
DataBase System
Haichang Gao , Software School , Xidian University 26
Basic operatorsBasic operators Union Operation(并操作 )
Notation: r s r union s Defined as:
r s = {t | t r t s} For r s to be valid:
1. r, s must have the same degree( 列数相同 )
2. The attribute domains must be compatible ( 域相容 ) Example: find all customers with either an account or a loan
π customer_name (depositor) π customer_name (borrower)
DataBase System
Haichang Gao , Software School , Xidian University 27
Basic operatorsBasic operators Union Operation(并操作 )
Relations r, s:
r s:
A B
121
r
A B
23
s
A B
1213
The 1st column of r deals with the same type of values(domain) as does the 1st column of s ,and 2nd column also, and also for all attributes.
DataBase System
Haichang Gao , Software School , Xidian University 28
Basic operatorsBasic operators Difference Operation(差操作 )
Notation: r – s r minus s Defined as:
r – s = {t | t r t s} Set differences must be taken between compatible relations.
r and s must have the same degree attribute domains of r and s must be compatible
DataBase System
Haichang Gao , Software School , Xidian University 29
Basic operatorsBasic operators Difference Operation (差操作 )
Relations r, s: A B
121
r
A B
23
s
r – s : A B
11
DataBase System
Haichang Gao , Software School , Xidian University 30
Basic operatorsBasic operators Cartesian-Product Operation(笛卡尔积操作 )
Notation: r × s r times s Defined as:
r × s = {tq | t r q s} Assume that attributes of r(R) and s(S) are disjoint. (That is,
R S = , NO same attribute in R and S). If attributes of r(R) and s(S) are not disjoint, then renaming
must be used.
DataBase System
Haichang Gao , Software School , Xidian University 31
Basic operatorsBasic operators Cartesian-Product Operation(笛卡尔积操作 )
Relations r, s:
r × s:A B
11112222
C D
1019201010102010
E
aabbaabb
A B
12
r
C D
10102010
E
aabb
s
DataBase System
Haichang Gao , Software School , Xidian University 32
Basic operatorsBasic operators Rename Operation(重命名操作 )
Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
Allows us to refer to a relation by more than one name. Example:
x (E)
returns the expression E under the name x If a relational-algebra expression E has degree n, then
x (A1, A2, …, An) (E)
A1, A2, …., An returns the result of expression E under the name x, and with the attributes renamed to A1, A2, …., An.
DataBase System
Haichang Gao , Software School , Xidian University
33
Example QueriesExample Queries Find the largest account balance.
account_number
Branch_name
balance
A-101 Downtown 500
A-215 Mianus 700
A-102 Perryridge 400
… …… …
account
A1.balance >A2.balance (A1(account) × A2(account) ) ?
πA1.balance (A1.balance < A2.balance (A1(account) × A2(account) ) )πbalance(account) –
最小值不被选中 !
Database SystemsDatabase Systems
DataBase System
Haichang Gao , Software School , Xidian University 35
Unit 2 Unit 2 The Relational ModelThe Relational ModelRelational ModelRelational Algebra(关系代数 )Relational Calculus(关系演算 )
DataBase System
Haichang Gao , Software School , Xidian University 36
Relational Algebra expressionRelational Algebra expression A basic expression in the relational algebra consists of either
one of the following: A relation in the database A constant relation
Let E1 and E2 be relational-algebra expressions; the
following are all relational-algebra expressions: E1 E2
E1 – E2
E1 × E2
p(E1), P is a predicate on attributes in E1
πs(E1), S is a list consisting of some of the attributes in E1
x(E1), x is the new name for the result of E1
DataBase System
Haichang Gao , Software School , Xidian University 37
Example QueriesExample Queries Banking DataBase:
branch (branch_name, branch_city, assets)customer (customer_name, customer_street, customer_city)depositor (customer_name, account_number)account (account_number, branch_name, balance)borrower (customer_name, loan_number)loan (loan_number, branch_name, amount)
DataBase System
Haichang Gao , Software School , Xidian University 38
Example QueriesExample Queries Find the loan number for each loan of an amount greater than
$1200. loan_number
branch_name amount
L-11 Round Hill 900L-14 Downtown 1500L-15 Perryridge 1500L-16 Perryridge 1300L-17 Downtown 1000L-23 Redwood 2000L-93 Mianus 500
loanamount > ‘1200’ (loan)
loan_number
L-14L-15L-16L-23
注:除特别要求外,写查询表达式时不需要写出结果集!
πloan_number ( )
DataBase System
Haichang Gao , Software School , Xidian University 39
Example QueriesExample Queries
Find the customers name who have at least one deposit of a balance greater than $700.
account_number
branch_name
balance
A-101 Downtown 500A-215 Mianus 700A-102 Perryridge 400A-305 Round Hill 350A-201 Brighton 900A-222 Redwood 700A-217 Brighton 750
account
customer_name
account_number
Hayes A-102Johnson A-101Johnson A-201Jones A-217Lindsay A-222Smith A-215Turner A-305
depositor
balance>’700’(account)π account_number( )×depositor
πCustomer_name(account.account_number = depositor.account_number (
) )πCustomer_name(account.account_number = depositor.account_number balance>’700’ (
account ×depositor ) )
A1:
A2:
DataBase System
Haichang Gao , Software School , Xidian University 40
Example QueriesExample Queries
Find all customers who have at least two deposits.
customer_name
account_number
Hayes A-102Johnson A-101Johnson A-201Jones A-217Lindsay A-222Smith A-215Turner A-305
depositor
D1.cname D1.a# D2.cname D2.a#Hayes A-102 Hayes A-102Hayes A-102 Johnson A-101Hayes A-102 Johnson A-201
…… …… …… ……Johnson A-101 Johnson A-201
…… …… …… ……Johnson A-201 Johnson A-101
…… …… …… ……
D1(cname,a#)(depositor) × D2(cname,a#)( depositor)
πD1.cname(D1.cname=D2.cname D1.a#<>D2.a#∧ (
) )
DataBase System
Haichang Gao , Software School , Xidian University 41
Example QueriesExample Queries Find the largest account balance.
account_number
Branch_name
balance
A-101 Downtown 500
A-215 Mianus 700
A-102 Perryridge 400
… …… …
accountA.ano A.bn A.b B.ano B.bn B.bA-101 Downtown 500 A-101 Downtown 500A-101 Downtown 500 A-215 Mianus 700A-101 Downtown 500 A-102 Perryridge 400A-215 Mianus 700 A-101 Downtown 500A-215 Mianus 700 A-215 Mianus 700A-215 Mianus 700 A-102 Perryridge 400A-102 Perryridge 400 A-101 Downtown 500A-102 Perryridge 400 A-215 Mianus 700A-102 Perryridge 400 A-102 Perryridge 400
… ……. … … …… …
A.b > B.b (A × B)?
πA1.balance (A1.balance < A2.balance (A1(account) × A2(account) ) )πbalance(account) –
A.b < B.b (A × B)?
DataBase System
Haichang Gao , Software School , Xidian University 42
Additional OperationsAdditional Operations We define additional operations that do not add any
power to the relational algebra, but that simplify common queries.
Set intersection( 集合交 ) Natural join( 自然连接 ) Division( 除法 )
DataBase System
Haichang Gao , Software School , Xidian University 43
Additional OperationsAdditional Operations Intersection Operation(交操作 )
Notation: r s Assume:
r, s have the same degree attributes of r and s are compatible
Defined as:
r s ={ t | t r t s } Note: r s = r - (r - s)
πcustomer_name (borrower ) πcustomer_name (depositor )
Find the names of all customers who have a loan and an account at bank.
DataBase System
Haichang Gao , Software School , Xidian University 44
Additional OperationsAdditional Operations -Join Operation(-连接 )
-Join is a general join which don’t need there are same attribute for both operators, and the join condition can be any compare operations. That is:
R times S WHERE F
R S = { | tr R ts S tr[A] ts[B] } AθB
tr ts
Equi-join is another join. Its join condition is equal operator.
R S = { | tr R ts S tr[A]= ts[B] } A=B
tr ts
DataBase System
Haichang Gao , Software School , Xidian University 45
Additional OperationsAdditional Operations -Join Operation(-连接 )
Relations r, s:RA B C
a1 b15
a1 b26
a2 b38
a2 b412
SB E
b13
b27
b310
b32
b52
R SB = B
A R.B C S.B E
a1 b1 5 b1 3
a1 b2 6 b2 7
a2 b3 8 b3 10
a2 b3 8 b3 2
R SC < E
ES.BCR.BA
10b35b1a1
7b25b1a1
10b38b3a2
10b36b2a1
7b26b2a1
DataBase System
Haichang Gao , Software School , Xidian University 46
Additional OperationsAdditional Operations Natural join Operation(自然连接 )
Notation: r s Let r and s be relations on schemas R and S respectively.The
result is a relation on schema R S which is obtained by considering each pair of tuples tr from r and ts from s.
If tr and ts have the same value on each of the attributes in RS, a tuple t is added to the result, where t has the same value as tr on r t has the same value as ts on s
Example:R = (A, B, C, D) S = (E, B, D)
Result schema = (A, B, C, D, E) r s is defined as:π r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r × s))
DataBase System
Haichang Gao , Software School , Xidian University 47
Additional OperationsAdditional Operations Natural join Operation(自然连接 )
Relations r, s:
A B
12412
C D
aabab
r
A B C D E
B
13123
D
aaabb
E
s
r s
1 a 1 a 1 a 1 a 2 b
DataBase System
Haichang Gao , Software School , Xidian University 48
Example QueriesExample QueriesAdditional Operations simplify the relational
algebra expressions !
balance>’700’(account)π account_number( )×depositor
πCustomer_name(account.account_number = depositor.account_number (
) )
A1:
πCustomer_name(account.account_number = depositor.account_number balance>’700’ (
account ×depositor ) )
A2:
Find the customers name who have at least one deposit of a balance greater than $700.
account depositorA3: πCustomer_name(balance>’700’ ( ) )
DataBase System
Haichang Gao , Software School , Xidian University 49
Example QueriesExample Queries
(exercise)Find all customers who have at least two deposits in different branch.
customer_name
account_number
Hayes A-102Johnson A-101Johnson A-201Jones A-217Lindsay A-222Smith A-215Turner A-305
depositor
account_number
branch_name
balance
A-101 Downtown 500A-215 Mianus 700A-102 Perryridge 400A-305 Round Hill 350A-201 Brighton 900A-222 Redwood 700A-217 Brighton 750
account
πD1.Customer_name( D1.Customer_name =D2. Customer_name
D1. Account_number<>D2. Account_number D1.branch_name<>D2. branch_name(
)
× D2(depositor account)D1(depositor account)
DataBase System
Haichang Gao , Software School , Xidian University 50
Example QueriesExample Queries
(exercise)Find all customers who have at least two deposits in different branch.
customer_name
account_number
Hayes A-102Johnson A-101Johnson A-201Jones A-217Lindsay A-222Smith A-215Turner A-305
depositor
account_number
branch_name
balance
A-101 Downtown 500A-215 Mianus 700A-102 Perryridge 400A-305 Round Hill 350A-201 Brighton 900A-222 Redwood 700A-217 Brighton 750
account
depositor.account_number=account.account_number(depositor × account))D1(cname,a#,bname)(πdepositor.customer_name,account_number,branch_name
D2(cname,a#,bname)(πdepositor.customer_name,account_number,branch_name
depositor.account_number=account.account_number(depositor × account)) ×
πD1.cname(D1.cname=D2.cname D1.a#<>D2.a# D1.bname<>D2.bname(
)
DataBase System
Haichang Gao , Software School , Xidian University 51
Additional OperationsAdditional Operations Division Operation(除 )
Suited to queries that include the phrase “for all”. Let r and s be relations on schemas R and S respectively
where
R = (A1, …, Am, B1, …, Bn)
S = (B1, …, Bn)
The result of r s is a relation on schema
R S = (A1, …, Am)
Defined as:
r s = { t | t π R-S(r) u s → tu r ) }
DataBase System
Haichang Gao , Software School , Xidian University 52
Additional OperationsAdditional Operations Division Operation(除 )
Relations r, s:
r s:
B
A
12
A B
12311134612
r
s
DataBase System
Haichang Gao , Software School , Xidian University 53
Additional OperationsAdditional Operations Division Operation(除 )
Another Defined as:
)}(|][{ SYRtXtSR Yxrr
Yx={y | tr R ∈ y=tr[Y] x=tr[X]}
A B
aaaaaaaa
C D
aabababb
E
11113111
D
ab
E
11
r sr s:
A B C
Relations r, s:
a a
DataBase System
Haichang Gao , Software School , Xidian University 54
Additional OperationsAdditional Operations Division Operation(除 )
Property Let q is the result of r s Then q is the largest relation satisfying q × s r
Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S R
r s = π R-S (r) –πR-S ( (π R-S (r) × s) – πR-S,S(r))
DataBase System
Haichang Gao , Software School , Xidian University 55
Example QueriesExample Queries Find all customers who have an account at all branches
located in Brooklyn city.
branch_name branch_city assets
Brighton Brooklyn 7100000Downtown Brooklyn 9000000Mianus Horseneck 400000North Town Rye 3700000Perryridge Horseneck 1700000Pownal Bennington 300000Redwood PaloAlto 2100000Round Hill Horseneck 8000000
branch
πbranch_name (branch_city = “Brooklyn” (branch))
account_number
branch_name
balance
A-101 Downtown 500A-215 Mianus 700A-102 Perryridge 400A-305 Round Hill 350A-201 Brighton 900A-222 Redwood 700A-217 Brighton 750
account
customer_name
JohnsonSmithHayesTurnerJohnsonLindsayJones
π customer_name, branch_name (depositor account)
DataBase System
Haichang Gao , Software School , Xidian University 56
Extended OperationsExtended Operations Outer join Operation(外连接 )
The (left) Outer join is a join on which tuples from R that do not have matching values in the common attributes of S are alse included in the result relation. Missing values in the second relation are set to null.
A B
12
s
A C
10102010
D
aabb
r
A C D B
10 a 1 10 a 2 20 b 2 10 b
r s
Notation: r s (left outer join )
DataBase System
Haichang Gao , Software School , Xidian University 57
Extended OperationsExtended Operationsloan_numbe
rbranch_name amount
L-170 Downtown 3000L-230 Redwood 4000L-260 Perryridge 1700
loan
customer_name loan_number
Jones L-170Smith L-230Hayes L-155
borrower
loan_number branch_Name amount customer_nameL-170 Downtown 3000 JonesL-230 Redwood 4000 Smith
Inner Join: loan borrower
Left Outer Join : loan borrower
loan_number branch_Name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 NULL
DataBase System
Haichang Gao , Software School , Xidian University 58
Extended OperationsExtended Operationsloan_numbe
rbranch_name amount
L-170 Downtown 3000L-230 Redwood 4000L-260 Perryridge 1700
loan
customer_name Loan_number
Jones L-170Smith L-230Hayes L-155
borrower
loan_number nranch_name amount customer_nameL-170 Downtown 3000 JonesL-230 Redwood 4000 SmithL-155 Hayes
loan_number branch_name amount customer_nameL-170 Downtown 3000 JonesL-230 Redwood 4000 SmithL-260 Perryridge 1700L-155 Hayes
Right Outer Join : loan borrower
Full Outer Join : loan borrower
DataBase System
Haichang Gao , Software School , Xidian University 59
Homework 1Homework 1 2.1
2.5
Database SystemsDatabase Systems
DataBase System
Haichang Gao , Software School , Xidian University 61
Unit 2 Unit 2 The Relational ModelThe Relational ModelOverviewRelational ModelRelational Algebra(关系代数 )Relational Calculus(关系演算 )
(教材 5.1 节 )
DataBase System
Haichang Gao , Software School , Xidian University 62
Relational calculusRelational calculus Relational Calculus Based on a branch of mathematical
logical called the predicate 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, called component of t on attribute A R(t) denotes that tuple t is in relation R P is a formula of the predicate calculus
DataBase System
Haichang Gao , Software School , Xidian University 63
Relational calculusRelational calculus Tuple-relational-Calculus Formula
R(t) is an atom formula(原子公式 ) <t[A] θ t[B]> (or <t[A] θ c> or <c θ t[A] > ) is an atom
formula
θ is one of the comparison operators: (, , , , , ) if P1, P2 are formulae, then
P1, P1 P2, P1 P2, P1 P2 are formulae x(P(x)), x (P(x)) are formulae
Sets of equivalent expressions: P1 P2 (P1 P2 )
P1 P2 P1 P2
t ( P(t) ) t ( P(t) )
DataBase System
Haichang Gao , Software School , Xidian University 64
Relational calculusRelational calculus Example
987
654
321
CBA
R
965
643
321
CBA
S R1={ t | S(t) t[1]> ‘2’ }
CBA
965
643
R2={ t | R(t) S(t)}
A B C
4 5 6
7 8 9
R3={ t | S(t) u(R(u) t[3]<u[2]) }
A B C
1 2 3
3 4 6
DataBase System
Haichang Gao , Software School , Xidian University 65
Relational calculusRelational calculus Example
987
654
321
CBA
R
965
643
321
CBA
S R4={ t | R(t) u(S(u)t[3]>u[1]) }
A B C
4 5 6
7 8 9
DataBase System
Haichang Gao , Software School , Xidian University 66
Relational calculusRelational calculus Example
987
654
321
CBA
R
965
643
321
CBA
S R4={ t | R(t) u(S(u)t[3]>u[1]) }
A B C
4 5 6
7 8 9
R5={ t | u v( R(u) S(v) u[1]>v[2] t[1]=u[2]
t[2]=v[3] t[3]=u[1] )}
Process:1. Ensure the schema of result;2. Obtain tuples according the
predicate calculus .
R.AS.CR.B
435
798
768
738
DataBase System
Haichang Gao , Software School , Xidian University 67
Example QueriesExample Queries Banking DataBase:
branch (branch_name, branch_city, assets)customer (customer_name, customer_street, customer_city)depositor (customer_name, account_number)account (account_number, branch_name, balance)borrower (customer_name, loan_number)loan (loan_number, branch_name, amount)
DataBase System
Haichang Gao , Software School , Xidian University 68
Example QueriesExample Queries Find the loan_number, branch_name, and amount for loans of
over $1200. loan_number
branch_name amount
L-11 Round Hill 900L-14 Downtown 1500L-15 Perryridge 1500L-16 Perryridge 1300L-17 Downtown 1000L-23 Redwood 2000L-93 Mianus 500
loan{t | loan(t) t[amount] 1200}
Find the loan number for each loan of an amount greater than $1200.
{t | s( loan(s) s[amount ] 1200 t [loan_number] = s [loan_number ] )}
DataBase System
Haichang Gao , Software School , Xidian University 69
Example QueriesExample Queries
Find the customers name who have at least one deposit of a balance greater than $700.
account_number
branch_name
balance
A-101 Downtown 500A-215 Mianus 700A-102 Perryridge 400A-305 Round Hill 350A-201 Brighton 900A-222 Redwood 700A-217 Brighton 750
account
customer_name
account_number
Hayes A-102Johnson A-101Johnson A-201Jones A-217Lindsay A-222Smith A-215Turner A-305
depositor
{t | u( depositor(u) v( account(v) u[account_number ]
= v[account_number] v[balance ] > ‘700’ )
t[customer_name] = u[customer_name ] ) }
DataBase System
Haichang Gao , Software School , Xidian University 70
Example QueriesExample Queries
Find all customers who have at least two deposits.
customer_name
account_number
Hayes A-102Johnson A-101Johnson A-201Jones A-217Lindsay A-222Smith A-215Turner A-305
depositor
customer_name
account_number
Hayes A-102Johnson A-101Johnson A-201Jones A-217Lindsay A-222Smith A-215Turner A-305
depositor
{t | u v( depositor(u) depositor(v) u[customer_name ] =
v[customer_name] u[account_number] v [account_number]
t[customer_name] = u [customer_name ] ) }
u v
DataBase System
Haichang Gao , Software School , Xidian University 71
Example QueriesExample Queries Find the largest account balance.
account_number
branch_name
balance
A-101 Downtown 500
A-215 Mianus 700
A-102 Perryridge 400
… …… …
account
account_number
branch_name
balance
A-101 Downtown 500
A-215 Mianus 700
A-102 Perryridge 400
… …… …
account
u v
{t | u ( account(u) v( account(v) u[balance] ≥
v[balance]) t[balance] = u [balance] ) }
A1:
A2: {t | u ( account(u) v( account(v) u[balance]
v[balance]) t[balance] = u [balance] ) }
x( P(x) Q(x) )
x(P(x) Q(x) )
DataBase System
Haichang Gao , Software School , Xidian University 72
Example QueriesExample Queries
(exercise)Find all customers who have at least two deposits in different branch.
customer_name
account_number
Hayes A-102Johnson A-101Johnson A-201Jones A-217Lindsay A-222Smith A-215Turner A-305
depositor
account_number
branch_name
balance
A-101 Downtown 500A-215 Mianus 700A-102 Perryridge 400A-305 Round Hill 350A-201 Brighton 900A-222 Redwood 700A-217 Brighton 750
account
{t | u x ( depositor(u) account(x) u[account_number] =
x[account_number] v y ( depositor(v) account(y)
v[account_number] = y[account_number] u[customer_name] =
v[customer_name] x[branch_name] y[branch_name]
u[account_number] v[account_number] ) t[customer_name] =
u [customer_name] ) }
u
v
x
y
DataBase System
Haichang Gao , Software School , Xidian University 73
Example QueriesExample Queries Find all customers who have an account at all branches
located in Brooklyn city.branch_name branch_city …
Brighton Brooklyn …Downtown Brooklyn …Mianus Horseneck …North Town Rye …Perryridge Horseneck …Pownal Bennington …Redwood PaloAlto …Round Hill Horseneck …
branch
account_number
branch_name
…
A-101 Downtown …A-215 Mianus …A-102 Perryridge …A-305 Round Hill …A-201 Brighton …A-222 Redwood …A-217 Brighton …
account{t | u ( depositor(u) v (branch(v) v[branch_city] = ‘Brooklyn’
w( account(w) w[account_number] = u[account_number]
w[branch_name] = v[branch_name]) ) t[customer_name] =
u[customer_name] ) }
customer_name
account_number
Hayes A-102Johnson A-101Johnson A-201Jones A-217Lindsay A-222Smith A-215Turner A-305
depositor
DataBase System
Haichang Gao , Software School , Xidian University 74
Example QueriesExample Queries Find all customers who have an account at all branches
located in Brooklyn city.
{t | u ( depositor(u) v (branch(v) v[branch_city] = ‘Brooklyn’
w( account(w) w[account_number] = u[account_number]
w[branch_name] = v[branch_name]) ) t[customer_name] = u
[customer_name] ) }
等价转换
x( P(x) Q(x) )
x(P(x) Q(x) )
{t | u ( depositor(u) v (branch(v) v[branch_city] = ‘Brooklyn’
w( account(w) w[account_number] = u[account_number]
w[branch_name] = v[branch_name]) ) t[customer_name] =
u[customer_name] ) }
DataBase System
Haichang Gao , Software School , Xidian University 75
Example QueriesExample Queries Find the names of all customers having a loan from the
Perryridge branch, and the cities in which they live.
Banking DataBase:branch (branch_name, branch_city, assets)customer (customer_name, customer_street,
customer_city)depositor (customer_name, account_number)account (account_number, branch_name, balance)borrower (customer_name, loan_number)loan (loan_number, branch_name, amount)
{t | u v w (customer(u) borrower(v) loan(w) u[customer_name]
= v[customer_name] v[loan_number] = w[loan_number]
w[branch_name] = ‘Perryridge’ t[customer_name] =
u[customer_name] t[customer_city] = u[customer_city] ) }
DataBase System
Haichang Gao , Software School , Xidian University 76
Safety of ExpressionsSafety of ExpressionsIt is possible to write tuple calculus expressions
that generate infinite(无穷的 ) relations. For example, {t | R(t)} 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
IN Relational Algebra Operations we defined is safety.
DataBase System
Haichang Gao , Software School , Xidian University 77
Homework 2Homework 2 5.1 5.6