Sunteți pe pagina 1din 98

Chapter 3

Relational Model and


Algebra

Introduction
A relational database consists of tables each of which
is assigned a unique name.
Row in a table represents a relationship among a set of
values.
In relational model the term relation is used to refer to
a table.
The term tuple is used to refer to a row.
The term attribute refers to a column.
Relation instance refers to a specific instance of a
relation.
The order in which the tuples appear in the relation is
irrelevant, since a relation is set of tuples.

Introduction
For each attribute of a relation there is set of permitted
values called the domain of that attribute.
E.g. domain of salary attribute is set of all possible
salary values.
For all possible relations r, the domain of all attribute be
atomic.
A domain is atomic if elements of the domain are
considered to be indivisible unit.
E.g. the value of an attribute can be an account number,
but cannot be a set of account numbers

The important issue is not what the domain is but rather


how we use the domain element in our database.

Example of a Relation

Schema
Schema: the logical design of a
database.
Instance: snapshot of the data in the
database at a given instant in time.

Relation Schema

Formally, given domains D1, D2, . Dn a

relation r is a subset of
D 1 x D2 x x Dn
Thus, a relation is a set of n-tuples (a1,
a2, , an) where each ai Di
Schema of a relation consists of
attribute definitions
name
type/domain

integrity constraints

Relation Instance

The current values (relation instance) of a relation are


specified by a table
An element t of r is a tuple, represented by a row in a
table
Order of tuples is irrelevant (tuples may be stored in an
arbitrary order)
attributes
(or columns)
customer_namecustomer_street customer_city
Jones
Smith
Curry
Lindsay

Main
North
North
Park
customer

Harrison
Rye
Rye
Pittsfield

tuples
(or rows)

Database

A database consists of multiple relations


Information about an enterprise is broken up
into parts, with each relation storing one part
of the information
E.g.
account : information about accounts
depositor : which customer owns which
account
customer : information about customers

The customer Relation

The depositor Relation

Why Split Information Across


Relations?
Storing all information as a single relation such as
bank(account_number, balance,
customer_name, ..)
results in
repetition of information
e.g.,if two customers own an account (What gets repeated?)

the need for null values


e.g., to represent a customer without an account

Normalization theory (Chapter 7) deals with how


to design relational schemas

Keys

Let K R
K is a superkey of R if values for K are sufficient to
identify a unique tuple of each possible relation r(R)
by possible r we mean a relation r that could exist in
the enterprise we are modeling.
Example: {customer_name, customer_street} and
{customer_name}
are both superkeys of Customer, if no two customers can
possibly have the same name
In real life, an attribute such as customer_id would be used
instead of customer_name to uniquely identify customers, but
we omit it to keep our examples small, and instead assume
customer names are unique.

Keys (Cont.)
K is a candidate key if K is minimal
Example: {customer_name} is a candidate key
for Customer, since it is a superkey and no
subset of it is a superkey.
Primary key: a candidate key chosen as the
principal means of identifying tuples within a
relation
Should choose an attribute whose value never, or very
rarely, changes.
E.g. email address is unique, but may change

Foreign Keys

A relation schema may have an attribute that


corresponds to the primary key of another relation.
The attribute is called a foreign key.
E.g. customer_name and account_number attributes of
depositor are foreign keys to customer and account
respectively.
Only values occurring in the primary key attribute of the
referenced relation may occur in the foreign key
attribute of the referencing relation.

Schema Diagram

ER-to-Relational Mapping Algorithm

Step 1: Mapping of Regular Entity Types.


For each regular (strong) entity type E in the ER schema,
create a
relation R that includes all the simple
attributes of E.
Choose one of the key attributes of E as the primary key
for R. If the chosen key of E is composite, the set of
simple attributes that form it will together form the
primary key of R.
Example: We create the relations EMPLOYEE,
DEPARTMENT, and PROJECT in the relational schema
corresponding to the regular entities in the ER diagram.
SSN, DNUMBER, and PNUMBER are the primary keys for
the relations EMPLOYEE, DEPARTMENT, and PROJECT as
shown.

FIGURE
7.1
The ER
conceptual
schema
diagram for
the
COMPANY
database.

FIGURE 7.2
Result of
mapping the
COMPANY ER
schema into
a relational
schema.

ER-to-Relational Mapping Algorithm (cont)

Step 2: Mapping of Weak Entity Types


For each weak entity type W in the ER schema with owner entity type E,
create a relation R and include all simple attributes (or simple
components of composite attributes) of W as attributes of R.
In addition, include as foreign key attributes of R the primary key
attribute(s) of the relation(s) that correspond to the owner entity
type(s).
The primary key of R is the combination of the primary key(s) of the
owner(s) and the partial key of the weak entity type W, if any.
Example: Create the relation DEPENDENT in this step to correspond to
the weak entity type DEPENDENT. Include the primary key SSN of the
EMPLOYEE relation as a foreign key attribute of DEPENDENT (renamed
to ESSN).
The primary key of the DEPENDENT relation is the combination {ESSN,
DEPENDENT_NAME} because DEPENDENT_NAME is the partial key of
DEPENDENT.

ER-to-Relational Mapping Algorithm (cont)

Step 3: Mapping of Binary 1:1 Relation Types


For each binary 1:1 relationship type R in the ER schema,
identify the relations S and T that correspond to the entity
types participating in R. There are three possible approaches:
(1) Foreign Key approach: Choose one of the relations-S, say-and include a
foreign key in S the primary key of T. It is better to choose an entity type with
total participation in R in the role of S.
Example: 1:1 relation MANAGES is mapped by choosing the participating
entity type DEPARTMENT to serve in the role of S, because its participation in
the MANAGES relationship type is total.
(2) Merged relation option: An alternate mapping of a 1:1 relationship type is
possible by merging the two entity types and the relationship into a single
relation. This may be appropriate when both participations are total.
(3) Cross-reference or relationship relation option: The third alternative is to set
up a third relation R for the purpose of cross-referencing the primary keys of
the two relations S and T representing the entity types.

ER-to-Relational Mapping Algorithm (cont)

Step 4: Mapping of Binary 1:N Relationship Types.


For each regular binary 1:N relationship type R, identify
the relation S that represent the participating entity type
at the N-side of the relationship type.
Include as foreign key in S the primary key of the
relation T that represents the other entity type
participating in R.
Include any simple attributes of the 1:N relation type as
attributes of S.
Example: 1:N relationship types WORKS_FOR,
CONTROLS, and SUPERVISION in the figure. For
WORKS_FOR we include the primary key DNUMBER of
the DEPARTMENT relation as foreign key in the
EMPLOYEE relation and call it DNO.

ER-to-Relational Mapping Algorithm (cont)

Step 5: Mapping of Binary M:N Relationship Types.


For each regular binary M:N relationship type R, create a
new relation S to represent R.
Include as foreign key attributes in S the primary keys of
the relations that represent the participating entity types;
their combination will form the primary key of S.
Also include any simple attributes of the M:N relationship
type (or simple components of composite attributes) as
attributes of S.
Example: The M:N relationship type WORKS_ON from the
ER diagram is mapped by creating a relation WORKS_ON in
the relational database schema. The primary keys of the
PROJECT and EMPLOYEE relations are included as foreign
keys in WORKS_ON and renamed PNO and ESSN,
respectively.
Attribute HOURS in WORKS_ON represents the HOURS

ER-to-Relational Mapping Algorithm (cont)

Step 6: Mapping of Multivalued attributes.


For each multivalued attribute A, create a new relation R.
This relation R will include an attribute corresponding to A,
plus the primary key attribute K-as a foreign key in R-of the
relation that represents the entity type of relationship type
that has A as an attribute.
The primary key of R is the combination of A and K. If the
multivalued attribute is composite, we include its simple
components.
Example: The relation DEPT_LOCATIONS is created. The
attribute DLOCATION represents the multivalued attribute
LOCATIONS of DEPARTMENT, while DNUMBER-as foreign
key-represents the primary key of the DEPARTMENT
relation. The primary key of R is the combination of

ER-to-Relational Mapping Algorithm (cont)

Step 7: Mapping of N-ary Relationship Types.


For each n-ary relationship type R, where n>2,
create a new relationship S to represent R.
Include as foreign key attributes in S the
primary keys of the relations that represent the
participating entity types.
Also include any simple attributes of the n-ary
relationship type (or simple components of
composite attributes) as attributes of S.
Example: The relationship type SUPPY in the ER below.
This can be mapped to the relation SUPPLY shown in the
relational schema, whose primary key is the combination
of the three foreign keys {SNAME, PARTNO, PROJNAME}

FIGURE 4.11
Ternary relationship types. (a) The SUPPLY
relationship.

FIGURE 7.3
Mapping the n-ary relationship type SUPPLY
from Figure 4.11a.

Summary of Mapping constructs and constraints

Table 7.1 Correspondence between ER and


Relational Models
ER Model
Relational Model
Entity type
Entity relation
1:1 or 1:N relationship type Foreign key (or relationship
relation)
M:N relationship type Relationship relation and two
foreign keys
n-ary relationship type Relationship relation and n foreign
keys
Simple attribute
Attribute
Composite attribute
Set of simple component attributes
Multivalued attribute
Relation and foreign key
Value set
Domain
Key attribute
Primary (or secondary) key

Mapping EER Model Constructs


to Relations

Step8: Options for Mapping Specialization or Generalization.


Convert each specialization with m subclasses {S1, S2,.,Sm}
and generalized superclass C, where the attributes of C are
{k,a1,an} and k is the (primary) key, into relational schemas
using one of the four following options:
Option 8A: Multiple relations-Superclass and subclasses.
Create a relation L for C with attributes Attrs(L) = {k,a 1,an} and
PK(L) = k. Create a relation Li for each subclass Si, 1 < i < m, with
the attributesAttrs(Li) = {k} U {attributes of Si} and PK(Li)=k. This
option works for any specialization (total or partial, disjoint of
over-lapping).
Option 8B: Multiple relations-Subclass relations only
Create a relation Li for each subclass Si, 1 < i < m, with the
attributes Attr(Li) = {attributes of Si} U {k,a1,an} and PK(Li) = k.
This option only works for a specialization whose subclasses are
total (every entity in the superclass must belong to (at least) one of
the subclasses).

FIGURE 4.4
EER diagram
notation for
an attributedefined
specializatio
n on
JobType.

FIGURE 7.4
Options for mapping specialization or generalization.
(a) Mapping the EER schema in Figure 4.4 using option 8A.

FIGURE 4.3
Generalization. (b) Generalizing CAR and TRUCK into
the superclass VEHICLE.

FIGURE 7.4
Options for mapping specialization or generalization.
(b) Mapping the EER schema in Figure 4.3b using option 8B.

Mapping EER Model Constructs to Relations


(cont)

Option 8C: Single relation with one type attribute.


Create a single relation L with attributes Attrs(L) = {k,a1,
an} U {attributes of S1} UU {attributes of Sm} U {t} and
PK(L) = k. The attribute t is called a type (or
discriminating) attribute that indicates the subclass to
which each tuple belongs
Option 8D: Single relation with multiple type
attributes.
Create a single relation schema L with attributes Attrs(L) =
{k,a1,an} U {attributes of S1} UU {attributes of Sm} U {t1,
t2,,tm} and PK(L) = k. Each ti, 1 < I < m, is a Boolean type
attribute indicating whether a tuple belongs to the subclass

FIGURE 4.4
EER diagram
notation for
an attributedefined
specializatio
n on
JobType.

FIGURE 7.4
Options for mapping specialization or generalization.
(c) Mapping the EER schema in Figure 4.4 using option 8C.

FIGURE 4.5
EER diagram notation for an overlapping
(nondisjoint) specialization.

FIGURE 7.4
Options for mapping specialization or generalization.
(d) Mapping Figure 4.5 using option 8D with Boolean type fields Mflag and
Pflag.

Mapping EER Model Constructs to Relations


(cont)
Mapping of Shared Subclasses (Multiple Inheritance)
A shared subclass, such as STUDENT_ASSISTANT, is a subclass of
several classes, indicating multiple inheritance. These classes
must all have the same key attribute; otherwise, the shared
subclass would be modeled as a category.
We can apply any of the options discussed in Step 8 to a shared
subclass, subject to the restriction discussed in Step 8 of the
mapping algorithm. Below both 8C and 8D are used for the shared
class STUDENT_ASSISTANT.

FIGURE 4.7
A specialization
lattice with
multiple
inheritance for a
UNIVERSITY
database.

FIGURE 7.5
Mapping the EER specialization lattice in Figure
4.6 using multiple options.

Mapping EER Model Constructs to Relations


(cont)

Step 9: Mapping of Union Types (Categories).


For mapping a category whose defining superclass have
different keys, it is customary to specify a new key
attribute, called a surrogate key, when creating a
relation to correspond to the category.
In the example below we can create a relation OWNER to
correspond to the OWNER category and include any
attributes of the category in this relation. The primary key
of the OWNER relation is the surrogate key, which we
called OwnerId.

FIGURE 4.8
Two categories (union
types): OWNER and
REGISTERED_VEHICLE.

FIGURE 7.6
Mapping the EER
categories
(union types) in
Figure 4.7 to
relations.

Mapping Exercise

Query Languages

Language in which user requests


information from the database.
Categories of languages

Procedural
Non-procedural, or declarative
In a procedural language the user instructs
the system to perform a sequence of
operations on the database to compute the
desired result.
In nonprocedural the user describes the
desired information without giving a specific
procedure.

Relational
Algebra

Procedural language
Six basic operators

select:
project:
union:
set difference:
Cartesian product: x
rename:

The operators take one or two relations as inputs and


produce a new relation as a result.

Fundamentals of Relational Algebra


Operation
1. The Select Operation:

Selects the tuple that satisfies a given predicate.


Use the lower case Greek letter sigma() to denote the
selection.
The predicate appears as a subscript to .
The argument relation is in parentheses after the .
To select those tuples of the loan relation where the
branch is Bandra
branch_name=Bandra (loan)

Select Operation

Notation: p(r)
p is called the selection predicate
Defined as:

p(r) = {t | t r and p(t)}


Where p is a formula in propositional calculus consisting of
terms connected by : (and), (or), (not)
Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, , >, . <.

Example of selection:

branch_name=Perryridge

(account)

Select Operation Example


Relation r

A=B ^ D > 5 (r)

12

23 10

23 10

Project
Operation

Notation:

A1 , A2 ,, Ak

(r )

where A1, A2 are attribute names and r is a relation name.

The result is defined as the relation of k columns obtained by


erasing the columns that are not listed
Duplicate rows removed from result, since relations are sets
Example: To eliminate the branch_name attribute of account
account_number, balance (account)

Project Operation Example

Relation r:

A,C (r)

10

20

30

40

Union
Operation

Notation: r s
Defined as:
r s = {t | t r or t s}
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd
column
of r deals with the same type of values as does the 2nd
column of s)
Example: to find all customers with either an account or a loan
customer_name (depositor) customer_name (borrower)

Union Operation Example

Relations r, s:

r s:

Set
Difference
Operation

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 arity
attribute domains of r and s must be compatible

Set Difference Operation Example

Relations r, s:

r
r s:

Cartesian-Product Operation
Example
Relations r, s:

10
10
20
10

a
a
b
b

r x s:
A

1
1
1
1
2
2
2
2

10
10
20
10
10
10
20
10

a
a
b
b
a
a
b
b

Cartesian-Product
Operation

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.

Composition of Operations

Can build expressions using multiple operations


Example: A=C(r x s)
rxs

A=C(r x s)

1
1
1
1
2
2
2
2

10
10
20
10
10
10
20
10

a
a
b
b
a
a
b
b

1
2
2

10 a
10 a
20 b

Rename
Operation

Allows us to name, and therefore to refer to, the results of


relational-algebra expressions.
Allows us to refer to a relation by more than one name.
Example:

x (E)

returns the expression E under the name X


If a relational-algebra expression E has arity n, then

x ( A ,A
1

2 ,..., An

(E )

returns the result of expression E under the name X, and with


the
attributes renamed to A1 , A2 , ., An .

Banking Example
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street,
customer_city)
account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)

Example Queries
Find all loans of over $1200

amount > 1200


(loan)
Find the loan number for each loan of an amount

greater than

$1200

loan_number (amount

> 1200

(loan))

Find the names of all customers who have a loan, an account,

or both, from the bank

customer_name (borrower) customer_name (depositor)

Find the names of all customers who have a loan at the Perryridge
branch.

customer_name (branch_name=Perryridge
(borrower.loan_number = loan.loan_number(borrower x loan)))

Find the names of all customers who have a loan


at the
Perryridge branch but do not have an account at
any branch of the bank.

Example
Queries

Find the names of all customers who have a loan at the


Perryridge branch.

customer_name (branch_name=Perryridge
(borrower.loan_number = loan.loan_number(borrower x
loan)))

Find the names of all customers who have a loan at the

Perryridge branch but do not have an account at any branch of


the bank.
customer_name
(branch_name = Perryridge

(borrower.loan_number = loan.loan_number(borrower x loan)))


customer_name(depositor)

Example
Queries

Find the names of all customers who have a loan at the Perryridge
branch.

Query 1

customer_name (branch_name = Perryridge (


borrower.loan_number = loan.loan_number (borrower x loan)))

Query 2

customer_name(loan.loan_number = borrower.loan_number (
(branch_name = Perryridge (loan)) x
borrower))

Example Queries

Find the largest account balance


Strategy:
Find those balances that are not the largest
Rename account relation as d so that we can compare
each account balance with all others
Use set difference to find those account balances that were
not found in the earlier step.
The query is:

balance(account) - account.balance
(account.balance < d.balance (account x d (account)))

Formal
Definition

A basic expression in the relational algebra consists of either


one of the following:
A relation in the database
A constant relation

Let E1 and E2 be relational-algebra expressions; the following


are all relational-algebra expressions:
E1 E2
E1 E2
E1 x E2
p (E1), P is a predicate on attributes in E1
s(E1), S is a list consisting of some of the attributes in E1

x (E1), x is the new name for the result of E1

Additional Operations

We define additional operations that do not add any power to the


relational algebra, but that simplify common queries.

Set intersection
Natural join
Division
Assignment

Set-Intersection
Operation

Notation: r s
Defined as:
r s = { t | t r and t s }
Assume:
r, s have the same arity
attributes of r and s are compatible
Note: r s = r (r s)

Set-Intersection Operation Example

Relation r, s:

1
2
1
r

rs

2
3
s

Natural-Join Operation

Notation: r

Let r and s be relations on schemas R and S respectively.


Then, r
s is a relation on schema R S obtained as follows:
Consider each pair of tuples t from r and t from s.
r
s
If tr and ts have the same value on each of the attributes in R
S, add a tuple t to the result, where
t has the same value as t on r
r

t has the same value as t on s


s
Example:
R = (A, B, C, D)
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))

Natural Join Operation


Relations r, s:
Example
A

1
2
4
1
2

a
a
b
a
b

1
3
1
2
3

a
a
a
b
b

r
r

s
A

1
1
1
1
2

a
a
a
a
b

Assignment Operation

The assignment operation () provides a convenient way to


express complex queries.
Write query as a sequential program consisting of
a series of assignments
followed by an expression whose value is displayed as a
result of the query.
Assignment must always be made to a temporary relation
variable.
Example: Write r s as
temp1 R-S (r )
temp2 R-S ((temp1 x s ) R-S,S (r ))
result = temp1 temp2
The result to the right of the is assigned to the relation
variable on the left of the .
May use variable in subsequent expressions.

Bank Example Queries


Find the names of all customers who have a loan and an

account at bank.

customer_name (borrower) customer_name (depositor)

Find the name of all customers who have a loan at the bank
and the loan amount

customer_name, loan_number, amount (borrower

loan)

Bank Example Queries

Find all customers who have an account from at least the


Downtown and the Uptown branches.
Query 1
customer_name (branch_name = Downtown (depositor

account ))

customer_name (branch_name = Uptown (depositor


Query 2
account))
customer_name, branch_name (depositor

account)

temp(branch_name) ({(Downtown ),
(Uptown )})
Note that Query 2 uses a constant relation.

Bank
Example
Queries

Find all customers who have an account at all branches located


in Brooklyn city.

customer_name, branch_name (depositor

account)

branch_name (branch_city = Brooklyn (branch))

Extended Relational-AlgebraOperations

Generalized Projection
Aggregate Functions
Outer Join

Generalized
Projection

Extends the projection operation by allowing arithmetic


functions to be used in the projection list.

F1 ,F2 ,..., Fn (E )

E is any relational-algebra expression

Each of F1, F2, , Fn are are arithmetic expressions involving

constants and attributes in the schema of E.


Given relation credit_info(customer_name, limit,
credit_balance), find how much more each person can spend:
customer_name, limit credit_balance (credit_info)

Aggregate Functions and Operations

Aggregation function takes a collection of values and returns


a single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate operation in relational algebra
G1 ,G2 ,,Gn

F ( A ),F ( A ,,F ( A ) (E )
1

E is any relational-algebra expression


G1, G2 , Gn is a list of attributes on which to group (can be
empty)
Each Fi is an aggregate function
Each Ai is an attribute name

Aggregate Operation
Relation r:
Example

g sum(c) (r)

10

sum(c )
27

Aggregate Operation
Relation account grouped by branch-name:
Example
branch_nameaccount_number
Perryridge
Perryridge
Brighton
Brighton
Redwood
branch_name

balance

A-102
A-201
A-217
A-215
A-222

sum(balance)

400
900
750
750
700

(account)

branch_name sum(balance)
Perryridge
Brighton
Redwood

1300
1500
700

Aggregate
Functions
(Cont.)

Result of aggregation does not have a name


Can use rename operation to give it a name
For convenience, we permit renaming as part of aggregate
operation

branch_name

sum(balance) as sum_balance

(account)

Outer
Join

An extension of the join operation that avoids loss of


information.
Computes the join and then adds tuples form one relation that
does not match tuples in the other relation to the result of the
join.
Uses null values:

null signifies that the value is unknown or does not exist


All comparisons involving null are (roughly speaking) false
by definition.
We shall study precise meaning of comparisons with
nulls later

Outer
Join

Example

Relation loan

loan_number branch_name amount


L-170
L-230
L-260

Downtown
Redwood
Perryridge

3000
4000
1700

Relation borrower

customer_nameloan_number
Jones
Smith
Hayes

L-170
L-230
L-155

Outer
Join

Example

Join

loan

borrower

loan_number branch_name amount customer_name


L-170
L-230

Downtown
Redwood

3000
4000

Jones
Smith

Left Outer Join

loan

borrower

loan_number branch_name amount customer_name


L-170
L-230
L-260

Downtown
Redwood
Perryridge

3000
4000
1700

Jones
Smith
null

Outer Join Example


Right Outer Join

loan

borrower

loan_number branch_name amount customer_name


L-170
L-230
L-155

Downtown
Redwood
null

3000
4000
null

Jones
Smith
Hayes

Full Outer Join

loan

borrower

loan_number branch_name amount customer_name


L-170
L-230
L-260
L-155

Downtown
Redwood
Perryridge
null

3000
4000
1700
null

Jones
Smith
null
Hayes

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 (as in SQL)

For duplicate elimination and grouping, null is treated like


any other value, and two nulls are assumed to be the same
(as in SQL)

Modification of the
The content of the database may be modified using the
Database
following operations:

Deletion
Insertion
Updating
All these operations are expressed using the assignment
operator.

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.
Can delete only whole tuples; cannot delete values on
only particular attributes
A deletion is expressed in relational algebra by:
rrE
where r is a relation and E is a relational algebra query.

Deletion
Examples

Delete all account records in the Perryridge branch.

account account

branch_name = Perryridge (account )

Delete all loan records with amount in the range of 0 to 50

loan loan

amount 0and amount 50 (loan)

Delete all accounts at branches located in Needham.


r1 branch_city = Needham (account
branch )
r2

account_number, branch_name, balance

r3 customer_name, account_number (r2


depositor)
account account r2
depositor depositor r3

(r1)

Insertion

To insert data into a relation, we either:


specify a tuple to be inserted
write a query whose result is a set of tuples to be inserted
in relational algebra, an insertion is expressed by:
r r E
where r is a relation and E is a relational algebra expression.
The insertion of a single tuple is expressed by letting E be a
constant relation containing one tuple.

Insertion
Examples

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)}

Provide as a gift for all loan customers in the Perryridge

branch, a $200 savings account. Let the loan number


serve
as the account number for the new savings account.

r1 (branch_name = Perryridge (borrower

loan))

account account loan_number, branch_name, 200 (r1)


depositor depositor customer_name, loan_number (r1)

Updating

A mechanism to change a value in a tuple without charging all


values in the tuple
Use the generalized projection operator to do this task

r F ,F ,,F , (r )

Each Fi is either

the I th attribute of r, if the I th attribute is not updated, or,


if the attribute is to be updated Fi is an expression,
involving only constants and the attributes of r, which gives
the new value for the attribute

Update Examples

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 account_number, branch_name, balance * 1.06 ( BAL 10000
(account ))
account_number, branch_name, balance * 1.05 (BAL 10000
(account))

S-ar putea să vă placă și