Professional Documents
Culture Documents
Lecture 5 Relational Algebra
Lecture 5 Relational Algebra
Lecture 5 Relational Algebra
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
Modification
of the 6 Modification of the Database
Database
Outline
Database
System
Concepts
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
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
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
Null Values
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
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
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
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
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
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
Null Values
Modification
of the
Database
The Set Difference Operation - Example
Database
System
Concepts
Null Values
Modification
of the
Database
The Set Difference Operation - Example
Database
System
Concepts
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
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
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
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
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
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
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
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
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
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
Extended
Relational
Algebra
Operations
Null Values
Modification
of the
Database
Aggregate Operation - Example
Database
System Relation account grouped by branch name
Concepts
Null Values
Modification
of the
Database
Aggregate Operation - Example
Database
System Relation account grouped by branch name
Concepts
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
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
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
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
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
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
Database
System
Concepts
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
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
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
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
Database
System
Concepts
Extended
Relational
where r is a relation and E is a relational algebra
Algebra expression.
Operations
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
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
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
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
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
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
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
Database
System
Concepts
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