You are on page 1of 26

UNIT – II

RELATIONAL ALGEBRA OPERATIONS

2.1 INTRODUCTION ABOUT RELATIONAL ALGEBRA

Relational algebra is one of the two formal query languages associated with the
relational model. Queries in algebra are composed using a collection of operators.
A fundamental property is hat every operator in the algebra accepts relation
instances as arguments and returns a relation instance as the result.
A relation algebra expression is recursively defined to be a relation a unary
algebra operator applied to a single expression, or a binary algebra operator applied to
two expressions.
The 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.
Queries in relational algebra are composed using a collection of operators, and
each query describes a step-by-step procedure for computing the desired answer. The
fundamental operations of relational algebra are,
 Select
 Project
Selection:
This operation is used to select the rows/tuple from a relation, which satisfy the given
condition. It is denoted by Greek letter sigma (σ). The argument relation is given in
parentheses following the σ.
For example, consider the relation EMPLOYEE,
EID SALARY
E1 2538
E4 7891
E3 1000
E1 1234

Then the result of the expression σ EID=”E1” (EMPLOYEE) is,

EID SALARY
E1 2538
E1 1234
The selection operator σ specifies the tuple to retain
through a selection condition. The selection condition is a Boolean combination of terms
that have the form attribute op constant or attribute1 op attributes2, where op is one of
the comparison operators <, <=, =, >, >=, #.
Projection:
The projection operator p allows us to extract columns from a relation.
Ex:
The expression is psalary(Employee)
And the result of the expression is,

SALARY
2538
7891
1000
1234

SET OPERATIONS
Query language is a language in which a user
requests information from the database. Query language
can be categorized as procedural or nonprocedural. In
procedural language, the user instructs the system to
perform a sequence of operations on the database to
compute the desired result (ex: relational algebra). In
nonprocedural language, the user describes the
information desired without giving a specific procedure
for obtaining that information (ex: relational calculus).
Also called declarative.
SET operations are:
1. Set union
2. Set intersection
3. Set difference
4. Cross product
Union (U):
R U S returns a relation instance containing all tuples that occur in either relation
instance R or relation instance S (or both). Two relation instances are said to be union-
compatible if the following conditions hold,
 They have the same number of the fields.
 Corresponding fields have the same domains.
R U S inherits names from R, if the fields of R have names.
Intersection():
R  S returns a relation instance containing all tuples that occur in both R and S.
The relations R and S must be union compatible and the schema of the result is defined
to be identical to the schema of R.
Set difference:
Set difference is denoted by minus (-), where R - S returns a relation instance
containing all tuples that occur in R but not in S. The relations R and S must be union
compatible and the schema of the result is defined to be identical to the schema of R.
Cross Product:
This operation is denoted by a cross (X). We write the Cross product for relation
R and S as R X S returns a relation instance whose schema consists of all the fields of R
followed by all the fields of S. This operation is sometimes called Cartesian product. The
result of RXS contains one tuple <r,s> for each pair of tuples r € R, s € S.

Example:

Fig: Instance S1 S2

Eid Ename age Salary Eid Ename age salary

21 Mala 21 5000 22 Meera 25 6000

22 Meera 25 6000 25 Vimala 27 5500

24 Ramya 28 7000 26 Rakha 22 7500

R1 24 Ramya 28 7000

Eid DOJ Service


The result of the set operations are:
21 10/5/2005 3
S1  S2
25 7/8/2004 2
Eid Ename Age Salary
22 Meera 25 6000
25 Ramya 28 7000
S1 U S2 S1-S2

Eid Ename age salary Eid Ename Age Salary


21 Mala 21 5000 22 Meera 25 6000
22 Meera 25 6000 25 Ramya 28 7000
24 Ramya 28 7000
25 Vimala 27 5500
26 Rekha 22 7500

S1 X R1

Eid Ename age salary Eid DOJ Service


21 Mala 21 5000 21 10/5/2005 3
21 Mala 21 5000 25 7/8/2004 2
22 Meera 25 6000 21 10/5/2005 3
22 Meera 25 6000 25 7/8/2004 2
24 Ramya 28 7000 21 10/05/2005 3
24 Ramya 28 7000 25 7/8/2004 2
Renaming
operator:
The use can break a large algebra expression into smaller pieces by giving names
to the results of sub expressions.
The rename operator, denoted by the lower-case Greek letter rho (r). The
expression is P(R(F), E) takes an arbitrary relational algebra expression E and returns an
instance of a new relation called R. ‘R’ contains the same tuples as E, and same scheme
as E, but some fields are renamed. For example, consider the relation
r (C( 1  Eid1, 5  Eid2), S1XR1 ))
ADDITIONAL OPERATORS
Joins:
The join operation is one of the most useful operations in relational algebra and
the most commonly used way to combine information from two or more relations.
The join operation is used to combine information from two or more relations.
Although a join can be defined as a cross-product followed by selection and
projections.
There are three types
 Condition joins
 Equijoin
 Natural join
Condition joins:
The operation is defined as, R S = σ c(R X S). Thus is defined to be a cross
product followed by a selection. After performing the condition it will do the join
operation. It is a collection of selection operation & Cartesian product.
Ex:
The result of S1 S1.eid<R1.eid R1

Eid Ename age salary Eid DOJ Service


21 Mala 21 5000 25 7/8/2004 2
22 Meera 25 6000 25 7/8/2004 2

Because eid appears in both S1 and R1, the corresponding fields in the result of the cross
product S1XR1.

Equijoin:
When the join operation contains only equalities then the join operation is called
equijoin. S1.eid=R1.eid, that is eualities between two fields in R and S. The join
operation with this refinement is called equijoin.
The result of S1 (s1.eid = r1.eid) R1

Eid Ename age salary Eid DOJ Service


21 Mala 21 5000 21 10/5/2005 3
Natural join:
Natural join is an equijoin in which equalities are specified on all fields having
the same name in R and S. Here we can omit the condition; generally join condition is a
collection of equalities on all common fields.
Division:
The division operator is useful for expressing certain kinds of queries. Consider
two relation A and B in which A has exactly two fields x and y and B has just one field
y, with the same domain as in A. we define the division operation A/B as the set of all x
values such that for every y value in B, there is tuple <x,y> in A.
For example, consider the relations A ,B1, B2 and B3.

PNo
B1
P2

SNo PNo
S1 P1
S1 P2 PNo
S1 B2 P3 P2
S1 P4 P4
S2 P1
S2 P2 A

S3 B3 P2 PNo

S4 P2 P1

S4 P4 P2
SNo A/B1 SNO SNo A/B2 A/B3
P4
S1 S1 S1
S2 S4
S3
S4
The division operations id full generality , to consider the case when both x and y are
replaced by a set of attributes.
More examples about relational algebra:
Find the names of sailors who have reserved boat 103.
Relational algebra query is, name ((σ bid=103 Reserves) Sailors)
Find the sids of sailors with age over 20 who have not reserved a red boat.
sid(σage > 20 sailors) - sid((σcolor = ‘red’ boats) reserves sailors)
Find the name of sailors who have reserved a red boat.
name ((σcolor=’red’ Boats) Reserves Sailors)

RELATIONAL LANGUAGES

2.2THE TUPLE RELATIONAL CALCULAS

Introduction:
Relational calculus is an alternative to relational algebra. Relational calculus has
had a big influence on the design of commercial query languages such as SQL and
especially query by example (QBE).
The relational calculus is nonprocedural, or declarative, i.e) the user has to
specify what data are needed without specifying how to get those data. Two types of
calculus are,
 Tuple Relational Calculus
 Domain Relational Calculus
Tuple Relational Calculus(TRC):
A tuple variable is a variable that takes on tuples of a particular relation schema
as values. The TRC query has the form {T | P(T)}, where T is a tuple variable and P(T)
denotes a formula that describes T. The result of this query is the set of all tuples t for
which the formula P(T) evaluates to true with T=t.
Example,
Find the sname, sno and mark1 for student relation and mark1 greater than 75.
{S|S  student^s.mark1>75}
Syntax of TRC queries:
Let Rel be a relation name, R and S be tuple Variable, a an attribute of R, and b
an attribute of S. Let op denote a relational operator. An atomic formula in TRC is one
of the following,
 R Rel
 R.a op S.b
 R.a op constants, or constant op R.a
A formula is recursively defined to be one of the following, where p and q are
themselves formulas,
 any atomic formula
 p, p q, p q, or p => q
 R(p(R)), where R is a tuple variable
 R(p(R)), where R is a tuple variable
In the last two clauses above, the quantifiers and are said to bind the variable R. a
variable are said to be free in a formula or sub formula if the formula does not contain
an occurrence of a qualifier that binds it.
A TRC query is defined to be expression of the form {T | P(T) } , where T is the
only free variable in the formula P.
Semantics of TRC queries:
A query is evaluated on a given instance of the DB. Let each free variable in a
formula F be bound to a tuple value. F evaluates true if one of the following holds:
o F is an atomic formula R Rel, and R is assigned a tuple in the instance of relation
Rel.
o F is a comparison R.a op S.b, R.a op constant op R.a, and the tuples assigned to R
and S have field values R.a and S.b that make the comparison true.
o F is of the form p, and p is not true; or of the form p q, and both p and q are true;
or of the form p q and one of the them is true, or of the form p q is true whenever p
is true.
o F is of the form R(p(R)), and there is some assignment of tuples to the free
variables in p(R), including the variable R , that makes the formula p(R) true.
o F is of the form R(p(R), and there is some assignment of tuples to the free
variables in p(R) that makes the formula p(R) true .

2.3 THE DOMAIN RELATIONAL CALCULUS

Domain relational calculus: (DRC):


A domain variable is a variable that ranges over the values in the domain of
some attributes. A DRC query has the form of {<x1,x2,....xn> | p(<x1,x2,.....xn>)}, where
each xi is either is a domain variable or constant, and p(<x1,x2,.....xn>) denotes a DRC
formula. The result of this query is the set of all tuple(x1,x2,x3…….xn). Let op denote a
relational operator. An atomic formula in DRC is one of the following,
o <x1,x2,…,xn> Rel, where Rel is a relation with n attributes, each x 1,x2,….,xn is either
a variable or constant.
o X op Y.
o X op constant, or constant op X.
o A formula is recursively defined to be one of the following, where p and q are
themselves formulas and p(x) denotes a formula in which the variable x appears.
o Any atomic formula.
o p, p q, p q, or p=>q.
o X(p(X)), where X is a domain variable.
o X(p(X)), where X is a domain variable
SQL
 SQL uses a combination of relational-algebra and relational-calculus.
 We refer to the SQL language as a “query language”.

2.4 SQL BACKGROUND

The SQL language has several parts:


 Data-definition language (DDL). The SQL DDL provides commands for
defining relation schemas, deleting relations, and modifying relation schemas.
 Interactive data-manipulation language (DML). The SQL DML includes a query
language based on both the relational algebra and the tuple relational calculus. It
includes also commands to insert tuples into, delete tuples from, and modify
tuples in the database.
 View definition. The SQL DDL includes commands for defining views.
 Transaction control. SQL includes commands for specifying the beginning and
ending of transactions.
 Embedded SQL and dynamic SQL. Embedded and dynamic SQL define how
SQL statements can be embedded within general-purpose programming
languages, such as C, C++, Java, PL/I, Cobol, Pascal, and Fortran.
 Integrity. The SQL DDL includes commands for specifying integrity constraints
that the data stored in the database must satisfy. Updates that violate integrity
constraints are disallowed.
 Authorization. The SQL DDL includes commands for specifying access rights to
relations and views.
Data-definition Languages (DDL)
 A Database schema is specified by a set of definitions expressed by a special
language called a data-definition language (DDL)
 The result of compilation of DDL Statements is a set of tables that is stored in a
special file is called Data Dictionary (DD) or Data Directory
 A Data Dictionary is a file that contains metadata that is data about data.
 The storage structure and access methods used by the database system are
specified by a set of definitions in a special type of DDL called a Data storage
and Definition Language.
 Database systems concentrate on integrity constraints that casn be tested with
minimal overhead:
2.5 BASIC STRUCTURE OF SQL
The basic structure of an SQL expression consists of three clauses:
1. select,
2. from,
3. where.
The select clause corresponds to the projection operation of the relational algebra. It
is used to list the attributes desired in the result of a query.
 The from clause corresponds to the Cartesian-product operation of the relational
algebra. It lists the relations to be scanned in the evaluation of the expression.
 The where clause corresponds to the selection predicate of the relational algebra.
It consists of a predicate involving attributes of the relations that appear in the
from clause.
A typical SQL query has the form
select A1, A2, . . . , An
from r1, r2, . . . , rm
where P
Here An – attribute, rm – relation, P- predicate
Select Clause:
 The select clause is used to select the attributes from the relation.
 Duplicate tuples never appear in relations.
Where clause:
 Where clause is used with the select clause.
 It is the condition used to check part of the query.
 Here and, or, not is used instead of ^, V, -
Example:
Select loan-number from loan where branch-name=”perryridge”
and amount > 1200

select loan-number from loanwhere amount between 9000 and 10000

The From Clause:


The from clause by itself defines a Cartesian product of the relations in the
clause.
Eg:
select customer-name, borrower.loan-number, amount
from borrower, loan
Where borrower.loan-number = loan. loan-number

The Rename Operation:


It is used to rename both relations and attributes. It uses the as clause, taking the
form:
old-name as new-name

The as clause can appear in both the select and from clauses. For example, if we want
the attribute name loan-number to be replaced with the
name loan-id, we can rewrite the preceding query as

select customer-name, borrower.loan-number as loan-id, amountfrom borrower, loan


where borrower.loan-number = loan.loan-number

Tuple Variables:
The as clause is particularly useful in defining the notion of tuple variables, as
done in the tuple relational calculus. Tuple variables are defined in the from clause by
way of the as clause.
We can write the query “For all customers who have a loan from the bank, find
their names, loan numbers, and loan amount” as
select customer-name, T.loan-number, S.amount
from borrower as T, loan as S
where T.loan-number = S.loan-number

String Operations:
SQL specifies strings by enclosing them in single quotes, for example,
’Perryridge’,
The most commonly used operation on strings is pattern matching using the
operator like.
We describe patterns by using two special characters:
• Percent (%): The % character matches any substring.
• Underscore (_): This character matches any character.
To illustrate pattern matching, we consider the following examples:
• ’Perry%’ matches any string beginning with “Perry”.
• ’%idge%’ matches any string containing “idge” as a substring, for example,
’Perryridge’, ’Rock Ridge’, ’Mianus Bridge’, and ’Ridgeway’.
• ’_ _ _’ matches any string of exactly three characters.
• ’_ _ _%’ matches any string of at least three characters.
Eg:
“Find the names of all customers whose street address includes the substring
‘Main’”. This query can be written as
Select customer-name
from customer
where customer-street like ’%Main%’

Ordering the Display of Tuples:


The order by clause causes the tuples in the result of a query to appear in sorted
order. By default, the order by clause lists items in ascending order. To specify the sort
order, we may specify desc for descending order or asc for ascending order.
Eg:
Select * from loan order by amount desc, loan-number asc Duplicates

We can define the duplicate semantics of an SQL query using multiset


versions of the relational operators. For example, suppose that relations r 1with schema
(A, B) and r2 with schema (C) are the following multisets:
r1= {(1, a), (2, a)} r2= {(2), (3), (3)}
Then ΠB(r1) would be {(a), (a)},
Whereas ΠB(r1) × r2 would be {(a, 2), (a, 2), (a, 3), (a, 3), (a, 3), (a, 3)}
2.6 SET OPERATIONS

The SQL operations union, intersect, and except operate on relations and
correspond to the relational-algebra operations U, ∩, and −.
The Union Operation:
To find all customers having a loan, an account, or both at the bank, we write ..
(select customer-name from depositor)
union (select customer-name from borrower)

The union operation automatically eliminates duplicates,


If we want to retain all duplicates, we must write union all in place of union:
(select customer-name from depositor)
union all (select customer-name from borrower)

2. The Intersect Operation


To find all customers who have both a loan and an account at the bank, we write
(select distinct customer-name from depositor)
intersect (select distinct customer-name from borrower)

The intersect operation automatically eliminates duplicates.

If we want to retain all duplicates, we must write intersect all in place of intersect:
(select customer-name from depositor)intersect all (select customer-name from borrower)

3. The Except Operation:


To find all customers who have an account but no loan at the bank, we write
select distinct customer-name from depositor)
except (select customer-name from borrower)

If we want to retain all duplicates, we must write except all in place of except:
(select customer-name from depositor)
except all (select customer-name from borrower)

2.7 AGGREGATE OPERATORS

Aggregate operations takes collection of values as input and return a single value
as a result. The SQL supports five aggregate operations. They are (Let A is a column
name),
 COUNT: The number of values in the A column.
 SUM: The sum of all values in the A column.
 AVG: The average of all values in the A column.
 MAX: The maximum value in the A column.
 MIN: The minimum value in the A column.

SID NAME AGE


22 Siva 20
24 Madhu 21
23 Keerthana 5
26 Tamil 30
25 Madhu 21

Find the average of all students.SELECT AVG(S.age) from Student S


The average age is 19.4
Find the average age of students with age above 20.

SELECT ANY (S.age) from student S WHERE S.age>20

Count the number of students.


SELECT COUNT (*) FROM STUDENT S

This command is used to count the number of rows.


Answer is 5.Count the number of different student name.
SELECT COUNT (DISTINCT S.name) from student S

Answer is 4, because the name madhu is counted twice.


Find the names of student who are older than the oldest student with age>20.

SELECT S.NAME FROM STUDENT S


WHERE S.AGE > (SELECT MAX (S2.AGE)
FROM STUDENT S2
WHERE S2.GE> 20)

The answer is Madhu, Tamil.


GROUP BY and HAVING clause:
To apply aggregate operations to each of a number of groups of rows in a
relation, where the number of groups depends in the relation instance.

The aggregate functions also applied to group of set


of tuples; we specify this is by GROUP BY clause. Tuples
with same value on all attributes in the group by clause
are placed in one group. HAVING clause contains
predicates that applied after the formulation of groups.
SELECT [DISTINCT] selection-list FROM from-list WHERE qualification
GROUP BY grouping-list HAVING group-qualification.

Some important points:


o Select-list in the SELECT clause consists of a list of column names.
o Grouping-list – This is a collection of rows that agree on the values of columns
in grouping list.
o HAVING clause determines whether an answer row is to generated for a given
group
o If GROUP BY is omitted the entire table is regarded as a single group.
Example:

SID SNAME RATING AGE


22 Siva 7 45
29 Madhu 1 33
31 Naveen 8 55
32 Keerthana 8 25
58 Venkatesh 10 35
64 Sankar 7 35
71 Kalai 10 16
74 Jothi 9 35
85 Tamil 3 25
95 Sahana 3 63

Findthe age of the youngest student who is eligibleto vote(at least 18 years old) for
each rating level with atlest two such students.
SELECT S.rating, MIN(S.age) AS minage
FROM Student S WHERE S.age >= 18
GROUP BY S.rating
HAVING COUNT (*) > 1.

1) The first step is constructing the cross product of tables in the from_list.
2) The second step is to apply the qualification in the
3) The third step is to eliminate unwanted

SID SNAME RATING AGE RATING AGE


22 Siva 7 45 7 45
29 Madhu 1 33 1 33
31 Naveen 8 55 8 55
32 Keerthana 8 25 8 25
58 Venkatesh 10 35 10 35
64 Sankar 7 35 7 35
74 Jothi 9 35 9 35
85 Tamil 3 25 3 25
95 Sahana 3 63 3 63

WHERE clause s.age>=18 columns

RATING AGE
RATING AGE COUNT(*)>1.
3 25
1 33
7 35
3 63
8 25
3 25
7 45
7 35 If the query contains DISTINCT in the SELECT
8 25
8 55
9 35
10 35
clause,duplicated are eliminated in an additional and final step.

2.8 NULL VALUES

SQL provides a special column value called null to use in such situation. Use
null when the column value is either unknown or inapplicable
Use my student table enter the row < 10, raji, null, 25

SID SNAME RATING AGE


22 Siva 7 45
29 Madhu 1 33
31 Naveen 8 55
32 Keerthana 8 25
58 Venkates 10 35
h
64 Sankar 7 35
10 raji null 25
1. Comparison using Null values:
The above table for the comparison rating>8. This comparison should evaluate to
the value unknown. If we compare two null values using <,>,= and so on. The result is
always unknown. SQL provides a special comparison operator IS NULL to test whether
a column value is Null. Suppose we say rating IS NULL, which would evaluate to true,
say rating IS NOT NULL, which would evaluate to false.
2. Impact on SQL constructs:
WHERE clause eliminates rows for which qualification does not evaluate to true.
In the presence of null values any row that evaluates to false or unknown is eliminated.
3. Outer join:
The outer join is an extension of the join operation to deal with missing
information. It contains three types, they are, Left outer join, Right outer join, Full outer
join.

Consider the table’s loan and customer,

Branch loan_id Amount Customer_name Loan_id


Madurai 170 3000 Brintha 170
Salem 230 4000 Kishore 230
Tirunelveli 260 1700 Deepa 155

Loan customer
Left outer join:
It takes all tuples in the left relation that did not match with any tuples in right
relation, pads the tuples with the null values for all other attributes from the right
relation, and adds them to the result of natural join.
Example:
SELECT * FROM loan NATURAL LEFT OUTER JOIN customer

The natural keyword specifies that the join condition is equality on all common
attribute.

Branch loan_id Amount Customer_name Loan_id


Madurai 170 3000 Brintha 170
Salem 230 4000 Kishore 230
Tirunelveli 260 1700 Null Null
Right outer join:
It takes all tuples in the right relation that did not match with any tuples in left
relation, pads the tuples with the null values and adds to the result of natural join. All
information from right relation is present in the result right outer join.
Example:

SELECT * FROM loan NATURAL RIGHT OUTER JOIN customer

Branch loan_id Amount Customer_name


Madurai 170 3000 Brintha
Salem 230 4000 Kishore
Tirunelveli 155 Null Deepa

Full outer-join:
It is a combination of both the above join operations, padding tuples from left
relation that did not match any form the right relation, as well as tuples from right
relation that did not match any from left relation, and padding them to the result of the
join.
Example:
SELECT * FROM loan NATURAL FULL OUTER JOIN customer

Branch loan_id Amount Customer_name


Madurai 170 3000 Brintha
Salem 230 4000 Kishore
Tirunelveli 260 1700 Null
Null 155 Null Deepa
4. Disallowing Null values:
We can disallow null values by specifying NOT NULL as part of the fields’
definition.
Ex, sname char (20) NOT NULL. The primary key is not allowed to take on null values.
NESTED QUERIES
A nested query is a query that has another query embedded within it; the
embedded query is called a sub query. Sub query can sometimes appear in the FROM
clause or the HAVING clause.

Ex: 1
SELECT S.sname
FROM sailors S
WHERE S.sid IN(SELECT R.sid FROM Reserves R WHERE R.bid=103)
The nested subqury computes the set of sids for sailors who have reserved boat

Ex: 2
Find the names of sailors who have reserved a red boat.
SELECT S.sname
FROM sailors S
WHERE S.sid IN ( SELECT R.sid
FROM Reserves R
WHERE R.bid IN (SELECT B.bid
FROM Boats B
WHERE B.color=’red’)

The innermost sub query finds the set of bids of red boats.
VIEWS:
Any relation that is not part of the logical model, but is made visible to a user as
a virtual relation, is called a view. It is possible to support a large number of views on
top of any given set of actual relations.
View Definition:
We define a view by using the create view statement. To define a view, we must
give the view a name, and must state the query that computes the view. The form of the
create view statement is
create view v as <query expression>

where <query expression> is any legal relational-algebra query expression.


v is name of the view.

Eg:
Consider the view consisting of branches and their customers this view is called
as customer. This can be written asΠ branch-name, customer-name (depositor |X|
account)U Π branch-name, customer-name (borrower1loan).
Once we have defined a view, we can use the view name to refer to the virtual
relation that the view generates.
Updates through Views and Null Values:
If we want to see all loan data in the loan relation except loan amount. Let we
write branch loan as the view name, then we define this view as
create view loan-branch as Π loan-number, branch-name(loan)

Since we allow a view name to appear wherever a relation name is allowed, we can
write:
loan-branch←loan-branch U {(L-37, “Perryridge”)}

This insertion must be represented by an insertion into the relation loan, since
loan is the actual relation from which the database system constructs the view loan-
branch.
However, to insert a tuple into loan, we must have some value for amount.
There are two reasonable approaches to dealing with this insertion:
• Reject the insertion, and return an error message to the user.
• Insert a tuple (L-37, “Perryridge”, null) into the loan relation.
MODIFICATION OF DATA BASE:
Modification of data base is used to add, remove, or change information in the
database.
Deletion:
The deletion operation is used to remove the selected tuples 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.
Here are several examples of relational-algebra delete requests:

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”(account1branch)
r2←Πbranch-name, account-number, balance(r1)
account ← account − r2
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←rUE
Where r is a relation and E is a relational-algebra expression.
Suppose that we wish to insert the fact that Smith has $1200 in account A-973 at the
Perry ridge branch. We write,
account ← account U {(A-973, “Perry ridge”, 1200)}
depositor ← depositor U {(“Smith”, A-973)}
Updating:
In certain situations, we may wish to change a value in a tuple without changing
all values in the tuple. We can use the generalized-projection operator to do this task:
r← ΠF1, F2, ,,Fn(r)
Where each Fi is either the i th attribute of r, to illustrate the use of the update
operation, suppose that interest payments are being made, and that all balances are to
be increased by 5 percent. We write
account ← Πaccount-number, branch-name, balance ∗1.05(account)

Now suppose that accounts with balances over $10,000 receive 6 percent interest,
Where as all others receive 5 percent. We write
account ← Π AN,BN, balance ∗1.06(σbalance>10000(account))

U ΠAN, BN balance∗1.05(σbalance≤10000(account))

Where the abbreviations AN and BN stand for account-number and branch-name,


respectively.

You might also like