Relational Model: - Example: If
Relational Model: - Example: If
Chapter
• Structure of Relational Databases
• Fundamental Relational-Algebra-Operations
Relational Model
• Additional Relational-Algebra-Operations
• Extended Relational-Algebra-Operations
• Null Values
1
Relation Schema Relation Instance
2
Keys Query Languages
• Let K ⊆ R
• Language in which user requests information from the
• K is a superkey of R if values for K are sufficient to identify a
unique tuple of each possible relation r(R) database.
– by “possible r ” we mean a relation r that could exist in the • Categories of languages
enterprise we are modeling.
– Procedural
– Example: {customer_name, customer_street} and
{customer_name} – Non-procedural, or declarative
are both superkeys of Customer, if no two customers can • “Pure” languages:
possibly have the same name.
– Relational algebra
• K is a candidate key if K is minimal
Example: {customer_name} is a candidate key for Customer, – Tuple relational calculus
since it is a superkey (assuming no two customers can – Domain relational calculus
possibly have the same name), and no subset of it is a
superkey. • Pure languages form underlying basis of query languages that
• Primary Key people use.
– project: ∏ β β 12 3
β β 23 10
– union: ∪
– set difference: –
σA=B ^ D > 5 (r)
– Cartesian product: x A B C D
– rename: ρ
α α 1 7
• The operators take one or two relations as inputs and β β 23 10
produce a new relation as a result.
σ branch_name=“Perryridge”(account)
3
Project Operation Union Operation – Example
• Relations A B A B
• Notation: ∏ A1 , A2 ,K, Ak
(r ) r, s:
α 1 α 2
α 2 β 3
where A1, A2 are attribute names and r is a relation name. β 1 s
r
• The result is defined as the relation of k columns obtained by
A B
erasing the columns that are not listed
r ∪ s: α 1
• Duplicate rows removed from result, since relations are sets
α 2
• Defined as: α 1 α 2
α 2 β 3
r ∪ s = {t | t ∈ r or t ∈ s}
β 1 s
• For r ∪ s to be valid. r
α 1 α 10 a
• Defined as: β 10 a
β 2
β 20 b
r – s = {t | t ∈ r and t ∉ s} r γ 10 b
s
r x s:
A B C D E
• Set differences must be taken between compatible relations. α 1 α 10 a
α 1 β 10 a
– r and s must have the same arity α 1 β 20 b
α 1 γ 10 b
– attribute domains of r and s must be compatible β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b
4
Cartesian-Product Operation Composition of Operations
• Can build expressions using multiple
• Notation r x s operations
• Example: σA=C(r x s) A B C D E
• Defined as:
• rxs α 1 α 10 a
α 1 β 10 a
r x s = {t q | t ∈ r and q ∈ s} α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
• Assume that attributes of r(R) and s(S) are disjoint. (That is, R β 2 γ 10 b
∩ S = ∅).
• σA=C(r x s) A B C D E
• If attributes of r(R) and s(S) are not disjoint, then renaming α 1 α 10 a
β 2 β 10 a
must be used. β 2 β 20 b
• Allows us to name, and therefore to refer to, the results of branch (branch_name, branch_city, assets)
relational-algebra expressions.
• Allows us to refer to a relation by more than one name. customer (customer_name, customer_street,
customer_city)
• Example:
ρ x (E) account (account_number, branch_name, balance)
returns the expression E under the name X loan (loan_number, branch_name, amount)
• If a relational-algebra expression E has arity n, then
ρ x ( A ,A
1 2 ,..., A n ) (E ) depositor (customer_name, account_number)
returns the result of expression E under the name X, and with borrower (customer_name, loan_number)
the
attributes renamed to A1 , A2 , …., An .
• Find all loans of over $1200 • Find the names of all customers who have a loan,
an account, or both, from the bank
σamount > 1200 (loan)
∏customer_name (borrower) ∪ ∏customer_name (depositor)
Find the loan number for each loan of an amount Find the names of all customers who have a loan
greater than $1200 and an account at bank.
∏loan_number (σamount > 1200 (loan))
∏customer_name (borrower) ∩ ∏customer_name (depositor)
5
Example Queries Example 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.
∏customer_name (σbranch_name=“Perryridge” z Query 1
∏customer_name(σloan.loan_number = borrower.loan_number (
∏customer_name (σbranch_name = “Perryridge”
(σbranch_name = “Perryridge” (loan)) x borrower))
(σborrower.loan_number = loan.loan_number(borrower x loan))) –
∏customer_name(depositor)
6
Set-Intersection Operation – Example Natural-Join Operation
Notation: r s
r s
follows:
• Relations r, s:
A B C D B D E
R = (A, B, C, D)
α 1 α a 1 a α
β 2 γ a 3 a β
S = (E, B, D) γ 4 β b 1 a γ
α 1 γ a 2 b δ
Result schema = (A, B, C, D, E) δ 2 β b 3 b ∈
r s
r s is defined as: r s
A B C D E
∏r.A, r.B, r.C, r.D, s.E (σr.B = s.B ∧ r.D = s.D (r x s)) α 1 α a α
α 1 α a γ
α 1 γ a α
α 1 γ a γ
δ 2 β b δ
7
Another Division Example Division Operation (Cont.)
Relations r, s: • Property
A B C D E D E – Let q = r ÷ s
α a α a 1 a 1 – Then q is the largest relation satisfying q x s ⊆ r
α a γ a 1 b 1
• Definition in terms of the basic algebra operation
α a γ b 1 s
β a γ a 1 Let r(R) and s(S) be relations, and let S ⊆ R
β a γ b 3
γ a γ a 1 r ÷ s = ∏R-S (r ) – ∏R-S ( ( ∏R-S (r ) x s ) – ∏R-S,S(r ))
γ a γ b 1
γ a β b 1
r To see why
r ÷ s:
– ∏R-S,S (r) simply reorders attributes of r
A B C
α a γ
– ∏R-S (∏R-S (r ) x s ) – ∏R-S,S(r) ) gives those tuples t in
γ a γ
∏R-S (r ) such that for some tuple u ∈ s, tu ∉ r.
• Find all customers who have an account from at • Find all customers who have an account at all
least the “Downtown” and the Uptown” branches. branches located in Brooklyn city.
z Query 1
z Query 2
∏customer_name, branch_name (depositor account)
÷ ρtemp(branch_name) ({(“Downtown” ), (“Uptown” )})
8
Extended Relational-Algebra-Operations Generalized Projection
• Extends the projection operation by allowing arithmetic
• Generalized Projection
functions to be used in the projection list.
• Aggregate Functions
• Outer Join ∏F ,F ,...,F (E)
1 2 n
• Relation account grouped by branch-name: • Result of aggregation does not have a name
branch_name account_number balance
– Can use rename operation to give it a name
Perryridge A-102 400
Perryridge A-201 900
– For convenience, we permit renaming as part of
Brighton A-217 750
Brighton A-215 750
aggregate operation
Redwood A-222 700
•branch_name g (account) g
sum(balance)
branch_name sum(balance) as sum_balance (account)
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
9
Outer Join Outer Join – Example
10
Modification of the Database Deletion
• The content of the database may be • A delete request is expressed similarly to a query,
modified using the following operations:
except instead of displaying tuples to the user, the
– Deletion
– Insertion selected tuples are removed from the database.
– Updating • Can delete only whole tuples; cannot delete values on
• All these operations are expressed using only particular attributes
the assignment operator.
• A deletion is expressed in relational algebra by:
r←r–E
where r is a relation and E is a relational algebra
query.
• Delete all account records in the Perryridge branch. • To insert data into a relation, we either:
account ← account – σ branch_name = “Perryridge” (account ) – specify a tuple to be inserted
Delete all loan records with amount in the range of 0 to 50 – write a query whose result is a set of tuples to be
loan ← loan – σ amount ≥ 0 and amount ≤ 50 (loan)
inserted
Delete all accounts at branches located in Needham. • in relational algebra, an insertion is expressed by:
r1 ← σ branch_city = “Needham” (account branch )
r← r ∪ E
r2 ← ∏branch_name, account_number, balance (r1)
r3 ← ∏ customer_name, account_number (r2 depositor) where r is a relation and E is a relational algebra
account ← account – r2 expression.
depositor ← depositor – r3
• The insertion of a single tuple is expressed by letting
E be a constant relation containing one tuple.
• Insert information in the database specifying • A mechanism to change a value in a tuple without
that Smith has $1200 in account A-973 at charging all values in the tuple
the Perryridge branch. • Use the generalized projection operator to do this task
account ← account ∪ {(“Perryridge”, A-973, 1200)}
depositor ← depositor ∪ {(“Smith”, A-973)}
r ← ∏ F ,F ,K,F , ( r )
Provide as a gift for all loan customers in the 1 2 l
11
Update Examples
12