Beruflich Dokumente
Kultur Dokumente
attributes
(or columns)
customer-name customer-street customer-city
customer
Relation r A B C D
1 7
5 7
12 3
23 10
1 7
23 10
Notation: p(r)
p is called the selection predicate
Defined as:
p(r) = {t | t r and 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: =, , >, . <.
Example of selection:
branch-name=Perryridge(account)
Relation r: A B C
10 1
20 1
30 1
40 2
A,C (r) A C A C
1 1
1 = 1
1 2
2
Relations r, s:
A B A B
1 2
2 3
1 s
r
r s: A B
1
2
1
3
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column
of r deals with the same type of values as does the 2nd
column of s)
E.g. to find all customers with either an account or a loan
customer-name (depositor) customer-name (borrower)
Relations r, s:
A B A B
1 2
2 3
1 s
r
r s: A B
1
1
r s
rs A B
Relations r, s: A B C D E
1 10 a
10 a
2
20 b
r 10 b
s
r x s:
A B C D E
1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
1 10 a
2 20 a
2 20 b
Database System Concepts 3.60 Silberschatz, Korth and Sudarshan
Join or Theta Join
Selection over a cartesian product
R BS B (RxS)
Meaning:
For every row r of R
output all rows s of S
which satisfy condition B.
Relations r, s:
A B C D B D E
1 a 1 a
2 a 3 a
4 b 1 a
1 a 2 b
2 b 3 b
r s
r s
A B C D E
1 a
1 a
1 a
1 a
2 b
rs
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)
r s = { t | t R-S(r) u s ( tu r ) }
Relations r, s: A B B
1 1
2
3 2
1 s
1
1
3
4
6
1
2
r s: A r
Relations r, s:
A B C D E D E
a a 1 a 1
a a 1 b 1
a b 1 s
a a 1
a b 3
a a 1
a b 1
a b 1
r
r s: A B C
a
a
Property
Let q = r s
Then q is the largest relation satisfying q x s r
Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S R
To see why
R-S,S(r) simply reorders attributes of r
Find the names of all customers who have a loan, an account, or both, from
the bank
Find the names of all customers who have a loan and an account at
bank.
customer-name (branch-name=Perryridge
(borrower.loan-number = loan.loan-number(borrower x loan)))
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.
Query 2
customer-name(loan.loan-number = borrower.loan-number(
(branch-name = Perryridge(loan)) x borrower))
balance(account) - account.balance
(account.balance < d.balance (account x d (account)))
Second term is all those accounts which are smaller than some
account
CN(BN=Downtown(depositor account))
CN(BN=Uptown(depositor account))
Generalized Projection
Outer Join
Aggregate Functions
7
7
3
10
sum-C
g sum(c) (r)
27
Relation loan
Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
Inner Join
loan Borrower
loan-number branch-name amount customer-name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
Operation Purpose
Notation
SELECT Selects all tuples that satisfy the selection < selection condition> (R)
condition from a relative R.
PROJECT Produces a new relation with only some < attribute list > (R)
of the attributes of R, and removes
duplicate tuples.
THETA JOIN Produces all combinations of tuples from R1 < join condition > R2
R1 and R2 that satisfy the join condition.
EQUIJOIN Produces all the combinations of tuples R1 < join condition > R2, or
from R1 and R2 that satisfy a join R1 (< join attributes 1>),
condition with only equality (<join attributes 2>R2
comparisons.
NATURAL JOIN Same as equijoin except that the join R1 < join condition > R2, or
attributes of R2 are not included in the R1 (< join attributes 1>),
resulting relation; (<join attributes 2>)
R2 or R1*R2
R1 R2
DIFFERENCE Produces a relation that includes all the
tuples in R1 that are not in R2;
.