the relational algebra. 1 relational algebrarelational algebra 2 unary relational operations *...

13
The Relational Algebra

Upload: katherine-candice-dalton

Post on 02-Jan-2016

223 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

The Relational Algebra

Page 2: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT
Page 3: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

BACK TO INDEX

The basic set of operations for the relational model is known as the relational algebra. These operations enable a user to specify basic retrieval requests.

The result of a retrieval is a new relation, which may have been formed from one or more relations. The algebra operations thus produce new relations, which can be further manipulated using operations of the same algebra.

A sequence of relational algebra operations forms a relational algebra expression, whose result will also be a relation that represents the result of a database query (or retrieval request).

Page 4: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

SELECT Operation

SELECT operation is used to select a subset of the tuples from a relation

that satisfy a selection condition. It is a filter that keeps only those tuples

that satisfy a qualifying condition – those satisfying the condition are

selected while others are discarded.

Example: To select the EMPLOYEE tuples those whose salary is greater

than 8000 the following notation is used:

SALARY >= 8000 (EMPLOYEE)

In general, the select operation is denoted by <selection condition>(R) where the

symbol (sigma) is used to denote the select operator, and the selection

condition is a Boolean expression specified on the attributes of relation R

Page 5: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

SELECT Operation Properties

◦ It is an Unary Operation i.e. it can operate only on a single a relation.

◦ The resulting relation (table) has the same degree as that of the original relation(table). This means that the number of columns in both the relations are same.

◦ The number of rows of the resulting relation(table) is always less than or equal to number of rows of the original relation(table).

◦ The SELECT operation <selection condition>(R) produces a relation S that has the same schema as R

◦ The SELECT operation is commutative; i.e., <condition1>(< condition2>

( R)) = <condition2> (< condition1> ( R))

◦ The selection operation operates on each row of the relation(table) independently.

Page 6: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

ID NAME SALARY

501 AMIT 10000

503 ANAND 6000

504 SONAL 7000

507 RAJNI 8000

510 KAPIL 15000

512 RANJIT 12000

EMPLOYEE

ID NAME SALARY

501 AMIT 10000

507 RAJNI 8000

510 KAPIL 15000

512 RANJIT 12000

RESULT OF SELECTION

SALARY >= 8000 (EMPLOYEE)

BACK TO INDEX

Page 7: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

PROJECT Operation

This operation selects certain columns from the table and discards the other columns. The PROJECT creates a vertical partitioning – one with the needed columns (attributes) containing results of the operation and other containing the discarded Columns.

Example: To list each employee’s name and salary, the following is used:NAME,SALARY(EMPLOYEE)

The general form of the project operation is <attribute list>(R) where (pi) is the symbol used to represent the project operation and <attribute list> is the desired list of attributes from the attributes of relation R.

The project operation removes any duplicate tuples, so the result of the project operation is a set of tuples and hence a valid relation.

Page 8: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

PROJECT Operation Properties

◦ The number of tuples in the result of projection Π <list> (R)is always less or equal to the number of tuples in R.

◦ If the list of attributes includes a key of R, then the number of tuples is equal to the number of tuples in R.

◦ Π <list1> (Π <list2> (R) ) = Π <list1> (R) as long as <list2> contains the attributes in <list2>

◦ It is an Unary Operation i.e. it operates only on a single relation(table).

◦ It doesn’t hold the commutative property.

◦ The projection operation removes the duplicate rows from the table which

results in a valid relation(table) known as duplicate elimination.

Page 9: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

ID NAME SALARY

501 AMIT 10000

503 ANAND 6000

504 SONAL 7000

507 RAJNI 8000

510 KAPIL 15000

512 RANJIT 12000

EMPLOYEE

NAME SALARY

AMIT 10000

ANAND 6000

SONAL 7000

RAJNI 8000

KAPIL 15000

RANJIT 12000

RESULT OF PROJECTION

NAME,SALARY(EMPLOYEE)

BACK TO INDEX

Page 10: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

Rename Operation

We may want to apply several relational algebra operations one after the

other. Either we can write the operations as a single relational algebra

expression by nesting the operations, or we can apply one operation at a

time and create intermediate result relations. In the latter case, we must

give names to the relations that hold the intermediate results.

Example: To retrieve the first name, last name, and salary of all employees

who work in department number 5, we must apply a select and a project

operation. We can write a single relational algebra expression as follows:

FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))

OR We can explicitly show the sequence of operations, giving a name to

each intermediate relation:

DEP5_EMPS DNO=5(EMPLOYEE)

RESULT FNAME, LNAME, SALARY (DEP5_EMP5)

Page 11: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

Rename Operation (cont.)

The rename operator is The general Rename operation can be expressed by any of the following forms:

S (B1, B2, …, Bn ) ( R) is a renamed relation S based on R with column names

B1, B1, ……….…..Bn.

S ( R) is a renamed relation S based on R (which does not specify column

names).

(B1, B2, …, Bn ) ( R) is a renamed relation with column names B1, B1,

…..Bn which does not specify a new relation name.

Page 12: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

BACK TO INDEX

Page 13: The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT

BACK TO INDEX