relational algebra...relational algebra basic structure – consider the account table. – it has...
TRANSCRIPT
![Page 1: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/1.jpg)
Relational Algebra
Relational
Algebra
7/4/2017 1Md. Golam Moazzam, Dept. of CSE, JU
![Page 2: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/2.jpg)
Relational Algebra
Structure of Relational Databases
– A relational database consists of a collection of tables, each of which is
assigned a unique name.
– A row in a table represents a relationship among a set of values. Since
a table is a collection of such relationships, there is a close
correspondence between the concept of table and the mathematical
concept of relation, from which the relational data model takes its
name.
– Example of a Relation:
7/4/2017 2Md. Golam Moazzam, Dept. of CSE, JU
![Page 3: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/3.jpg)
Relational Algebra
Basic Structure
– Consider the account table.
– It has three column headers: account-number, branch-name, and
balance.
– For each attribute, there is a set of permitted values, called the domain
of that attribute.
– For the attribute branch-name, for example, the domain is the set of all
branch names.
– Let D1 denote the set of all account numbers,
D2 the set of all branch names, and
D3 the set of all balances.
Any row of account must consist of a 3-tuple (v1, v2, v3), where v1 is an
account number (that is, v1 is in domain D1), v2 is a branch name (that
is, v2 is in domain D2), and v3 is a balance (that is, v3 is in domain D3).
7/4/2017 3Md. Golam Moazzam, Dept. of CSE, JU
![Page 4: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/4.jpg)
Relational Algebra
Tuple Variable
– A tuple variable is a variable that stands for a tuple.
– In other words, a tuple variable is a variable whose domain is
the set of all tuples.
– In the account relation, there are seven tuples.
– Let the tuple variable t refer to the first tuple of the relation.
– Thus, t[account-number] = “A-101,” and t[branch-name] =
“Downtown”.
– Alternatively, we may write t[1] to denote the value of tuple t
on the first attribute (account-number), t[2] to denote branch-
name, and so on.
– Since a relation is a set of tuples, we use the mathematical
notation of t ∈ r to denote that tuple t is in relation r.
7/4/2017 4Md. Golam Moazzam, Dept. of CSE, JU
![Page 5: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/5.jpg)
Relational Algebra
Attribute Types
– Each attribute of a relation has a name.
– The set of allowed values for each attribute is called the
domain of the attribute.
– Attribute values are (normally) required to be atomic,
that is, indivisible.
• E.g. multivalued attribute values are not atomic.
• E.g. composite attribute values are not atomic.
– The special value null is a member of every domain.
– The null value causes complications in the definition of
many operations.
7/4/2017 5Md. Golam Moazzam, Dept. of CSE, JU
![Page 6: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/6.jpg)
Relational Algebra
Query Languages
– A query language is a language in which a user requests
information from the database.
– These languages are usually on a level higher than that of a
standard programming language.
– Query languages can be categorized as either procedural or
nonprocedural.
– In a procedural language, the user instructs the system to
perform a sequence of operations on the database to compute
the desired result.
– In a nonprocedural language, the user describes the desired
information without giving a specific procedure for obtaining
that information.
– Very widely used query language is SQL.
– Some other query languages: QBE and Datalog.
7/4/2017 6Md. Golam Moazzam, Dept. of CSE, JU
![Page 7: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/7.jpg)
Relational Algebra
Relational Algebra
– Relational algebra is a procedural query language.
– It consists of a set of operations that take one or two relations
as input and produce a new relation as their result.
– The fundamental operations in the relational algebra are select,
project, union, set difference, Cartesian product, and rename.
– select, project, and rename operations are called unary
operations, because they operate on one relation.
– The other three operations operate on pairs of relations and are,
therefore, called binary operations.
– There are several other operations—namely, set intersection,
natural join, division, and assignment.
7/4/2017 7Md. Golam Moazzam, Dept. of CSE, JU
![Page 8: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/8.jpg)
Relational Algebra
Consider the following database
7/4/2017 8Md. Golam Moazzam, Dept. of CSE, JU
branch(branch- name, branch-city, assets)
customer(customer-name, customer-street, customer-city)
loan(loan-number, branch-name, amount)
borrower(customer-name, loan-number)
account(account-number, branch-name, balance)
depositor(customer-name, account-number)
![Page 9: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/9.jpg)
Relational Algebra
Select Operation
– The select operation selects tuples that satisfy a given
predicate.
– The lowercase Greek letter sigma (σ) used to denote
selection.
– The predicate appears as a subscript to σ.
– The argument relation is in parentheses after the σ.
– Notation: p(r)
p is called the selection predicate.
7/4/2017 9Md. Golam Moazzam, Dept. of CSE, JU
![Page 10: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/10.jpg)
Relational Algebra
Select Operation
– Example:
“Select those tuples of the loan relation where the branch is
Perryridge”.
σbranch-name =“Perryridge” (loan)
“Find all tuples in which the amount lent is more than $1200”.
σamount>1200 (loan)
Allow comparisons using =, ≠, <, ≤, >, ≥ in the selection
predicate.
We can combine several predicates by using the connectives:
and (∧), or (∨), and not (¬).
7/4/2017 10Md. Golam Moazzam, Dept. of CSE, JU
![Page 11: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/11.jpg)
Relational Algebra
Select Operation
– “Find those tuples pertaining to loans of more than $1200
made by the Perryridge branch”.
σbranch-name =“Perryridge”∧ amount>1200 (loan)
7/4/2017 11Md. Golam Moazzam, Dept. of CSE, JU
![Page 12: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/12.jpg)
Relational Algebra
Project Operation
– Project operation is a unary operation that returns its
argument relation, with certain attributes left out.
– Since a relation is a set, any duplicate rows are
eliminated.
– Projection is denoted by the uppercase Greek letter pi
(Π).
– We list those attributes that we wish to appear in the
result as a subscript to Π.
– The argument relation follows in parentheses.
– Notation:
A1, A2, …, Ak (r)
– where A1, A2 are attribute names and r is a relation name.
7/4/2017 12Md. Golam Moazzam, Dept. of CSE, JU
![Page 13: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/13.jpg)
Relational Algebra
Project Operation
– Example:
“List all loan numbers and the amount of the loan”.
Πloan-number, amount(loan)
– “Find those customers who live in Harrison.”
Πcustomer-name (σcustomer-city =“Harrison” (customer))
7/4/2017 13Md. Golam Moazzam, Dept. of CSE, JU
![Page 14: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/14.jpg)
Relational Algebra
Union Operation
– Notation: r s
– Defined as:
r s = {t | t r or t s}
– For r s to be valid:
r, s must have the same number of attributes.
Attribute domains must be compatible.
– Example:
“Find the names of all bank customers who have either an
account or a loan or both”.
Πcustomer-name (borrower ) ∪ Πcustomer-name (depositor)
– Since relations are sets, duplicate values are eliminated.
7/4/2017 14Md. Golam Moazzam, Dept. of CSE, JU
![Page 15: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/15.jpg)
Relational Algebra
Set Difference Operation
– Set-difference operation, denoted by −, allows us to find tuples
that are in one relation but are not in another.
– Notation: r – s
– Defined as:
r – s = {t | t r and t s}
– Set differences must be taken between compatible relations.
r and s must have the same number of attributes.
Attribute domains of r and s must be compatible.
– Example:
“Find all customers of the bank who have an account but not a
loan”.
Πcustomer-name (depositor) − Πcustomer-name (borrower )
7/4/2017 15Md. Golam Moazzam, Dept. of CSE, JU
![Page 16: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/16.jpg)
Relational Algebra
Cartesian-Product Operation
– Cartesian-product operation, denoted by a cross (×),
allows us to combine information from any two relations.
– Notation: r x s
– Defined as:
r x s = {t q | t r and q s}
– Assume that attributes of r(R) and s(S) are disjoint.
(That is, R S = ).
– If attributes of r(R) and s(S) are not disjoint, then
renaming must be used.
7/4/2017 16Md. Golam Moazzam, Dept. of CSE, JU
![Page 17: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/17.jpg)
Relational Algebra
Cartesian-Product Operation
– Example: Relations r, s:
– Thus, r x s:
7/4/2017 17Md. Golam Moazzam, Dept. of CSE, JU
A B
1
2
C D
10
10
20
10
E
a
a
b
b
r s
A B
1
1
1
1
2
2
2
2
C D
10
10
20
10
10
10
20
10
E
a
a
b
b
a
a
b
b
![Page 18: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/18.jpg)
Relational Algebra
Cartesian-Product Operation
– Example: A=C(r x s)
– r = borrower × loan is
(customer-name, borrower.loan-number, loan.loan-
number, branch-name, amount)
– “Find the names of all customers who have a loan at the
Perryridge branch”.
Πcustomer-name (σborrower .loan-number =loan.loan-number (σbranch-name
=“Perryridge” (borrower × loan)))
7/4/2017 18Md. Golam Moazzam, Dept. of CSE, JU
A B C D E
1
2
2
10
20
20
a
a
b
![Page 19: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/19.jpg)
Relational Algebra
Rename Operation
– rename operator, denoted by the lowercase Greek letter
rho (ρ).
– Notation: ρx(E)
returns the result of expression E under the name x.
– A second form of the rename operation is as follows.
ρx(A1,A2,...,An) (E)
returns the result of expression E under the name x, and
with the attributes renamed to A1,A2, . . .,An.
7/4/2017 19Md. Golam Moazzam, Dept. of CSE, JU
![Page 20: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/20.jpg)
Relational Algebra
Rename Operation
– Example:
“Find the largest account balance in the bank.”
Πbalance (account) − Πaccount .balance (σaccount .balance < d.balance
(account × ρd (account)))
“Find the names of all customers who live on the same
street and in the same city as Smith.”
Πcustomer.customer-name(σcustomer.customer-street=smith-addr.street ∧
customer.customer-city=smith-addr.city (customer × ρsmith-addr(street,city)
(Πcustomer-street, customer-city(σcustomer-name= “Smith”(customer)))))
7/4/2017 20Md. Golam Moazzam, Dept. of CSE, JU
![Page 21: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/21.jpg)
Relational Algebra
Set-Intersection Operation
– set intersection denoted by (∩).
– Notation: r s
– Defined as:
r s ={ t | t r and t s }
– Assume:
r and s must have the same number of attributes.
Attribute domains of r and s must be compatible.
7/4/2017 21Md. Golam Moazzam, Dept. of CSE, JU
![Page 22: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/22.jpg)
Relational Algebra
Set-Intersection Operation
– Relation r, s:
– Thus, r s :
– Example:
“Find all customers who have both a loan and an
account”.
Πcustomer-name (borrower ) ∩ Πcustomer-name (depositor)
7/4/2017 22Md. Golam Moazzam, Dept. of CSE, JU
A B
1
2
1
A B
2
3
rs
A B
2
![Page 23: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/23.jpg)
Relational Algebra
Natural-Join Operation
– natural join is a binary operation that allows us to
combine certain selections and a Cartesian product into
one operation.
– It is denoted by the “join” symbol :
– natural-join operation forms a Cartesian product of its
two arguments, performs a selection forcing equality on
those attributes that appear in both relation schemas, and
finally removes duplicate attributes.
– Notation: r s
– Let R = (A, B, C, D) and S = (E, B, D)
Result schema = (A, B, C, D, E)
– r s is defined as:
– r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
7/4/2017 23Md. Golam Moazzam, Dept. of CSE, JU
![Page 24: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/24.jpg)
Relational Algebra
Natural-Join Operation
– Example:
“Find the names of all customers who have a loan at the
bank, along with the loan number and the loan amount.”
– Using Cartesian product:
Πcustomer-name, loan.loan-number, amount
(σborrower .loan-number =loan.loan-number (borrower × loan))
– Using Natural-Join operation:
Πcustomer-name, loan-number, amount (borrower loan)
7/4/2017 24Md. Golam Moazzam, Dept. of CSE, JU
![Page 25: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/25.jpg)
Relational Algebra
Natural-Join Operation
– “Find the names of all branches with customers who
have an account in the bank and who live in Harrison.”
Πbranch-name (σcustomer-city =“Harrison”
(customer account depositor))
7/4/2017 25Md. Golam Moazzam, Dept. of CSE, JU
![Page 26: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/26.jpg)
Relational Algebra
Division Operation
– division operation, denoted by ÷, is suited to queries that
include the phrase “for all.”
– Example:
“Find all customers who have an account at all the branches
located in Brooklyn.”
r1 = Πbranch-name (σbranch-city =“Brooklyn” (branch))
r2 = Πcustomer-name, branch-name (depositor account)
– Now, find customers who appear in r2 with every branch name
in r1.
Πcustomer-name, branch-name (depositor account)
÷ Πbranch-name (σbranch-city =“Brooklyn” (branch))
7/4/2017 26Md. Golam Moazzam, Dept. of CSE, JU
![Page 27: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/27.jpg)
Relational Algebra
Assignment Operation
– Assignment operation () provides a convenient way to
express complex queries.
Aggregate Functions
– Aggregate functions take a collection of values as input
and return a single value as a result.
• avg: average value
• min: minimum value
• max: maximum value
• sum: sum of values
• count: number of values
7/4/2017 27Md. Golam Moazzam, Dept. of CSE, JU
![Page 28: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/28.jpg)
Relational Algebra
Aggregate Functions
– The aggregate function avg returns the average of the
values.
– The aggregate function min returns the minimum value
in a collection.
– The aggregate function max returns the maximum value
in a collection.
– The aggregate function sum returns the sum of the
values.
– The aggregate function count returns the number of the
elements in the collection.
7/4/2017 28Md. Golam Moazzam, Dept. of CSE, JU
![Page 29: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/29.jpg)
Relational Algebra
Aggregate Functions
– Aggregate operation in relational algebra:
G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E)
– E is any relational-algebra expression.
– G1, G2 …, Gn is a list of attributes on which to group
(group by).
– Each Fi is an aggregate function.
– Each Ai is an attribute name.
7/4/2017 29Md. Golam Moazzam, Dept. of CSE, JU
![Page 30: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/30.jpg)
Relational Algebra
Aggregate Functions
– “Find out the total sum of salaries of all the part-time
employees in the bank.”
gsum(salary)(pt-works)
; pt-works(employee-name, branch-name, salary)
– The symbol g is the letter G in calligraphic font; read it
as “calligraphic G.”
– Its subscript specifies the aggregate operation to be
applied.
7/4/2017 30Md. Golam Moazzam, Dept. of CSE, JU
![Page 31: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/31.jpg)
Relational Algebra
Aggregate Functions
– “Find the number of branches appearing in the pt-works
relation.”
gcount - distinct(branch-name)(pt-works)
– “Find the total salary sum of all part-time employees at
each branch of the bank.”
branch-name gsum(salary)(pt-works)
– The attribute branch-name in the left-hand subscript of G
indicates that the input relation pt-works must be divided
into groups based on the value of branch-name.
7/4/2017 31Md. Golam Moazzam, Dept. of CSE, JU
![Page 32: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/32.jpg)
Relational Algebra
Outer Join
– outer-join operation is an extension of the join operation
to deal with missing information.
– It avoids loss of information.
– Consider loan and borrower relations:
7/4/2017 32Md. Golam Moazzam, Dept. of CSE, JU
3000
4000
1700
loan-number amount
L-170
L-230
L-260
branch-name
Downtown
Redwood
Perryridge
customer-name loan-number
Jones
Smith
Hayes
L-170
L-230
L-155
![Page 33: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/33.jpg)
Relational Algebra
Outer Join
– Inner join operation (natural join)
– loan borrower
– We have lost some information. We can use the outer-join
operation to avoid this loss of information.
– There are actually three forms of the outer-join operation:
• left outer join, denoted by ;
• right outer join, denoted by and
• full outer join, denoted by .
7/4/2017 33Md. Golam Moazzam, Dept. of CSE, JU
loan-number amount
L-170
L-230
3000
4000
customer-name
Jones
Smith
branch-name
Downtown
Redwood
![Page 34: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/34.jpg)
Relational Algebra
Outer Join
– Left-outer join
– loan borrower
– left outer join takes all tuples in the left relation that did
not match with any tuple in the right relation, pads the
tuples with null values for all other attributes from the
right relation, and adds them to the result of the natural
join.
7/4/2017 34Md. Golam Moazzam, Dept. of CSE, JU
Jones
Smith
null
loan-number amount
L-170
L-230
L-260
3000
4000
1700
customer-namebranch-name
Downtown
Redwood
Perryridge
![Page 35: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/35.jpg)
Relational Algebra
Outer Join
– Right-outer join
– loan borrower
– right outer join is symmetric with the left outer join: It
pads tuples from the right relation that did not match any
from the left relation with nulls and adds them to the
result of the natural join.
7/4/2017 35Md. Golam Moazzam, Dept. of CSE, JU
loan-number amount
L-170
L-230
L-155
3000
4000
null
customer-name
Jones
Smith
Hayes
branch-name
Downtown
Redwood
null
![Page 36: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/36.jpg)
Relational Algebra
Outer Join
– Full-outer join
– loan borrower
– full outer join does both of those operations, padding
tuples from the left relation that did not match any from
the right relation, as well as tuples from the right relation
that did not match any from the left relation, and adding
them to the result of the join.
7/4/2017 36Md. Golam Moazzam, Dept. of CSE, JU
loan-number amount
L-170
L-230
L-260
L-155
3000
4000
1700
null
customer-name
Jones
Smith
null
Hayes
branch-name
Downtown
Redwood
Perryridge
null
![Page 37: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/37.jpg)
Relational Algebra
Null Values
– It is possible for tuples to have a null value, denoted by
null, for some of their attributes.
– null signifies an unknown value or that a value does not
exist.
– The result of any arithmetic expression involving null is
null.
– Aggregate functions simply ignore null values.
– For duplicate elimination and grouping, null is treated
like any other value, and two nulls are assumed to be the
same
7/4/2017 37Md. Golam Moazzam, Dept. of CSE, JU
![Page 38: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/38.jpg)
Relational Algebra
Modification of the Database
– The content of the database may be modified using the
following operations:
• Deletion
• Insertion
• Updating
– All these operations are expressed using the assignment
operator.
7/4/2017 38Md. Golam Moazzam, Dept. of CSE, JU
![Page 39: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/39.jpg)
Relational Algebra
Deletion
– A delete request is expressed similarly to a query, except
instead of displaying tuples to the user, the selected
tuples are removed from the database.
– We can delete only whole tuples; we cannot delete values
on only particular attributes.
– In relational algebra a deletion is expressed by:
r ← r − E
where r is a relation and E is a relational-algebra query.
7/4/2017 39Md. Golam Moazzam, Dept. of CSE, JU
![Page 40: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/40.jpg)
Relational Algebra
Deletion
Example:
“Delete all of Smith’s account records.”
depositor ← depositor − σcustomer-name =“Smith” (depositor)
“Delete all loans with amount in the range 0 to 50.”
loan ← loan − σamount≥0 and amount≤50 (loan)
“Delete all accounts at branches located in Needham.”
r1 ← σbranch-city =“Needham” (account branch)
r2 ← Πbranch-name, account -number, balance (r1)
account ← account − r2
7/4/2017 40Md. Golam Moazzam, Dept. of CSE, JU
![Page 41: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/41.jpg)
Relational Algebra
Insertion
– To insert data into a relation, we either specify a tuple to be
inserted or write a query whose result is a set of tuples to be
inserted.
– The relational algebra expresses an insertion by:
r ← r ∪ E
where r is a relation and E is a relational-algebra expression.
– Example:
“Insert information in the database specifying that Smith has
$1200 in account A-973 at the Perryridge branch.”
account←account ∪ {(A-973, “Perryridge”, 1200)}
depositor←depositor ∪ {(“Smith”, A-973)}
7/4/2017 41Md. Golam Moazzam, Dept. of CSE, JU
![Page 42: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/42.jpg)
Relational Algebra
Updating
– A mechanism to change a value in a tuple without
changing all values in the tuple.
– Use the generalized-projection operator to do this task:
r ← ΠF1,F2,...,Fn(r)
where each Fi is either the ith attribute of r, if the ith
attribute is not updated, or, if the attribute is to be
updated, Fi is an expression, involving only constants
and the attributes of r, that gives the new value for the
attribute.
7/4/2017 42Md. Golam Moazzam, Dept. of CSE, JU
![Page 43: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/43.jpg)
Relational Algebra
Updating
– Example:
“Make interest payments by increasing all balances by 5
percent.”
account ← Πaccount-number, branch-name, balance *1.05 (account)
“Pay all accounts with balances over $10,000 6 percent
interest and pay all others 5 percent.”
account ← ΠAN,BN, balance *1.06 (σbalance>10000 (account))
∪ ΠAN, BN balance *1.05 (σbalance≤10000 (account))
where the abbreviations AN and BN stand for account-
number and branch-name, respectively.
7/4/2017 43Md. Golam Moazzam, Dept. of CSE, JU
![Page 44: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/44.jpg)
Relational Algebra
Views
– In some cases, it is not desirable for all users to see the
entire logical model (i.e., all the actual relations stored in
the database.)
– Consider a person who needs to know a customer’s loan
number but has no need to see the loan amount. This
person should see a relation described, in the relational
algebra, by
customer-name, loan-number (borrower loan)
Any relation that is not of the conceptual model but is
made visible to a user as a “virtual relation” is called a
view.
7/4/2017 44Md. Golam Moazzam, Dept. of CSE, JU
![Page 45: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/45.jpg)
Relational Algebra
View definition
– A view is defined using the create view statement which
has the form:
create view v as <query expression>
where <query expression> is any legal relational algebra
query expression. The view name is represented by v.
– Once a view is defined, the view name can be used to
refer to the virtual relation that the view generates.
– View definition is not the same as creating a new relation
by evaluating the query expression.
7/4/2017 45Md. Golam Moazzam, Dept. of CSE, JU
![Page 46: Relational Algebra...Relational Algebra Basic Structure – Consider the account table. – It has three column headers: account-number, branch-name, and balance. – For each attribute,](https://reader035.vdocuments.mx/reader035/viewer/2022081409/6086838ec91e72574770c776/html5/thumbnails/46.jpg)
Relational Algebra
View definition
– Example:
Create a view (named all-customer) consisting of
branches and their customers.
7/4/2017 46Md. Golam Moazzam, Dept. of CSE, JU
create view all-customer as
branch-name, customer-name (depositor account)
branch-name, customer-name (borrower loan)