Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of...

19
Presented By: Miss N. Nembhard

Transcript of Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of...

Page 1: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Presented By: Miss N. Nembhard

Page 2: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
Page 3: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Relation AlgebraRelational Algebra is : the formal description of how a relational database operates the mathematics which underpin SQL operations.Operators in relational algebra are not necessarily the same as SQL operators, even if they have the same name.

Page 4: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

TerminologyRelation - a set of tuples.Tuple - a collection of attributes which

describe some real world entity.Attribute - a real world role played by a

named domain.Domain - a set of atomic values.Set - a mathematical definition for a

collection of objects which contains no duplicates.

Page 5: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Operators - RetrievalThere are two groups of operations:Mathematical set theory based relations:

UNION, INTERSECTION, DIFFERENCE, and CARTESIAN PRODUCT.

Special database operations: SELECT (not the same as SQL SELECT), PROJECT, and JOIN.

Page 6: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Query Languages

A language in which a user requests information from the database.

Usually on a higher level than that of standard programming languages.

Page 7: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Query LanguagesCan be categorized as either procedural or non-

procedural language.

Procedural - user instructs the system to a sequence of operations on the database to compute the desired result.

Non-procedural - the user describes the desired information without giving the specific procedure for obtaining the information.

Page 8: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Relational AlgebraRelational Algebra is a “pure” language

because it is procedural whereas the tuple relational calculus and domain relational are non-procedural.

These languages lack the syntax of commercial languages, but illustrate the fundamental techniques for extracting data from a database.

Page 9: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Relational AlgebraA procedural query language. That 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.

Page 10: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Relational AlgebraThere are several other operations :

set intersection natural joindivisionassignment

Page 11: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Fundamental OperationsThe select, project, and rename operations

are called unary operations, because they operate on one relation.

The other three operations (union, set difference, Cartesian product) operate on pairs of relations and are, therefore, called binary, operations.

Page 12: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Select OperationSelect operation selects tuples that satisfy a

given predicate.

Use lower case Greek letter sigma () to denote selection. The predicate appears as a subscript to . The argument relation is in the parenthesis after the sign.

Page 13: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Select OperationSELECT is used to obtain a subset of the tuples of a relation that satisfy a select condition.

For example, find all employees born after 1st Jan 1950:

SELECT dob > ’01/JAN/1950’ (employee)

Page 14: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Project OperationA unary operation that returns its argument

relation, with certain attributes left out.Since the relation is a set duplicate rows are

eliminated.Projection is denoted by the uppercase Greek

letter pi ().

Page 15: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Project OperationThe PROJECT operation is used to select a subset of the attributes of a relation by specifying the names of the required attributes.

For example, to get a list of all employees surnames and employee numbers:

PROJECT surname,empno (employee)

Page 16: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

SELECT and PROJECT

FROM employeeWHERE depno = 1;

SELECT empno

PROJECT empno (SELECT depno = 1 (employee))

Mapping this back to SQL gives:

SELECT and PROJECT can be combined together. For example, to get a list of employee numbers for employees in department number 1:

Page 17: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Cartesian ProductThe Cartesian product operation, denoted by

a cross (x). Allows us to combine information from two

relations.We write the Cartesian product of relations

r1 and r2 as r1 x r2.

Page 18: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Cartesian ProductThe Cartesian Product is also an operator which works on two sets. It is sometimes called the CROSS PRODUCT or CROSS JOIN.It combines the tuples of one relation with all the tuples of the other relation.

Page 19: Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.

Cartesian Product

E 5F 4D 3B 2A 1

A 1C 2D 3E 4

A 1 A 1C 2D 3E 4

B 2 A 1C 2D 3E 4A 1C 2D 3E 4

F 4 A 1C 2D 3E 4

E 5 A 1C 2D 3E 4

D 3

A 1A 1A 1

B 2B 2B 2

D 3D 3D 3

F 4F 4F 4

E 5E 5E 5

R

S

R CROSS S