0% found this document useful (0 votes)
55 views

4-Relation Algebra Extended

The document summarizes key concepts in relational algebra including: 1) Generalized projection allows arithmetic functions to project new attributes. Aggregate functions like sum, avg, min, max return single values from collections. 2) Outer joins return all tuples by adding null values. Left, right, and full outer joins are explained. 3) Modification operations include delete, insert, and update expressed using assignment operators. Deletion and insertion use relational expressions while update uses generalized projection.

Uploaded by

sherlock holmes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

4-Relation Algebra Extended

The document summarizes key concepts in relational algebra including: 1) Generalized projection allows arithmetic functions to project new attributes. Aggregate functions like sum, avg, min, max return single values from collections. 2) Outer joins return all tuples by adding null values. Left, right, and full outer joins are explained. 3) Modification operations include delete, insert, and update expressed using assignment operators. Deletion and insertion use relational expressions while update uses generalized projection.

Uploaded by

sherlock holmes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

LECTURE 4

RELATIONAL ALGEBRA
(EXTENDED)

02 SEPTEMBER 2020
EXTENDED RELATIONAL-ALGEBRA-OPERATIONS

 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 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 1 2 2 n n

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 – EXAMPLE
Relation r:

A B C

  7
  7
  3
  10

 g sum(c) (r) sum(c )

27
AGGREGATE OPERATION – EXAMPLE
Relation account grouped by branch-name:

branch_name account_number balance


Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

branch_name g sum(balance) (account)

branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 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 g 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 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

 Relation borrower

customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
OUTER JOIN – EXAMPLE
Join
loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith

 Left Outer Join


loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
OUTER JOIN – EXAMPLE
 Right Outer Join
loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 null null Hayes
 Full Outer Join
loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
L-155 null 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)
NULL VALUES
 Comparisons with null values return the special truth value: unknown
E.g. A > null is equivalent to unknown
B <= null is equivalent to unknown

 Three-valued logic using the truth value unknown:

 OR: (unknown or true) = true,


(unknown or false) = unknown
(unknown or unknown) = unknown

 AND: (true and unknown) = unknown,


(false and unknown) = false,
(unknown and unknown) = unknown

 NOT: (not unknown) = unknown

 In SQL “P is unknown” evaluates to true if predicate P evaluates to unknown


Result of select predicate is treated as false if it evaluates to unknown
MODIFICATION OF THE DATABASE
The content of the database may be modified using the following
operations:

Delete
Insert
Update
Update

All these operations are expressed using the assignment operator.


DELETE
 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:
rr–E
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 0and amount  50 (loan)

 Delete all accounts at branches located in Needham.

r1  branch_city = “Needham” (account branch )


r2   account_number, branch_name, balance (r1)
r3   customer_name, account_number (r2 depositor)
account  account – r2
depositor  depositor – r3
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   F1,F2 ,,Fl , (r )

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy