Download Database System Concepts - Chapter 2: Relational

Document related concepts
no text concepts found
Transcript
Database
System
Concepts
Structure of
Relational
Databases
Database System Concepts
Chapter 2: Relational Model
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Departamento de Engenharia Informática
Instituto Superior Técnico
1st Semester
2007/2008
Null Values
Modification
of the
Database
Slides (fortemente) baseados nos slides oficiais do livro
“Database System Concepts”
c
Silberschatz,
Korth and Sudarshan.
Outline
Database
System
Concepts
Structure of
Relational
Databases
1
Structure of Relational Databases
2
Relational Algebra
3
Additional Relational Algebra Operations
4
Extended Relational Algebra Operations
5
Null Values
6
Modification of the Database
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Outline
Database
System
Concepts
Structure of
Relational
Databases
1
Structure of Relational Databases
2
Relational Algebra
3
Additional Relational Algebra Operations
4
Extended Relational Algebra Operations
5
Null Values
6
Modification of the Database
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Example of a Relation
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
customer_name
Jones
Smith
Curry
Lindsay
customer_street
Main
North
North
Park
customer_city
Harrison
Rye
Rye
Pittsfield
Basic Structure
Database
System
Concepts
Formally, given sets D1 , D2 , . . . , Dn , a relation r is a subset of
D1 × D2 × · · · × Dn
Structure of
Relational
Databases
Thus, a relation is a set of n-tuples (a1 , a2 , . . . , an ), where each
ai ∈ Di
Relational
Algebra
Example: If
customer name ={Jones, Smith, Curry , Lindsay }
customer street ={Main, North, Park}
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
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
Attribute Types
Database
System
Concepts
Each attribute of a relation has a name
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
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
We shall ignore the effect of null values in our main
presentation and consider their effect later
Relation Schema
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
A1 , A2 , . . . , An are attributes
R = (A1 , A2 , . . . , An ) is a relation schema
Example:
Customer schema=(customer name,customer street,customer city)
r (R) is a relation on the relation schema R
Example:
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
customer(Customer schema)
Or:
customer(customer name,customer street,customer city)
Relation Instance
Database
System
Concepts
Structure of
Relational
Databases
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
attributes
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
customer name
Jones
Smith
Curry
Lindsay
customer street
Main
North
North
Park
customer city
Harrison
Rye
Rye
Pittsfield
(columns)
tuples
(rows)
Null Values
Modification
of the
Database
Order of tuples is irrelevant (tuples may be stored in an
arbitrary order)
Relational Database
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
A database consists of multiple relations
Information about an enterprise is broken up into parts,
with each relation storing one part of the information
account: stores information about accounts
depositor: stores information about which customer
owns which account
customer : stores information about customers
Why not store all information as a single relation?
bank(account number, balance, customer name, ...)
The bank database
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Keys
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
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)
by “possible r” we mean a relation r that could exist in the
enterprise we are modeling.
Example: {customer name, customer street} and
{customer name} are both superkeys of Customer, if no
two customers can possibly have the same name.
K is a candidate key if K is minimal
Example: {customer name} is a candidate key for
Customer, since it is a superkey (assuming no two
customers can possibly have the same name), and no
subset of it is a superkey.
Primary Key is a candidate key that will be taken as the
main identifier for the tuples in the relation
Keys in the customer Relation
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Is {customer_name, customer_street, customer_city} a
superkey?
Modification
of the
Database
Is {customer_street} a candidate key?
Is {customer_street, customer_city} a superkey?
Keys in the depositor Relation
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
What are the possible candidate keys?
Outline
Database
System
Concepts
Structure of
Relational
Databases
1
Structure of Relational Databases
2
Relational Algebra
3
Additional Relational Algebra Operations
4
Extended Relational Algebra Operations
5
Null Values
6
Modification of the Database
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Query Languages
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Language in which user requests information from the
database.
Categories of languages
Procedural
Non-procedural, or declarative
“Pure” languages:
Relational algebra
Tuple relational calculus
Domain relational calculus
Pure languages form underlying basis of query languages
that people use.
Relational Algebra
Database
System
Concepts
Procedural language
Structure of
Relational
Databases
Relational
Algebra
Six basic operators
select: σ
project: π
Additional
Relational
Algebra
Operations
union: ∪
Extended
Relational
Algebra
Operations
Cartesian product: ×
Null Values
The operators take one or two relations as inputs and
produce a new relation as a result.
Modification
of the
Database
set difference: −
rename: ρ
The Select Operation
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
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: =, 6=, >, ≥, <, ≤
Example of selection:
σbranch name=′′ Perryridge ′′ (account)
The Select Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
σbranch name=′′ Perryridge ′′ (account)
The Select Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relation r
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
σA=B∧D>5 (r ) =?
A
a
a
b
b
B
a
b
b
b
C
1
5
12
23
D
7
7
3
10
The Select Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relation r
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
σA=B∧D>5 (r ) =?
A
a
a
b
b
B
a
b
b
b
C
1
5
12
23
D
7
7
3
10
The Select Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relation r
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
A
a
a
b
b
B
a
b
b
b
C
1
5
12
23
D
7
7
3
10
A
a
b
B
a
b
C
1
23
D
7
10
σA=B∧D>5 (r ) =?
The Project Operation
Database
System
Concepts
Notation:
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
π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: To eliminate the branch name attribute of
account
πaccount number ,balance (account)
The Project Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
πaccount
number ,balance (account)
=?
The Project Operation - Example
Database
System
Concepts
Relation r
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
πA,C (r ) =?
A
a
a
b
b
B
10
20
30
40
C
1
1
1
2
The Project Operation - Example
Database
System
Concepts
Relation r
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
πA,C (r ) =?
A
a
a
b
b
B
10
20
30
40
C
1
1
1
2
The Project Operation - Example
Database
System
Concepts
Relation r
A
a
a
b
b
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
πA,C (r ) =?
A
a
a
b
b
C
1
1
1
2
B
10
20
30
40
C
1
1
1
2
The Project Operation - Example
Database
System
Concepts
Relation r
A
a
a
b
b
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
B
10
20
30
40
C
1
1
1
2
πA,C (r ) =?
A
a
a
b
b
C
1
1
1
2
A
a
=
b
b
C
1
1
2
The Union Operation
Database
System
Concepts
Notation:
r ∪s
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Defined as:
r ∪ s = {t|t ∈ r or t ∈ s}
For r ∪ s to be valid:
1
2
r and s must have the same arity (same number of
attributes)
The attribute domains must be compatible (example: 2nd
column of r deals with the same type of values as does the
2nd column of s)
Example: to find all customers with either an account or a
loan
πcustomer
name (depositor )
∪ πcustomer
name (borrower )
The Union Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
πcustomer
name (depositor )
=?
πcustomer
name (borrower )
=?
πcustomer
name (depositor )
∪ πcustomer
Incorrect:
πaccount number (depositor ) ∪ πcustomer
name (borrower )
=?
name (borrower )
The Union Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
πcustomer
name (depositor )
=?
πcustomer
name (borrower )
=?
πcustomer
name (depositor )
∪ πcustomer
Incorrect:
πaccount number (depositor ) ∪ πcustomer
name (borrower )
=?
name (borrower )
The Union Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and s
A
a
r =
a
b
r ∪ s =?
B
1
2
1
A
s = a
b
B
2
3
The Union Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and s
A
a
r =
a
b
r ∪ s =?
B
1
2
1
A
s = a
b
B
2
3
The Union Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and s
A
a
r =
a
b
r ∪ s =?
B
1
2
1
A
s = a
b
A
a
a
b
b
B
1
2
1
3
B
2
3
The Set Difference Operation
Database
System
Concepts
Notation:
r −s
Structure of
Relational
Databases
Defined as:
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
r − s = {t|t ∈ r and t 6∈ s}
For r − s to be valid:
1
2
r and s must have the same arity
The attribute domains must be compatible
Example: to find all customers with an account but not a
loan
πcustomer
name (depositor )
− πcustomer
name (borrower )
The Set Difference Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
πcustomer
name (depositor )
=?
πcustomer
name (borrower )
=?
πcustomer
name (depositor )
− πcustomer
Incorrect:
πaccount number (depositor ) − πcustomer
name (borrower )
=?
name (borrower )
The Set Difference Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
πcustomer
name (depositor )
=?
πcustomer
name (borrower )
=?
πcustomer
name (depositor )
− πcustomer
Incorrect:
πaccount number (depositor ) − πcustomer
name (borrower )
=?
name (borrower )
The Set Difference Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and s
A
a
r =
a
b
r − s =?
B
1
2
1
A
s = a
b
B
2
3
The Set Difference Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and s
A
a
r =
a
b
r − s =?
B
1
2
1
A
s = a
b
B
2
3
The Set Difference Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and s
A
a
r =
a
b
r − s =?
B
1
2
1
A
s = a
b
A
a
b
B
1
1
B
2
3
The Cartesian Product Operation
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Notation:
r ×s
Defined as:
r × s = {tq|t ∈ r and q ∈ s}
Assume that attributes of r (R) and s(S) are disjoint.
(That is, R ∪ S = ∅).
If attributes of r (R) and s(S) are not disjoint, then
renaming must be used.
The Cartesian Product Operation - Example
Database
System
Concepts
Relations r and s
Structure of
Relational
Databases
A
r = a
b
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
r × s =?
B
1
2
C
a
s = b
b
c
D
10
10
20
10
E
x
x
y
y
The Cartesian Product Operation - Example
Database
System
Concepts
Relations r and s
Structure of
Relational
Databases
A
r = a
b
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
r × s =?
B
1
2
C
a
s = b
b
c
D
10
10
20
10
E
x
x
y
y
The Cartesian Product Operation - Example
Database
System
Concepts
Relations r and s
Structure of
Relational
Databases
A
r = a
b
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
C
a
s = b
b
c
B
1
2
r × s =?
A
a
a
a
a
b
b
b
b
B
1
1
1
1
2
2
2
2
C
a
b
b
c
a
b
b
c
D
10
10
20
10
10
10
20
10
E
x
x
y
y
x
x
y
y
D
10
10
20
10
E
x
x
y
y
The Rename Operation
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
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 arity n, then
ρX (A1 ,A2 ,...,An ) (E )
returns the result of expression E under the name X , and
with the attributes renamed to A1 , A2 , ..., An .
ρdep (depositor ) → dep(customer name, account number )
ρdep(cn,an) (depositor ) → dep(cn, an)
Operation Composition
Database
System
Concepts
Can build expressions using multiple operations
Example: σA=C (r × s)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
1
r ×s
2
σA=C
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
3
σA=C (r × s)
A
a
a
a
a
b
b
b
b
B
1
1
1
1
2
2
2
2
C
a
b
b
c
a
b
b
c
D
10
10
20
10
10
10
20
10
E
x
x
y
y
x
x
y
y
A
a
b
b
B
1
2
2
C
a
b
b
D
10
10
20
E
x
x
y
Operation Composition
Database
System
Concepts
Can build expressions using multiple operations
Example: σA=C (r × s)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
1
r ×s
2
σA=C
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
3
σA=C (r × s)
A
a
a
a
a
b
b
b
b
B
1
1
1
1
2
2
2
2
C
a
b
b
c
a
b
b
c
D
10
10
20
10
10
10
20
10
E
x
x
y
y
x
x
y
y
A
a
b
b
B
1
2
2
C
a
b
b
D
10
10
20
E
x
x
y
Operation Composition
Database
System
Concepts
Can build expressions using multiple operations
Example: σA=C (r × s)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
1
r ×s
2
σA=C
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
3
σA=C (r × s)
A
a
a
a
a
b
b
b
b
B
1
1
1
1
2
2
2
2
C
a
b
b
c
a
b
b
c
D
10
10
20
10
10
10
20
10
E
x
x
y
y
x
x
y
y
A
a
b
b
B
1
2
2
C
a
b
b
D
10
10
20
E
x
x
y
Operation Composition
Database
System
Concepts
Can build expressions using multiple operations
Example: σA=C (r × s)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
1
r ×s
2
σA=C
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
3
σA=C (r × s)
A
a
a
a
a
b
b
b
b
B
1
1
1
1
2
2
2
2
C
a
b
b
c
a
b
b
c
D
10
10
20
10
10
10
20
10
E
x
x
y
y
x
x
y
y
A
a
b
b
B
1
2
2
C
a
b
b
D
10
10
20
E
x
x
y
Operation Composition
Database
System
Concepts
Can build expressions using multiple operations
Example: σA=C (r × s)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
1
r ×s
2
σA=C
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
3
σA=C (r × s)
A
a
a
a
a
b
b
b
b
B
1
1
1
1
2
2
2
2
C
a
b
b
c
a
b
b
c
D
10
10
20
10
10
10
20
10
E
x
x
y
y
x
x
y
y
A
a
b
b
B
1
2
2
C
a
b
b
D
10
10
20
E
x
x
y
Outline
Database
System
Concepts
Structure of
Relational
Databases
1
Structure of Relational Databases
2
Relational Algebra
3
Additional Relational Algebra Operations
4
Extended Relational Algebra Operations
5
Null Values
6
Modification of the Database
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Additional Operations
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
We define additional operations that do not add any power to
the relational algebra, but that simplify common queries.
Set intersection: ∩
Natural join: ⋊
⋉
Division: ÷
Assignment: ←
The Set Intersection Operation
Database
System
Concepts
Structure of
Relational
Databases
Notation:
r ∩s
Defined as:
r ∩ s = {t|t ∈ r and t ∈ s}
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Note: r ∩ s = r − (r − s)
For r ∩ s to be valid:
1
2
r and s must have the same arity
The attribute domains must be compatible
Example: to find all customers with both an account and
a a loan
πcustomer
name (depositor )
∩ πcustomer
name (borrower )
The Set Intersection Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and s
A
a
r =
a
b
r ∩ s =?
B
1
2
1
A
s = a
b
B
2
3
The Set Intersection Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and s
A
a
r =
a
b
r ∩ s =?
B
1
2
1
A
s = a
b
B
2
3
The Set Intersection Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and s
A
a
r =
a
b
r ∩ s =?
B
1
2
1
A
s = a
b
A
a
B
2
B
2
3
The Natural Join Operation
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Notation:
r⋊
⋉s
Let r and s be relations on schemas R and S respectively.
Then, r ⋊
⋉ s is a relation on schema R ∪ S obtained as
follows:
Consider 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 R ∪ S, add a tuple t to the result, where
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
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 = πr .A,r .B,r .C ,r .D,s.E (σr .B=s.B∧r .D=s.D (r × s))
The Natural Join Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and
A
a
b
r =
c
a
d
r⋊
⋉ s =?
s
B
1
2
4
1
2
C
a
c
b
c
b
D
x
x
y
x
y
B
1
3
s =
1
2
3
D
x
x
x
y
y
E
a
b
c
d
e
The Natural Join Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and
A
a
b
r =
c
a
d
r⋊
⋉ s =?
s
B
1
2
4
1
2
C
a
c
b
c
b
D
x
x
y
x
y
B
1
3
s =
1
2
3
D
x
x
x
y
y
E
a
b
c
d
e
The Natural Join Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and
A
a
b
r =
c
a
d
r⋊
⋉ s =?
s
B
1
2
4
1
2
C
a
c
b
c
b
D
x
x
y
x
y
A
a
a
a
a
d
B
1
1
1
1
2
B
1
3
s =
1
2
3
C
a
a
c
c
b
D
x
x
x
x
y
E
a
c
a
c
d
D
x
x
x
y
y
E
a
b
c
d
e
The Division Operation
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Notation:
r ÷s
Let r and s be relations on schemas R and S respectively,
where
R = (A1 , . . . , Am , B1 , . . . , Bn )
S = (B1 , . . . , Bn )
Then, r ÷ s is a relation on schema R − S obtained, such
that:
r ÷ s = {t|t ∈ πR−S (t) ∧ ∀u∈s (tu ∈ r )}
Where tu means the concatenation of tuples t and u to
produce a single tuple
Note:
r ÷ s = πR−S (r ) − πR−S ((πR−S (r ) × s) − πR−S,S (r ))
The Division Operation - Example
Database
System
Concepts
Relations r and s
Structure of
Relational
Databases
Relational
Algebra
r =
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
r ÷ s =?
A
a
a
a
b
c
d
d
d
e
e
b
B
1
2
3
1
1
1
3
4
6
1
2
B
s = 1
2
The Division Operation - Example
Database
System
Concepts
Relations r and s
Structure of
Relational
Databases
Relational
Algebra
r =
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
r ÷ s =?
A
a
a
a
b
c
d
d
d
e
e
b
B
1
2
3
1
1
1
3
4
6
1
2
B
s = 1
2
The Division Operation - Example
Database
System
Concepts
Relations r and s
Structure of
Relational
Databases
Relational
Algebra
r =
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
A
a
a
a
b
c
d
d
d
e
e
b
B
1
2
3
1
1
1
3
4
6
1
2
B
s = 1
2
r ÷ s =?
A
a
b
Another Division Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and
A
a
a
a
r = b
b
c
c
c
r ÷ s =?
s
B
x
x
x
x
x
x
x
x
C
a
c
c
c
c
c
c
b
D
x
x
y
x
y
x
y
y
E
1
1
1
1
3
1
1
1
s =
D
x
y
E
1
1
Another Division Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and
A
a
a
a
r = b
b
c
c
c
r ÷ s =?
s
B
x
x
x
x
x
x
x
x
C
a
c
c
c
c
c
c
b
D
x
x
y
x
y
x
y
y
E
1
1
1
1
3
1
1
1
s =
D
x
y
E
1
1
Another Division Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations r and
A
a
a
a
r = b
b
c
c
c
r ÷ s =?
s
B
x
x
x
x
x
x
x
x
C
a
c
c
c
c
c
c
b
D
x
x
y
x
y
x
y
y
A
a
c
E
1
1
1
1
3
1
1
1
B
x
x
s =
C
c
c
D
x
y
E
1
1
The Assignement Operation
Database
System
Concepts
The assignment operation (←) provides a convenient way
to express complex queries.
Write query as a sequential program consisting of
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
a series of assignmets
followed by an expression whose value is displayed as a
result of the query.
Assignment must always be made to a temporary relation
variable.
Example: write r ÷ s as
temp1 ← πR−S (r )
temp2 ← πR−S ((temp1 × s) − πR−S,S (r ))
result = temp1 − temp2
The result to the right of the ← is assigned to the relation
variable on the left of the ←
May use variable in subsequent expressions.
Outline
Database
System
Concepts
Structure of
Relational
Databases
1
Structure of Relational Databases
2
Relational Algebra
3
Additional Relational Algebra Operations
4
Extended Relational Algebra Operations
5
Null Values
6
Modification of the Database
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Extended Operations
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Generalized projection
Additional
Relational
Algebra
Operations
Aggregate functions
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Outer join
Generalized Projection
Database
System
Concepts
Extends the projection operation by allowing arithmetic
functions to be used in the projection list
Structure of
Relational
Databases
πF1 ,F2 ,...,Fn (E )
Relational
Algebra
E is any relational-algebra expression
Additional
Relational
Algebra
Operations
Each of F1 , F2 , . . . , Fn are are arithmetic expressions
involving constants and attributes in the schema of E .
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Example
Given relation
credit info(customer name, limit, credit balance)
find how much more each person can spend.
πcustomer
name,limit−credit balance (credit
info)
Generalized Projection - Example
Database
System
Concepts
Relation r
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
πA,C ∗10 (r ) =?
A
a
a
b
b
B
10
20
30
40
C
1
1
1
2
Generalized Projection - Example
Database
System
Concepts
Relation r
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
πA,C ∗10 (r ) =?
A
a
a
b
b
B
10
20
30
40
C
1
1
1
2
Generalized Projection - Example
Database
System
Concepts
Relation r
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
A
a
a
b
b
B
10
20
30
40
C
1
1
1
2
A
a
b
b
C*10
10
10
20
πA,C ∗10 (r ) =?
Aggregate Functions and Operations
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Aggregation function takes a collection of values and
returns a single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate operation in relational algebra
G1 ,G2 ,...,Gn GF1 (A1 ),F2 (A2 ),...,Fn (An ) (E )
E is any relational-algebra expression
G1 , G2 . . . , Gn is a list of attributes on which to group (can
be empty)
Each Fi is an aggregate function
Each Ai is an attribute name
Aggregate Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relation r
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Gsum(C ) (r ) =?
A
a
a
b
b
B
a
b
b
b
C
7
7
3
10
Aggregate Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relation r
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Gsum(C ) (r ) =?
A
a
a
b
b
B
a
b
b
b
C
7
7
3
10
Aggregate Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relation r
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
A
a
a
b
b
B
a
b
b
b
C
7
7
3
10
Gsum(C ) (r ) =?
sum(C)
27
Aggregate Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relation account grouped by branch name
branch name
Perryridge
Perryridge
Brighton
Brighton
Redwood
account number
A-102
A-201
A-217
A-215
A-222
balance
400
900
750
750
700
Aggregate Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relation account grouped by branch name
branch name
Perryridge
Perryridge
Brighton
Brighton
Redwood
account number
A-102
A-201
A-217
A-215
A-222
balance
400
900
750
750
700
branch name Gsum(balance) (account)
Aggregate Operation - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relation account grouped by branch name
branch name
Perryridge
Perryridge
Brighton
Brighton
Redwood
account number
A-102
A-201
A-217
A-215
A-222
balance
400
900
750
750
700
branch name Gsum(balance) (account)
branch name
Perryridge
Brighton
Redwood
sum(balance)
1300
1500
700
Aggregate Functions (cont.)
Database
System
Concepts
Result of aggregation does not have a name
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Can use rename operation to give it a name
For convenience, we permit renaming as part of aggregate
operation
branch name Gsum(balance) as sum balance (account)
branch name
Perryridge
Brighton
Redwood
sum balance
1300
1500
700
Outer Join
Database
System
Concepts
Structure of
Relational
Databases
An extension of the join operation that avoids loss of
information.
Additional
Relational
Algebra
Operations
Computes the join and then adds tuples form one relation
that does not match tuples in the other relation to the
result of the join.
Uses null values:
Extended
Relational
Algebra
Operations
null signifies that the value is unknown or does not exist
All comparisons involving null are (roughly speaking) false
by definition.
Relational
Algebra
Null Values
Modification
of the
Database
We shall study precise meaning of comparisons with nulls
later
Outer Join - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Relations loan and borrower
loan number
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
L-170
L-230
L-260
branch name
amount
customer name
Downtown
Redwood
Perryridge
3000
4000
1700
Jones
Smith
Hayes
loan number
L-170
L-230
L-155
Outer Join - Example
Database
System
Concepts
Relations loan and borrower
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
loan number
L-170
L-230
L-260
branch name
amount
customer name
Downtown
Redwood
Perryridge
3000
4000
1700
Jones
Smith
Hayes
loan number
L-170
L-230
L-155
Inner join: loan ⋊
⋉ borrower
loan number
L-170
L-230
branch name
amount
customer name
Downtown
Redwood
3000
4000
Jones
Smith
Outer Join - Example
Database
System
Concepts
Relations loan and borrower
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
loan number
L-170
L-230
L-260
branch name
amount
customer name
Downtown
Redwood
Perryridge
3000
4000
1700
Jones
Smith
Hayes
loan number
L-170
L-230
L-155
Left outer join: loan−
⋉ borrower
−⋊
loan number
L-170
L-230
L-260
branch name
amount
customer name
Downtown
Redwood
Perryridge
3000
4000
1700
Jones
Smith
null
Outer Join - Example
Database
System
Concepts
Relations loan and borrower
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
loan number
L-170
L-230
L-260
branch name
amount
customer name
Downtown
Redwood
Perryridge
3000
4000
1700
Jones
Smith
Hayes
loan number
L-170
L-230
L-155
Right outer join: loan ⋊
⋉−
−borrower
loan number
L-170
L-230
L-155
branch name
amount
customer name
Downtown
Redwood
null
3000
4000
null
Jones
Smith
Hayes
Outer Join - Example
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Relations loan and borrower
loan number
L-170
L-230
L-260
branch name
amount
customer name
Downtown
Redwood
Perryridge
3000
4000
1700
Jones
Smith
Hayes
loan number
L-170
L-230
L-155
Full outer join: loan−
⋉−
−⋊
−borrower
loan number
L-170
L-230
L-260
L-155
branch name
amount
customer name
Downtown
Redwood
Perryridge
null
3000
4000
1700
null
Jones
Smith
null
Hayes
Outline
Database
System
Concepts
Structure of
Relational
Databases
1
Structure of Relational Databases
2
Relational Algebra
3
Additional Relational Algebra Operations
4
Extended Relational Algebra Operations
5
Null Values
6
Modification of the Database
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Null Values
Database
System
Concepts
Structure of
Relational
Databases
It is possible for tuples to have a null value, denoted by
null, for some of their attributes
Relational
Algebra
null signifies an unknown value or that a value does not
exist.
Additional
Relational
Algebra
Operations
The result of any arithmetic expression involving null is
null
Extended
Relational
Algebra
Operations
Aggregate functions simply ignore null values (as in SQL)
Null Values
Modification
of the
Database
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 Values
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Comparisons with null values return the special truth
value: unknown
x
unknown
unknown
unknown
y
true
false
unknown
x OR y
true
unknown
unknown
x
unknown
unknown
unknown
y
true
false
unknown
x AND y
unknown
false
unknown
Null Values
Modification
of the
Database
x
unknown
NOT x
unknown
Result of select predicate is treated as false if it evaluates
to unknown
Outline
Database
System
Concepts
Structure of
Relational
Databases
1
Structure of Relational Databases
2
Relational Algebra
3
Additional Relational Algebra Operations
4
Extended Relational Algebra Operations
5
Null Values
6
Modification of the Database
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Modification of the Database
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
The content of the database may be modified using the
following operations:
Deletion
Insertion
Updating
All these operations are expressed using the assignment
operator.
Deletion
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
A delete request is expressed similarly to a query, except
instead of displaying tuples to the user, the selected tuples
are removed from the database.
Can delete only whole tuples; cannot delete values on only
particular attributes
A deletion is expressed in relational algebra by:
r ←r −E
where r is a relation and E is a relational algebra query.
Deletion Examples
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Delete all account records in the Perryridge branch.
Delete all loan records with amount in the range of 0 to
50
Delete all accounts at branches located in Needham
Deletion Examples
Database
System
Concepts
Delete all account records in the Perryridge branch.
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Delete all loan records with amount in the range of 0 to
50
Delete all accounts at branches located in Needham
Deletion Examples
Database
System
Concepts
Delete all account records in the Perryridge branch.
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Delete all loan records with amount in the range of 0 to
50
Delete all accounts at branches located in Needham
Deletion Examples
Database
System
Concepts
Delete all account records in the Perryridge branch.
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Delete all loan records with amount in the range of 0 to
50
loan ← loan − σamount≥0 and amount≤50 (loan)
Delete all accounts at branches located in Needham
Deletion Examples
Database
System
Concepts
Delete all account records in the Perryridge branch.
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Delete all loan records with amount in the range of 0 to
50
loan ← loan − σamount≥0 and amount≤50 (loan)
Delete all accounts at branches located in Needham
Deletion Examples
Database
System
Concepts
Delete all account records in the Perryridge branch.
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Delete all loan records with amount in the range of 0 to
50
loan ← loan − σamount≥0 and amount≤50 (loan)
Delete all accounts at branches located in Needham
⋉ branch)
r1 ← σbranch city =′′ Needham′′ (account ⋊
Deletion Examples
Database
System
Concepts
Delete all account records in the Perryridge branch.
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Delete all loan records with amount in the range of 0 to
50
loan ← loan − σamount≥0 and amount≤50 (loan)
Delete all accounts at branches located in Needham
⋉ branch)
r1 ← σbranch city =′′ Needham′′ (account ⋊
r2 ← πbranch name,account
number ,balance (r1 )
Deletion Examples
Database
System
Concepts
Delete all account records in the Perryridge branch.
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Delete all loan records with amount in the range of 0 to
50
loan ← loan − σamount≥0 and amount≤50 (loan)
Delete all accounts at branches located in Needham
⋉ branch)
r1 ← σbranch city =′′ Needham′′ (account ⋊
r2 ← πbranch name,account
account ← account − r2
number ,balance (r1 )
Deletion Examples
Database
System
Concepts
Delete all account records in the Perryridge branch.
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Delete all loan records with amount in the range of 0 to
50
loan ← loan − σamount≥0 and amount≤50 (loan)
Delete all accounts at branches located in Needham
⋉ branch)
r1 ← σbranch city =′′ Needham′′ (account ⋊
r2 ← πbranch name,account
number ,balance (r1 )
account ← account − r2
r3 ← πcustomer
name,account number (r2
⋊
⋉ depositor )
Deletion Examples
Database
System
Concepts
Delete all account records in the Perryridge branch.
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Delete all loan records with amount in the range of 0 to
50
loan ← loan − σamount≥0 and amount≤50 (loan)
Delete all accounts at branches located in Needham
⋉ branch)
r1 ← σbranch city =′′ Needham′′ (account ⋊
r2 ← πbranch name,account
number ,balance (r1 )
account ← account − r2
r3 ← πcustomer
name,account number (r2
depositor ← depositor − r3
⋊
⋉ depositor )
Insertion
Database
System
Concepts
To insert data into a relation, we either:
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
specify a tuple to be inserted
write a query whose result is a set of tuples to be inserted
In relational algebra, an insertion is expressed by:
r ←r ∪E
where r is a relation and E is a relational algebra
expression.
The insertion of a single tuple is expressed by letting E be
a constant relation containing one tuple.
Insertion Examples
Database
System
Concepts
Insert information in the database specifying that Smith
has $1200 in account A-973 at the Perryridge branch.
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Provide as a gift for all loan customers in the Perryridge
branch, a $200 savings account. Let the loan number
serve as the account number for the new savings account.
Insertion Examples
Database
System
Concepts
Structure of
Relational
Databases
Insert information in the database specifying that Smith
has $1200 in account A-973 at the Perryridge branch.
account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Provide as a gift for all loan customers in the Perryridge
branch, a $200 savings account. Let the loan number
serve as the account number for the new savings account.
Insertion Examples
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Insert information in the database specifying that Smith
has $1200 in account A-973 at the Perryridge branch.
account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
depositor ← depositor ∪ {{′′ Smith′′ , A − 973}}
Provide as a gift for all loan customers in the Perryridge
branch, a $200 savings account. Let the loan number
serve as the account number for the new savings account.
Insertion Examples
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Insert information in the database specifying that Smith
has $1200 in account A-973 at the Perryridge branch.
account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
depositor ← depositor ∪ {{′′ Smith′′ , A − 973}}
Provide as a gift for all loan customers in the Perryridge
branch, a $200 savings account. Let the loan number
serve as the account number for the new savings account.
Insertion Examples
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Insert information in the database specifying that Smith
has $1200 in account A-973 at the Perryridge branch.
account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
depositor ← depositor ∪ {{′′ Smith′′ , A − 973}}
Provide as a gift for all loan customers in the Perryridge
branch, a $200 savings account. Let the loan number
serve as the account number for the new savings account.
⋉ loan)
r1 ← σbranch name=′′ Perryridge ′′ (borrower ⋊
Insertion Examples
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Insert information in the database specifying that Smith
has $1200 in account A-973 at the Perryridge branch.
account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
depositor ← depositor ∪ {{′′ Smith′′ , A − 973}}
Provide as a gift for all loan customers in the Perryridge
branch, a $200 savings account. Let the loan number
serve as the account number for the new savings account.
⋉ loan)
r1 ← σbranch name=′′ Perryridge ′′ (borrower ⋊
account ← account ∪ πbranch name,loan number ,200 (r1 )
Insertion Examples
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Insert information in the database specifying that Smith
has $1200 in account A-973 at the Perryridge branch.
account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
depositor ← depositor ∪ {{′′ Smith′′ , A − 973}}
Provide as a gift for all loan customers in the Perryridge
branch, a $200 savings account. Let the loan number
serve as the account number for the new savings account.
⋉ loan)
r1 ← σbranch name=′′ Perryridge ′′ (borrower ⋊
account ← account ∪ πbranch name,loan number ,200 (r1 )
depositor ← depositor ∪ πcustomer
name,loan number (r1 )
Updating
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
A mechanism to change a value in a tuple without
charging all values in the tuple
Use the generalized projection operator to do this task
r ← πF1 ,F2 ,...,Fn (r )
Each Fi is either
the i-th attribute of r , if the i-th attribute is not updated,
or,
if the attribute is to be updated Fi is an expression,
involving only constants and the attributes of r , which
gives the new value for the attribute
Updating Examples
Database
System
Concepts
Make interest payments by increasing all balances by 5
percent.
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Pay all accounts with balances over $10,000 6 percent
interest and pay all others 5 percent
Updating Examples
Database
System
Concepts
Structure of
Relational
Databases
Make interest payments by increasing all balances by 5
percent.
account ← πaccount
number ,branch name,balance×1.05 (account)
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Pay all accounts with balances over $10,000 6 percent
interest and pay all others 5 percent
Updating Examples
Database
System
Concepts
Structure of
Relational
Databases
Make interest payments by increasing all balances by 5
percent.
account ← πaccount
number ,branch name,balance×1.05 (account)
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Pay all accounts with balances over $10,000 6 percent
interest and pay all others 5 percent
Updating Examples
Database
System
Concepts
Structure of
Relational
Databases
Make interest payments by increasing all balances by 5
percent.
account ← πaccount
number ,branch name,balance×1.05 (account)
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Pay all accounts with balances over $10,000 6 percent
interest and pay all others 5 percent
r1 ← σbalance>10000 (account)
Updating Examples
Database
System
Concepts
Structure of
Relational
Databases
Make interest payments by increasing all balances by 5
percent.
account ← πaccount
number ,branch name,balance×1.05 (account)
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Pay all accounts with balances over $10,000 6 percent
interest and pay all others 5 percent
r1 ← σbalance>10000 (account)
r2 ← σbalance≤10000 (account)
Updating Examples
Database
System
Concepts
Structure of
Relational
Databases
Make interest payments by increasing all balances by 5
percent.
account ← πaccount
number ,branch name,balance×1.05 (account)
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Pay all accounts with balances over $10,000 6 percent
interest and pay all others 5 percent
r1 ← σbalance>10000 (account)
r2 ← σbalance≤10000 (account)
account ← πaccount
∪ πaccount
number ,branch name,balance×1.06 (r1 )
number ,branch name,balance×1.05 (r2 )
Database
System
Concepts
Structure of
Relational
Databases
Relational
Algebra
Additional
Relational
Algebra
Operations
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
End of Chapter 2
Related documents