Lecture 5 Relational Algebra

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 119

Database

System
Concepts
Database System Concepts
Structure of Chapter 2: Relational Model
Relational
Databases

Relational
Algebra

Additional
Relational Departamento de Engenharia Informática
Algebra Instituto Superior Técnico
Operations

Extended
Relational
Algebra 1st Semester
Operations
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

1 Structure of Relational Databases


Structure of
Relational
Databases 2 Relational Algebra
Relational
Algebra

Additional 3 Additional Relational Algebra Operations


Relational
Algebra
Operations
4 Extended Relational Algebra Operations
Extended
Relational
Algebra
Operations 5 Null Values
Null Values

Modification
of the 6 Modification of the Database
Database
Outline

Database
System
Concepts

1 Structure of Relational Databases


Structure of
Relational
Databases 2 Relational Algebra
Relational
Algebra

Additional 3 Additional Relational Algebra Operations


Relational
Algebra
Operations

Extended
4 Extended Relational Algebra Operations
Relational
Algebra
Operations
5 Null Values
Null Values

Modification
of the
Database
6 Modification of the Database
Example of a Relation

Database
System
Concepts

Structure of
Relational
Databases
customer_name customer_street customer_city
Relational
Algebra Jones Main Harrison
Additional
Relational
Smith North Rye
Algebra
Operations
Curry North Rye
Extended
Lindsay Park Pittsfield
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Basic Structure

Database
System Formally, given sets D1 , D2 , . . . , Dn , a relation r is a subset of
Concepts
D1 × D2 × · · · × Dn
Structure of Thus, a relation is a set of n-tuples (a1 , a2 , . . . , an ), where each
Relational
Databases ai ∈ Di
Relational Example: If
Algebra

Additional customer name ={Jones, Smith, Curry , Lindsay }


Relational
Algebra customer street ={Main, North, Park}
Operations
customer city ={Harrison, Rye, Pittsfield}
Extended
Relational
Algebra Then
Operations

Null Values r = {(Jones, Main, Harrison), (Smith, North, Rye),


Modification (Curry , North, Rye), (Lindsay , Park, Pittsfield)}
of the
Database
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 The set of allowed values for each attribute is called the
Relational
Databases domain of the attribute
Relational
Algebra
Attribute values are (normally) required to be atomic; that
Additional
is, indivisible
Relational
Algebra
Note: multivalued attribute values are not atomic
Operations Note: composite attribute values are not atomic
Extended
Relational The special value null is a member of every domain
Algebra
Operations The null value causes complications in the definition of
Null Values many operations
Modification We shall ignore the effect of null values in our main
of the
Database
presentation and consider their effect later
Relation Schema

Database
System
Concepts
A1 , A2 , . . . , An are attributes
Structure of R = (A1 , A2 , . . . , An ) is a relation schema
Relational
Databases Example:
Relational
Algebra Customer schema=(customer name,customer street,customer city)
Additional
Relational
Algebra
r (R) is a relation on the relation schema R
Operations Example:
Extended
Relational
Algebra customer(Customer schema)
Operations

Null Values Or:


Modification
of the customer(customer name,customer street,customer city)
Database
Relation Instance

Database
System The current values (relation instance) of a relation are
Concepts
specified by a table
Structure of
An element t of r is a tuple, represented by a row in a table
Relational
Databases

Relational
attributes (columns)
Algebra

Additional customer name customer street customer city


Relational tuples
Algebra Jones Main Harrison
Operations
Smith North Rye (rows)
Extended
Relational
Curry North Rye
Algebra Lindsay Park Pittsfield
Operations

Null Values

Modification Order of tuples is irrelevant (tuples may be stored in an


of the
Database
arbitrary order)
Relational Database

Database
System
Concepts

Structure of
A database consists of multiple relations
Relational
Databases
Information about an enterprise is broken up into parts,
Relational
with each relation storing one part of the information
Algebra
account: stores information about accounts
Additional
Relational
depositor: stores information about which customer
Algebra owns which account
Operations
customer : stores information about customers
Extended
Relational
Algebra
Why not store all information as a single relation?
Operations

Null Values bank(account number, balance, customer name, ...)


Modification
of the
Database
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 Let K ⊆ R
K is a superkey of R if values for K are sufficient to
Structure of identify a unique tuple of each possible relation r (R)
Relational
Databases by “possible r” we mean a relation r that could exist in the
Relational enterprise we are modeling.
Algebra Example: {customer name, customer street} and
Additional
Relational
{customer name} are both superkeys of Customer, if no
Algebra two customers can possibly have the same name.
Operations

Extended
K is a candidate key if K is minimal
Relational
Algebra
Example: {customer name} is a candidate key for
Operations Customer, since it is a superkey (assuming no two
Null Values customers can possibly have the same name), and no
Modification subset of it is a superkey.
of the
Database 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
Is {customer_name, customer_street, customer_city} a
Null Values
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
What are the possible candidate keys?
Modification
of the
Database
Outline

Database
System
Concepts

1 Structure of Relational Databases


Structure of
Relational
Databases 2 Relational Algebra
Relational
Algebra

Additional 3 Additional Relational Algebra Operations


Relational
Algebra
Operations

Extended
4 Extended Relational Algebra Operations
Relational
Algebra
Operations
5 Null Values
Null Values

Modification
of the
Database
6 Modification of the Database
Query Languages

Database
System
Concepts

Language in which user requests information from the


Structure of
Relational
database.
Databases
Categories of languages
Relational
Algebra Procedural
Additional Non-procedural, or declarative
Relational
Algebra “Pure” languages:
Operations
Relational algebra
Extended
Relational Tuple relational calculus
Algebra
Operations
Domain relational calculus
Null Values Pure languages form underlying basis of query languages
Modification
of the
that people use.
Database
Relational Algebra

Database
System
Concepts

Procedural language
Structure of
Relational
Six basic operators
Databases
select: σ
Relational
Algebra project: π
Additional
Relational union: ∪
Algebra
Operations set difference: −
Extended
Relational Cartesian product: ×
Algebra
Operations rename: ρ
Null Values
The operators take one or two relations as inputs and
Modification
of the produce a new relation as a result.
Database
The Select Operation

Database
System Notation: σp (r )
Concepts
p is called the selection predicate
Structure of Defined as:
Relational
Databases
σp (r ) = {t|t ∈ r and p(t)}
Relational
Algebra

Additional Where p is a formula in propositional calculus consisting


Relational
Algebra of terms connected by : ∧ (and), ∨ (or), ¬ (not)
Operations
Each term is one of:
Extended
Relational
Algebra
Operations
< attribute > op < attribute > or < constant >
Null Values
where op is one of: =, 6=, >, ≥, <, ≤
Modification
of the Example of selection:
Database

σ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 σbranch name=′′ Perryridge ′′ (account)
Database
The Select Operation - Example

Database
System
Concepts

Relation r
Structure of
Relational A B C D
Databases
a a 1 7
Relational
Algebra a b 5 7
Additional b b 12 3
Relational
Algebra b b 23 10
Operations

Extended σA=B∧D>5 (r ) =?
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Select Operation - Example

Database
System
Concepts

Relation r
Structure of
Relational A B C D
Databases
a a 1 7
Relational
Algebra a b 5 7
Additional b b 12 3
Relational
Algebra b b 23 10
Operations

Extended σA=B∧D>5 (r ) =?
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Select Operation - Example

Database
System
Concepts

Relation r
Structure of
Relational A B C D
Databases
a a 1 7
Relational
Algebra a b 5 7
Additional b b 12 3
Relational
Algebra b b 23 10
Operations

Extended σA=B∧D>5 (r ) =?
Relational
Algebra A B C D
Operations
a a 1 7
Null Values

Modification
b b 23 10
of the
Database
The Project Operation

Database
System
Concepts

Notation:
Structure of
Relational
πA1 ,A2 ,...,Ak (r )
Databases

Relational
where A1 , A2 are attribute names and r is a relation name.
Algebra
The result is defined as the relation of k columns obtained
Additional
Relational by erasing the columns that are not listed
Algebra
Operations Duplicate rows removed from result, since relations are
Extended
Relational
sets
Algebra
Operations Example: To eliminate the branch name attribute of
Null Values account
Modification πaccount number ,balance (account)
of the
Database
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 πaccount number ,balance (account) =?
Database
The Project Operation - Example

Database
System
Concepts
Relation r
A B C
Structure of
Relational
Databases
a 10 1
Relational
a 20 1
Algebra b 30 1
Additional
Relational
b 40 2
Algebra
Operations πA,C (r ) =?
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Project Operation - Example

Database
System
Concepts
Relation r
A B C
Structure of
Relational
Databases
a 10 1
Relational
a 20 1
Algebra b 30 1
Additional
Relational
b 40 2
Algebra
Operations πA,C (r ) =?
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Project Operation - Example

Database
System
Concepts
Relation r
A B C
Structure of
Relational
Databases
a 10 1
Relational
a 20 1
Algebra b 30 1
Additional
Relational
b 40 2
Algebra
Operations πA,C (r ) =?
Extended A C
Relational
Algebra
Operations
a 1
Null Values
a 1
Modification b 1
of the
Database b 2
The Project Operation - Example

Database
System
Concepts
Relation r
A B C
Structure of
Relational
Databases
a 10 1
Relational
a 20 1
Algebra b 30 1
Additional
Relational
b 40 2
Algebra
Operations πA,C (r ) =?
Extended A C
Relational A C
Algebra a 1
Operations a 1
a 1 =
Null Values
b 1
Modification b 1
of the b 2
Database b 2
The Union Operation

Database
System
Concepts
Notation:
r ∪s
Structure of
Relational
Defined as:
Databases
r ∪ s = {t|t ∈ r or t ∈ s}
Relational
Algebra For r ∪ s to be valid:
Additional
Relational
1 r and s must have the same arity (same number of
Algebra attributes)
Operations
2 The attribute domains must be compatible (example: 2nd
Extended
Relational column of r deals with the same type of values as does the
Algebra
Operations
2nd column of s)
Null Values Example: to find all customers with either an account or a
Modification
of the
loan
Database

π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 πcustomer name (depositor ) =?
Algebra
Operations πcustomer name (borrower ) =?
Null Values
πcustomer name (depositor ) ∪ πcustomer name (borrower ) =?
Modification
of the
Database
Incorrect:
πaccount number (depositor ) ∪ πcustomer name (borrower )
The Union Operation - Example

Database
System
Concepts

Structure of
Relational
Databases

Relational
Algebra

Additional
Relational
Algebra
Operations

Extended
Relational πcustomer name (depositor ) =?
Algebra
Operations πcustomer name (borrower ) =?
Null Values
πcustomer name (depositor ) ∪ πcustomer name (borrower ) =?
Modification
of the
Database
Incorrect:
πaccount number (depositor ) ∪ πcustomer name (borrower )
The Union Operation - Example

Database
System
Concepts

Relations r and s
Structure of
A B
Relational
Databases
A B
a 1
Relational r = s = a 2
Algebra a 2
b 3
Additional b 1
Relational
Algebra
Operations
r ∪ s =?
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Union Operation - Example

Database
System
Concepts

Relations r and s
Structure of
A B
Relational
Databases
A B
a 1
Relational r = s = a 2
Algebra a 2
b 3
Additional b 1
Relational
Algebra
Operations
r ∪ s =?
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Union Operation - Example

Database
System
Concepts

Relations r and s
Structure of
A B
Relational
Databases
A B
a 1
Relational r = s = a 2
Algebra a 2
b 3
Additional b 1
Relational
Algebra
Operations
r ∪ s =?
A B
Extended
Relational
Algebra
a 1
Operations a 2
Null Values b 1
Modification
of the
b 3
Database
The Set Difference Operation

Database
System
Concepts
Notation:
r −s
Structure of
Relational
Databases Defined as:
Relational
Algebra r − s = {t|t ∈ r and t 6∈ s}
Additional
Relational
Algebra For r − s to be valid:
Operations
1 r and s must have the same arity
Extended
Relational 2 The attribute domains must be compatible
Algebra
Operations Example: to find all customers with an account but not a
Null Values loan
Modification
of the
Database π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 πcustomer name (depositor ) =?
Algebra
Operations πcustomer name (borrower ) =?
Null Values
πcustomer name (depositor ) − πcustomer name (borrower ) =?
Modification
of the
Database
Incorrect:
πaccount number (depositor ) − πcustomer name (borrower )
The Set Difference Operation - Example

Database
System
Concepts

Structure of
Relational
Databases

Relational
Algebra

Additional
Relational
Algebra
Operations

Extended
Relational πcustomer name (depositor ) =?
Algebra
Operations πcustomer name (borrower ) =?
Null Values
πcustomer name (depositor ) − πcustomer name (borrower ) =?
Modification
of the
Database
Incorrect:
πaccount number (depositor ) − πcustomer name (borrower )
The Set Difference Operation - Example

Database
System
Concepts

Structure of Relations r and s


Relational
Databases A B
A B
Relational a 1
Algebra r = s = a 2
a 2
Additional
b 3
Relational
Algebra
b 1
Operations
r − s =?
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Set Difference Operation - Example

Database
System
Concepts

Structure of Relations r and s


Relational
Databases A B
A B
Relational a 1
Algebra r = s = a 2
a 2
Additional
b 3
Relational
Algebra
b 1
Operations
r − s =?
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Set Difference Operation - Example

Database
System
Concepts

Structure of Relations r and s


Relational
Databases A B
A B
Relational a 1
Algebra r = s = a 2
a 2
Additional
b 3
Relational
Algebra
b 1
Operations
r − s =?
Extended
Relational A B
Algebra
Operations a 1
Null Values b 1
Modification
of the
Database
The Cartesian Product Operation

Database
System
Concepts

Structure of
Notation:
Relational r ×s
Databases

Relational
Algebra
Defined as:
Additional
Relational r × s = {tq|t ∈ r and q ∈ s}
Algebra
Operations

Extended Assume that attributes of r (R) and s(S) are disjoint.


Relational
Algebra (That is, R ∪ S = ∅).
Operations
If attributes of r (R) and s(S) are not disjoint, then
Null Values

Modification
renaming must be used.
of the
Database
The Cartesian Product Operation - Example

Database
System Relations r and s
Concepts
C D E
Structure of
A B a 10 x
Relational
Databases
r = a 1 s = b 10 x
Relational b 2 b 20 y
Algebra
c 10 y
Additional
Relational r × s =?
Algebra
Operations

Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Cartesian Product Operation - Example

Database
System Relations r and s
Concepts
C D E
Structure of
A B a 10 x
Relational
Databases
r = a 1 s = b 10 x
Relational b 2 b 20 y
Algebra
c 10 y
Additional
Relational r × s =?
Algebra
Operations

Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Cartesian Product Operation - Example

Database
System Relations r and s
Concepts
C D E
Structure of
A B a 10 x
Relational
Databases
r = a 1 s = b 10 x
Relational b 2 b 20 y
Algebra
c 10 y
Additional
Relational r × s =?
Algebra A B C D E
Operations

Extended a 1 a 10 x
Relational
Algebra
a 1 b 10 x
Operations a 1 b 20 y
Null Values a 1 c 10 y
Modification b 2 a 10 x
of the
Database b 2 b 10 x
b 2 b 20 y
b 2 c 10 y
The Rename Operation

Database
System Allows us to name, and therefore to refer to, the results of
Concepts
relational-algebra expressions.
Structure of Allows us to refer to a relation by more than one name.
Relational
Databases Example:
Relational ρX (E )
Algebra

Additional returns the expression E under the name X


Relational
Algebra
Operations
If a relational-algebra expression E has arity n, then
Extended
Relational
Algebra
ρX (A1 ,A2 ,...,An ) (E )
Operations

Null Values returns the result of expression E under the name X , and
Modification with the attributes renamed to A1 , A2 , ..., An .
of the
Database ρ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 A B C D E
Relational
Databases a 1 a 10 x
Relational a 1 b 10 x
Algebra a 1 b 20 y
Additional 1 r ×s a 1 c 10 y
Relational
Algebra b 2 a 10 x
Operations
b 2 b 10 x
Extended
Relational b 2 b 20 y
Algebra
Operations
b 2 c 10 y
2 σA=C
Null Values
A B C D E
Modification
of the
3 σA=C (r × s) a 1 a 10 x
Database
b 2 b 10 x
b 2 b 20 y
Operation Composition

Database
System
Concepts
Can build expressions using multiple operations
Example: σA=C (r × s)
Structure of A B C D E
Relational
Databases a 1 a 10 x
Relational a 1 b 10 x
Algebra a 1 b 20 y
Additional 1 r ×s a 1 c 10 y
Relational
Algebra b 2 a 10 x
Operations
b 2 b 10 x
Extended
Relational b 2 b 20 y
Algebra
Operations
b 2 c 10 y
2 σA=C
Null Values
A B C D E
Modification
of the
3 σA=C (r × s) a 1 a 10 x
Database
b 2 b 10 x
b 2 b 20 y
Operation Composition

Database
System
Concepts
Can build expressions using multiple operations
Example: σA=C (r × s)
Structure of A B C D E
Relational
Databases a 1 a 10 x
Relational a 1 b 10 x
Algebra a 1 b 20 y
Additional 1 r ×s a 1 c 10 y
Relational
Algebra b 2 a 10 x
Operations
b 2 b 10 x
Extended
Relational b 2 b 20 y
Algebra
Operations
b 2 c 10 y
2 σA=C
Null Values
A B C D E
Modification
of the
3 σA=C (r × s) a 1 a 10 x
Database
b 2 b 10 x
b 2 b 20 y
Operation Composition

Database
System
Concepts
Can build expressions using multiple operations
Example: σA=C (r × s)
Structure of A B C D E
Relational
Databases a 1 a 10 x
Relational a 1 b 10 x
Algebra a 1 b 20 y
Additional 1 r ×s a 1 c 10 y
Relational
Algebra b 2 a 10 x
Operations
b 2 b 10 x
Extended
Relational b 2 b 20 y
Algebra
Operations
b 2 c 10 y
2 σA=C
Null Values
A B C D E
Modification
of the
3 σA=C (r × s) a 1 a 10 x
Database
b 2 b 10 x
b 2 b 20 y
Operation Composition

Database
System
Concepts
Can build expressions using multiple operations
Example: σA=C (r × s)
Structure of A B C D E
Relational
Databases a 1 a 10 x
Relational a 1 b 10 x
Algebra a 1 b 20 y
Additional 1 r ×s a 1 c 10 y
Relational
Algebra b 2 a 10 x
Operations
b 2 b 10 x
Extended
Relational b 2 b 20 y
Algebra
Operations
b 2 c 10 y
2 σA=C
Null Values
A B C D E
Modification
of the
3 σA=C (r × s) a 1 a 10 x
Database
b 2 b 10 x
b 2 b 20 y
Outline

Database
System
Concepts

1 Structure of Relational Databases


Structure of
Relational
Databases 2 Relational Algebra
Relational
Algebra

Additional 3 Additional Relational Algebra Operations


Relational
Algebra
Operations

Extended
4 Extended Relational Algebra Operations
Relational
Algebra
Operations
5 Null Values
Null Values

Modification
of the
Database
6 Modification of the Database
Additional Operations

Database
System
Concepts

Structure of
Relational We define additional operations that do not add any power to
Databases

Relational
the relational algebra, but that simplify common queries.
Algebra

Additional
Set intersection: ∩
Relational
Algebra Natural join: ⋊

Operations

Extended
Division: ÷
Relational
Algebra Assignment: ←
Operations

Null Values

Modification
of the
Database
The Set Intersection Operation

Database
System Notation:
Concepts
r ∩s
Structure of Defined as:
Relational
Databases

Relational r ∩ s = {t|t ∈ r and t ∈ s}


Algebra

Additional
Relational
Algebra Note: r ∩ s = r − (r − s)
Operations

Extended
For r ∩ s to be valid:
Relational
Algebra
1 r and s must have the same arity
Operations 2 The attribute domains must be compatible
Null Values
Example: to find all customers with both an account and
Modification
of the a a loan
Database

πcustomer name (depositor ) ∩ πcustomer name (borrower )


The Set Intersection Operation - Example

Database
System
Concepts

Structure of
Relational
Relations r and s
Databases
A B
Relational A B
Algebra a 1
r = s = a 2
Additional a 2
Relational b 3
Algebra
Operations
b 1
Extended r ∩ s =?
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Set Intersection Operation - Example

Database
System
Concepts

Structure of
Relational
Relations r and s
Databases
A B
Relational A B
Algebra a 1
r = s = a 2
Additional a 2
Relational b 3
Algebra
Operations
b 1
Extended r ∩ s =?
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Set Intersection Operation - Example

Database
System
Concepts

Structure of
Relational
Relations r and s
Databases
A B
Relational A B
Algebra a 1
r = s = a 2
Additional a 2
Relational b 3
Algebra
Operations
b 1
Extended r ∩ s =?
Relational
Algebra
A B
Operations
a 2
Null Values

Modification
of the
Database
The Natural Join Operation

Database
System Notation:
Concepts
r⋊
⋉s
Structure of Let r and s be relations on schemas R and S respectively.
Relational
Databases
Then, r ⋊⋉ s is a relation on schema R ∪ S obtained as
Relational
follows:
Algebra Consider each pair of tuples tr from r and ts from s
Additional If tr and ts have the same value on each of the attributes
Relational
Algebra in R ∪ S, add a tuple t to the result, where
Operations
t has the same value as tr on r
Extended t has the same value as ts on s
Relational
Algebra
Operations Example
Null Values
R = (A, B, C , D), S = (E , B, D)
Modification
of the
Database Result schema = (A, B, C , D, E )
⋉ s = πr .A,r .B,r .C ,r .D,s.E (σr .B=s.B∧r .D=s.D (r × s))
r⋊
The Natural Join Operation - Example

Database
System
Concepts Relations r and s
A B C D B D E
Structure of
Relational a 1 a x 1 x a
Databases
b 2 c x 3 x b
Relational r = s =
Algebra c 4 b y 1 x c
Additional a 1 c x 2 y d
Relational
Algebra d 2 b y 3 y e
Operations

Extended
⋉ s =?
r⋊
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Natural Join Operation - Example

Database
System
Concepts Relations r and s
A B C D B D E
Structure of
Relational a 1 a x 1 x a
Databases
b 2 c x 3 x b
Relational r = s =
Algebra c 4 b y 1 x c
Additional a 1 c x 2 y d
Relational
Algebra d 2 b y 3 y e
Operations

Extended
⋉ s =?
r⋊
Relational
Algebra
Operations

Null Values

Modification
of the
Database
The Natural Join Operation - Example

Database
System
Concepts Relations r and s
A B C D B D E
Structure of
Relational a 1 a x 1 x a
Databases
b 2 c x 3 x b
Relational r = s =
Algebra c 4 b y 1 x c
Additional a 1 c x 2 y d
Relational
Algebra d 2 b y 3 y e
Operations

Extended
⋉ s =?
r⋊
Relational A B C D E
Algebra
Operations a 1 a x a
Null Values a 1 a x c
Modification a 1 c x a
of the
Database a 1 c x c
d 2 b y d
The Division Operation

Database
System
Concepts
Notation:
r ÷s
Structure of Let r and s be relations on schemas R and S respectively,
Relational
Databases where
Relational R = (A1 , . . . , Am , B1 , . . . , Bn )
Algebra
S = (B1 , . . . , Bn )
Additional
Relational Then, r ÷ s is a relation on schema R − S obtained, such
Algebra
Operations that:
Extended
Relational
Algebra r ÷ s = {t|t ∈ πR−S (t) ∧ ∀u∈s (tu ∈ r )}
Operations

Null Values Where tu means the concatenation of tuples t and u to


Modification
of the
produce a single tuple
Database
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
A B
Structure of
a 1
Relational a 2
Databases
a 3
Relational
Algebra
b 1 B
r = c 1 s = 1
Additional
Relational d 1
Algebra d 3 2
Operations
d 4
Extended
Relational
e 6
Algebra e 1
Operations
b 2
Null Values

Modification
r ÷ s =?
of the
Database
The Division Operation - Example

Database
System
Concepts
Relations r and s
A B
Structure of
a 1
Relational a 2
Databases
a 3
Relational
Algebra
b 1 B
r = c 1 s = 1
Additional
Relational d 1
Algebra d 3 2
Operations
d 4
Extended
Relational
e 6
Algebra e 1
Operations
b 2
Null Values

Modification
r ÷ s =?
of the
Database
The Division Operation - Example

Database
System
Concepts
Relations r and s
A B
Structure of
a 1
Relational a 2
Databases
a 3
Relational
Algebra
b 1 B
r = c 1 s = 1
Additional
Relational d 1
Algebra d 3 2
Operations
d 4
Extended
Relational
e 6
Algebra e 1
Operations
b 2
Null Values

Modification
r ÷ s =?
of the A
Database
a
b
Another Division Example

Database
System
Concepts Relations r and s
A B C D E
Structure of
Relational a x a x 1
Databases
a x c x 1
Relational
Algebra a x c y 1 D E
Additional r = b x c x 1 s = x 1
Relational
Algebra b x c y 3 y 1
Operations
c x c x 1
Extended
Relational c x c y 1
Algebra
Operations c x b y 1
Null Values r ÷ s =?
Modification
of the
Database
Another Division Example

Database
System
Concepts Relations r and s
A B C D E
Structure of
Relational a x a x 1
Databases
a x c x 1
Relational
Algebra a x c y 1 D E
Additional r = b x c x 1 s = x 1
Relational
Algebra b x c y 3 y 1
Operations
c x c x 1
Extended
Relational c x c y 1
Algebra
Operations c x b y 1
Null Values r ÷ s =?
Modification
of the
Database
Another Division Example

Database
System
Concepts Relations r and s
A B C D E
Structure of
Relational a x a x 1
Databases
a x c x 1
Relational
Algebra a x c y 1 D E
Additional r = b x c x 1 s = x 1
Relational
Algebra b x c y 3 y 1
Operations
c x c x 1
Extended
Relational c x c y 1
Algebra
Operations c x b y 1
Null Values r ÷ s =?
Modification A B C
of the
Database a x c
c x c
The Assignement Operation

Database
System
The assignment operation (←) provides a convenient way
Concepts to express complex queries.
Write query as a sequential program consisting of
Structure of
a series of assignmets
Relational followed by an expression whose value is displayed as a
Databases
result of the query.
Relational
Algebra Assignment must always be made to a temporary relation
Additional
variable.
Relational
Algebra Example: write r ÷ s as
Operations

Extended
Relational temp1 ← πR−S (r )
Algebra
Operations temp2 ← πR−S ((temp1 × s) − πR−S,S (r ))
Null Values
result = temp1 − temp2
Modification
of the
Database 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

1 Structure of Relational Databases


Structure of
Relational
Databases 2 Relational Algebra
Relational
Algebra

Additional 3 Additional Relational Algebra Operations


Relational
Algebra
Operations

Extended
4 Extended Relational Algebra Operations
Relational
Algebra
Operations
5 Null Values
Null Values

Modification
of the
Database
6 Modification of the Database
Extended Operations

Database
System
Concepts

Structure of
Relational
Databases

Relational
Algebra Generalized projection
Additional
Relational
Aggregate functions
Algebra
Operations Outer join
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Generalized Projection

Database
System
Concepts
Extends the projection operation by allowing arithmetic
functions to be used in the projection list
Structure of
Relational πF1 ,F2 ,...,Fn (E )
Databases

Relational
Algebra E is any relational-algebra expression
Additional
Relational
Each of F1 , F2 , . . . , Fn are are arithmetic expressions
Algebra involving constants and attributes in the schema of E .
Operations

Extended
Relational
Example
Algebra
Operations Given relation
Null Values credit info(customer name, limit, credit balance)
Modification
of the
find how much more each person can spend.
Database

πcustomer name,limit−credit balance (credit info)


Generalized Projection - Example

Database
System
Concepts

Relation r
Structure of A B C
Relational
Databases a 10 1
Relational a 20 1
Algebra

Additional
b 30 1
Relational
Algebra
b 40 2
Operations
πA,C ∗10 (r ) =?
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Generalized Projection - Example

Database
System
Concepts

Relation r
Structure of A B C
Relational
Databases a 10 1
Relational a 20 1
Algebra

Additional
b 30 1
Relational
Algebra
b 40 2
Operations
πA,C ∗10 (r ) =?
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Generalized Projection - Example

Database
System
Concepts

Relation r
Structure of A B C
Relational
Databases a 10 1
Relational a 20 1
Algebra

Additional
b 30 1
Relational
Algebra
b 40 2
Operations
πA,C ∗10 (r ) =?
Extended
Relational A C*10
Algebra
Operations a 10
Null Values b 10
Modification
of the
b 20
Database
Aggregate Functions and Operations

Database
System Aggregation function takes a collection of values and
Concepts
returns a single value as a result.
avg: average value
Structure of
Relational min: minimum value
Databases
max: maximum value
Relational
Algebra sum: sum of values
Additional
Relational
count: number of values
Algebra
Operations
Aggregate operation in relational algebra
Extended
Relational G1 ,G2 ,...,Gn GF1 (A1 ),F2 (A2 ),...,Fn (An ) (E )
Algebra
Operations

Null Values E is any relational-algebra expression


Modification G1 , G2 . . . , Gn is a list of attributes on which to group (can
of the
Database be empty)
Each Fi is an aggregate function
Each Ai is an attribute name
Aggregate Operation - Example

Database
System
Concepts

Structure of Relation r
Relational
Databases
A B C
Relational a a 7
Algebra
a b 7
Additional
Relational b b 3
Algebra
Operations b b 10
Extended
Relational
Gsum(C ) (r ) =?
Algebra
Operations

Null Values

Modification
of the
Database
Aggregate Operation - Example

Database
System
Concepts

Structure of Relation r
Relational
Databases
A B C
Relational a a 7
Algebra
a b 7
Additional
Relational b b 3
Algebra
Operations b b 10
Extended
Relational
Gsum(C ) (r ) =?
Algebra
Operations

Null Values

Modification
of the
Database
Aggregate Operation - Example

Database
System
Concepts

Structure of Relation r
Relational
Databases
A B C
Relational a a 7
Algebra
a b 7
Additional
Relational b b 3
Algebra
Operations b b 10
Extended
Relational
Gsum(C ) (r ) =?
Algebra
Operations
sum(C)
Null Values 27
Modification
of the
Database
Aggregate Operation - Example

Database
System Relation account grouped by branch name
Concepts

branch name account number balance


Structure of
Relational
Perryridge A-102 400
Databases Perryridge A-201 900
Relational
Algebra
Brighton A-217 750
Additional
Brighton A-215 750
Relational
Algebra
Redwood A-222 700
Operations

Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Aggregate Operation - Example

Database
System Relation account grouped by branch name
Concepts

branch name account number balance


Structure of
Relational
Perryridge A-102 400
Databases Perryridge A-201 900
Relational
Algebra
Brighton A-217 750
Additional
Brighton A-215 750
Relational
Algebra
Redwood A-222 700
Operations

Extended branch name Gsum(balance) (account)


Relational
Algebra
Operations

Null Values

Modification
of the
Database
Aggregate Operation - Example

Database
System Relation account grouped by branch name
Concepts

branch name account number balance


Structure of
Relational
Perryridge A-102 400
Databases Perryridge A-201 900
Relational
Algebra
Brighton A-217 750
Additional
Brighton A-215 750
Relational
Algebra
Redwood A-222 700
Operations

Extended branch name Gsum(balance) (account)


Relational
Algebra
Operations

Null Values branch name sum(balance)


Modification
of the Perryridge 1300
Database
Brighton 1500
Redwood 700
Aggregate Functions (cont.)

Database
System
Concepts
Result of aggregation does not have a name
Structure of Can use rename operation to give it a name
Relational
Databases
For convenience, we permit renaming as part of aggregate
Relational
operation
Algebra

Additional
branch name Gsum(balance) as sum balance (account)
Relational
Algebra
Operations

Extended
Relational
branch name sum balance
Algebra
Operations Perryridge 1300
Null Values Brighton 1500
Modification Redwood 700
of the
Database
Outer Join

Database
System
Concepts

An extension of the join operation that avoids loss of


Structure of
Relational information.
Databases

Relational
Computes the join and then adds tuples form one relation
Algebra
that does not match tuples in the other relation to the
Additional
Relational result of the join.
Algebra
Operations Uses null values:
Extended null signifies that the value is unknown or does not exist
Relational
Algebra
All comparisons involving null are (roughly speaking) false
Operations by definition.
Null Values We shall study precise meaning of comparisons with nulls
Modification later
of the
Database
Outer Join - Example

Database
System
Concepts

Structure of
Relational
Databases
Relations loan and borrower
Relational
Algebra
loan number branch name amount customer name loan number
Additional
Relational L-170 Downtown 3000 Jones L-170
Algebra
Operations L-230 Redwood 4000 Smith L-230
Extended
L-260 Perryridge 1700 Hayes L-155
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Outer Join - Example

Database
System
Concepts

Relations loan and borrower


Structure of
Relational
Databases loan number branch name amount customer name loan number

Relational L-170 Downtown 3000 Jones L-170


Algebra
L-230 Redwood 4000 Smith L-230
Additional L-260 Perryridge 1700 Hayes L-155
Relational
Algebra
Operations

Extended Inner join: loan ⋊


⋉ borrower
Relational
Algebra loan number branch name amount customer name
Operations

Null Values
L-170 Downtown 3000 Jones
Modification L-230 Redwood 4000 Smith
of the
Database
Outer Join - Example

Database
System
Concepts
Relations loan and borrower
Structure of
Relational loan number branch name amount customer name loan number
Databases
L-170 Downtown 3000 Jones L-170
Relational
Algebra
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155
Additional
Relational
Algebra

Left outer join: loan−


Operations
⋉ borrower
−⋊
Extended
Relational
loan number branch name amount customer name
Algebra
Operations
L-170 Downtown 3000 Jones
Null Values
L-230 Redwood 4000 Smith
Modification
of the L-260 Perryridge 1700 null
Database
Outer Join - Example

Database
System
Concepts
Relations loan and borrower
Structure of
Relational loan number branch name amount customer name loan number
Databases
L-170 Downtown 3000 Jones L-170
Relational
Algebra
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155
Additional
Relational
Algebra

⋉−
Operations
Right outer join: loan ⋊−borrower
Extended
Relational
loan number branch name amount customer name
Algebra
Operations
L-170 Downtown 3000 Jones
Null Values
L-230 Redwood 4000 Smith
Modification
of the L-155 null null Hayes
Database
Outer Join - Example

Database
System
Concepts
Relations loan and borrower
Structure of loan number branch name amount customer name loan number
Relational
Databases L-170 Downtown 3000 Jones L-170
Relational L-230 Redwood 4000 Smith L-230
Algebra L-260 Perryridge 1700 Hayes L-155
Additional
Relational
Algebra
Operations Full outer join: loan−⋉−
−⋊−borrower
Extended
Relational loan number branch name amount customer name
Algebra
Operations L-170 Downtown 3000 Jones
Null Values L-230 Redwood 4000 Smith
Modification
of the
L-260 Perryridge 1700 null
Database L-155 null null Hayes
Outline

Database
System
Concepts

1 Structure of Relational Databases


Structure of
Relational
Databases 2 Relational Algebra
Relational
Algebra

Additional 3 Additional Relational Algebra Operations


Relational
Algebra
Operations

Extended
4 Extended Relational Algebra Operations
Relational
Algebra
Operations
5 Null Values
Null Values

Modification
of the
Database
6 Modification of the Database
Null Values

Database
System
Concepts

It is possible for tuples to have a null value, denoted by


Structure of
Relational null, for some of their attributes
Databases

Relational
null signifies an unknown value or that a value does not
Algebra exist.
Additional
Relational The result of any arithmetic expression involving null is
Algebra
Operations null
Extended
Relational
Aggregate functions simply ignore null values (as in SQL)
Algebra
Operations For duplicate elimination and grouping, null is treated like
Null Values any other value, and two nulls are assumed to be the same
Modification (as in SQL)
of the
Database
Null Values

Database
System Comparisons with null values return the special truth
Concepts
value: unknown

Structure of x y x OR y
Relational unknown true true
Databases
unknown false unknown
Relational
Algebra
unknown unknown unknown
Additional
Relational
Algebra x y x AND y
Operations
unknown true unknown
Extended unknown false false
Relational
Algebra unknown unknown unknown
Operations

Null Values

Modification
x NOT x
of the unknown unknown
Database

Result of select predicate is treated as false if it evaluates


to unknown
Outline

Database
System
Concepts

1 Structure of Relational Databases


Structure of
Relational
Databases 2 Relational Algebra
Relational
Algebra

Additional 3 Additional Relational Algebra Operations


Relational
Algebra
Operations

Extended
4 Extended Relational Algebra Operations
Relational
Algebra
Operations
5 Null Values
Null Values

Modification
of the
Database
6 Modification of the Database
Modification of the Database

Database
System
Concepts

Structure of
Relational
Databases The content of the database may be modified using the
Relational following operations:
Algebra
Deletion
Additional
Relational
Insertion
Algebra Updating
Operations

Extended All these operations are expressed using the assignment


Relational
Algebra operator.
Operations

Null Values

Modification
of the
Database
Deletion

Database
System
Concepts

Structure of
A delete request is expressed similarly to a query, except
Relational
Databases
instead of displaying tuples to the user, the selected tuples
Relational
are removed from the database.
Algebra
Can delete only whole tuples; cannot delete values on only
Additional
Relational particular attributes
Algebra
Operations A deletion is expressed in relational algebra by:
Extended
Relational
Algebra r ←r −E
Operations

Null Values
where r is a relation and E is a relational algebra query.
Modification
of the
Database
Deletion Examples

Database
System Delete all account records in the Perryridge branch.
Concepts

Structure of
Relational
Databases Delete all loan records with amount in the range of 0 to
Relational
Algebra
50
Additional
Relational
Algebra
Operations
Delete all accounts at branches located in Needham
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Deletion Examples

Database
System Delete all account records in the Perryridge branch.
Concepts
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases Delete all loan records with amount in the range of 0 to
Relational
Algebra
50
Additional
Relational
Algebra
Operations
Delete all accounts at branches located in Needham
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Deletion Examples

Database
System Delete all account records in the Perryridge branch.
Concepts
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases Delete all loan records with amount in the range of 0 to
Relational
Algebra
50
Additional
Relational
Algebra
Operations
Delete all accounts at branches located in Needham
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Deletion Examples

Database
System Delete all account records in the Perryridge branch.
Concepts
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases Delete all loan records with amount in the range of 0 to
Relational
Algebra
50
Additional
loan ← loan − σamount≥0 and amount≤50 (loan)
Relational
Algebra
Operations
Delete all accounts at branches located in Needham
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Deletion Examples

Database
System Delete all account records in the Perryridge branch.
Concepts
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases Delete all loan records with amount in the range of 0 to
Relational
Algebra
50
Additional
loan ← loan − σamount≥0 and amount≤50 (loan)
Relational
Algebra
Operations
Delete all accounts at branches located in Needham
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Deletion Examples

Database
System Delete all account records in the Perryridge branch.
Concepts
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases Delete all loan records with amount in the range of 0 to
Relational
Algebra
50
Additional
loan ← loan − σamount≥0 and amount≤50 (loan)
Relational
Algebra
Operations
Delete all accounts at branches located in Needham
Extended
Relational
Algebra
r1 ← σbranch city =′′ Needham′′ (account ⋊
⋉ branch)
Operations

Null Values

Modification
of the
Database
Deletion Examples

Database
System Delete all account records in the Perryridge branch.
Concepts
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases Delete all loan records with amount in the range of 0 to
Relational
Algebra
50
Additional
loan ← loan − σamount≥0 and amount≤50 (loan)
Relational
Algebra
Operations
Delete all accounts at branches located in Needham
Extended
Relational
Algebra
r1 ← σbranch city =′′ Needham′′ (account ⋊
⋉ branch)
Operations
r2 ← πbranch name,account number ,balance (r1 )
Null Values

Modification
of the
Database
Deletion Examples

Database
System Delete all account records in the Perryridge branch.
Concepts
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases Delete all loan records with amount in the range of 0 to
Relational
Algebra
50
Additional
loan ← loan − σamount≥0 and amount≤50 (loan)
Relational
Algebra
Operations
Delete all accounts at branches located in Needham
Extended
Relational
Algebra
r1 ← σbranch city =′′ Needham′′ (account ⋊
⋉ branch)
Operations
r2 ← πbranch name,account number ,balance (r1 )
Null Values

Modification account ← account − r2


of the
Database
Deletion Examples

Database
System Delete all account records in the Perryridge branch.
Concepts
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases Delete all loan records with amount in the range of 0 to
Relational
Algebra
50
Additional
loan ← loan − σamount≥0 and amount≤50 (loan)
Relational
Algebra
Operations
Delete all accounts at branches located in Needham
Extended
Relational
Algebra
r1 ← σbranch city =′′ Needham′′ (account ⋊
⋉ branch)
Operations
r2 ← πbranch name,account number ,balance (r1 )
Null Values

Modification account ← account − r2


of the
Database r3 ← πcustomer name,account number (r2 ⋉ depositor )

Deletion Examples

Database
System Delete all account records in the Perryridge branch.
Concepts
account ← account − σbranch name=′′ Perryridge ′′ (account)
Structure of
Relational
Databases Delete all loan records with amount in the range of 0 to
Relational
Algebra
50
Additional
loan ← loan − σamount≥0 and amount≤50 (loan)
Relational
Algebra
Operations
Delete all accounts at branches located in Needham
Extended
Relational
Algebra
r1 ← σbranch city =′′ Needham′′ (account ⋊
⋉ branch)
Operations
r2 ← πbranch name,account number ,balance (r1 )
Null Values

Modification account ← account − r2


of the
Database r3 ← πcustomer name,account number (r2 ⋉ depositor )

depositor ← depositor − r3
Insertion

Database
System
Concepts

To insert data into a relation, we either:


Structure of
Relational specify a tuple to be inserted
Databases write a query whose result is a set of tuples to be inserted
Relational
Algebra In relational algebra, an insertion is expressed by:
Additional
Relational
Algebra
r ←r ∪E
Operations

Extended
Relational
where r is a relation and E is a relational algebra
Algebra expression.
Operations

Null Values The insertion of a single tuple is expressed by letting E be


Modification a constant relation containing one tuple.
of the
Database
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 Provide as a gift for all loan customers in the Perryridge


Relational
Algebra branch, a $200 savings account. Let the loan number
Operations
serve as the account number for the new savings account.
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
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 account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
Databases

Relational
Algebra

Additional Provide as a gift for all loan customers in the Perryridge


Relational
Algebra branch, a $200 savings account. Let the loan number
Operations
serve as the account number for the new savings account.
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
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 account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
Databases

Relational
depositor ← depositor ∪ {{′′ Smith′′ , A − 973}}
Algebra

Additional Provide as a gift for all loan customers in the Perryridge


Relational
Algebra branch, a $200 savings account. Let the loan number
Operations
serve as the account number for the new savings account.
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
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 account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
Databases

Relational
depositor ← depositor ∪ {{′′ Smith′′ , A − 973}}
Algebra

Additional Provide as a gift for all loan customers in the Perryridge


Relational
Algebra branch, a $200 savings account. Let the loan number
Operations
serve as the account number for the new savings account.
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
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 account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
Databases

Relational
depositor ← depositor ∪ {{′′ Smith′′ , A − 973}}
Algebra

Additional Provide as a gift for all loan customers in the Perryridge


Relational
Algebra branch, a $200 savings account. Let the loan number
Operations
serve as the account number for the new savings account.
Extended
Relational
Algebra
Operations
r1 ← σbranch name=′′ Perryridge ′′ (borrower ⋊
⋉ loan)
Null Values

Modification
of the
Database
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 account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
Databases

Relational
depositor ← depositor ∪ {{′′ Smith′′ , A − 973}}
Algebra

Additional Provide as a gift for all loan customers in the Perryridge


Relational
Algebra branch, a $200 savings account. Let the loan number
Operations
serve as the account number for the new savings account.
Extended
Relational
Algebra
Operations
r1 ← σbranch name=′′ Perryridge ′′ (borrower ⋊
⋉ loan)
Null Values account ← account ∪ πbranch name,loan number ,200 (r1 )
Modification
of the
Database
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 account ← account ∪ {{′′ Perryridge ′′ , A − 973, 1200}}
Databases

Relational
depositor ← depositor ∪ {{′′ Smith′′ , A − 973}}
Algebra

Additional Provide as a gift for all loan customers in the Perryridge


Relational
Algebra branch, a $200 savings account. Let the loan number
Operations
serve as the account number for the new savings account.
Extended
Relational
Algebra
Operations
r1 ← σbranch name=′′ Perryridge ′′ (borrower ⋊
⋉ loan)
Null Values account ← account ∪ πbranch name,loan number ,200 (r1 )
Modification
of the
depositor ← depositor ∪ πcustomer name,loan number (r1 )
Database
Updating

Database
System
Concepts

A mechanism to change a value in a tuple without


Structure of
Relational
charging all values in the tuple
Databases
Use the generalized projection operator to do this task
Relational
Algebra

Additional r ← πF1 ,F2 ,...,Fn (r )


Relational
Algebra
Operations Each Fi is either
Extended the i-th attribute of r , if the i-th attribute is not updated,
Relational
Algebra or,
Operations
if the attribute is to be updated Fi is an expression,
Null Values
involving only constants and the attributes of r , which
Modification
of the
gives the new value for the attribute
Database
Updating Examples

Database
System
Concepts Make interest payments by increasing all balances by 5
percent.
Structure of
Relational
Databases

Relational
Algebra

Additional
Relational Pay all accounts with balances over $10,000 6 percent
Algebra
Operations interest and pay all others 5 percent
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Updating Examples

Database
System
Concepts Make interest payments by increasing all balances by 5
percent.
Structure of
Relational
Databases account ← πaccount number ,branch name,balance×1.05 (account)
Relational
Algebra

Additional
Relational Pay all accounts with balances over $10,000 6 percent
Algebra
Operations interest and pay all others 5 percent
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Updating Examples

Database
System
Concepts Make interest payments by increasing all balances by 5
percent.
Structure of
Relational
Databases account ← πaccount number ,branch name,balance×1.05 (account)
Relational
Algebra

Additional
Relational Pay all accounts with balances over $10,000 6 percent
Algebra
Operations interest and pay all others 5 percent
Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database
Updating Examples

Database
System
Concepts Make interest payments by increasing all balances by 5
percent.
Structure of
Relational
Databases account ← πaccount number ,branch name,balance×1.05 (account)
Relational
Algebra

Additional
Relational Pay all accounts with balances over $10,000 6 percent
Algebra
Operations interest and pay all others 5 percent
Extended
Relational
Algebra
r1 ← σbalance>10000 (account)
Operations

Null Values

Modification
of the
Database
Updating Examples

Database
System
Concepts Make interest payments by increasing all balances by 5
percent.
Structure of
Relational
Databases account ← πaccount number ,branch name,balance×1.05 (account)
Relational
Algebra

Additional
Relational Pay all accounts with balances over $10,000 6 percent
Algebra
Operations interest and pay all others 5 percent
Extended
Relational
Algebra
r1 ← σbalance>10000 (account)
Operations
r2 ← σbalance≤10000 (account)
Null Values

Modification
of the
Database
Updating Examples

Database
System
Concepts Make interest payments by increasing all balances by 5
percent.
Structure of
Relational
Databases account ← πaccount number ,branch name,balance×1.05 (account)
Relational
Algebra

Additional
Relational Pay all accounts with balances over $10,000 6 percent
Algebra
Operations interest and pay all others 5 percent
Extended
Relational
Algebra
r1 ← σbalance>10000 (account)
Operations
r2 ← σbalance≤10000 (account)
Null Values

Modification
account ← πaccount number ,branch name,balance×1.06 (r1 )
of the
Database ∪ πaccount number ,branch name,balance×1.05 (r2 )
Database
System
Concepts

Structure of
Relational
Databases

Relational
Algebra

Additional
Relational End of Chapter 2
Algebra
Operations

Extended
Relational
Algebra
Operations

Null Values

Modification
of the
Database

You might also like