Relational algebra calculus

16
Relational Algebra Relational Algebra is : The formal description of how a relational database operates An interface to the data stored in the database itself 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. For example, the SELECT statement exists in SQL, and also exists in relational algebra. These two uses of SELECT are not the same. The DBMS must take whatever SQL statements the user types in and translate them into relational algebra operations before applying them to the database.

description

RELATIONAL ALGEBRA CALCULES

Transcript of Relational algebra calculus

Page 1: Relational algebra  calculus

Relational Algebra

Relational Algebra is :

• The formal description of how a relational database operates

• An interface to the data stored in the database itself

• The mathematics which underpin SQL operations

Operators in relational algebra are not necessarily the same as SQL operators, evenif they have the same name.

For example, the SELECT statement exists in SQL, and also exists in relationalalgebra. These two uses of SELECT are not the same. The DBMS must take whateverSQL statements the user types in and translate them into relational algebraoperations before applying them to the database.

Page 2: Relational algebra  calculus

Important Terminology

• Relation - 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 3: Relational algebra  calculus

Operators - Write

• INSERT - provides a list of attribute values for a new tuple in a relation. Thisoperator is the same as SQL.

• DELETE - provides a condition on the attributes of a relation to determine whichtuple(s) to remove from the relation. This operator is the same as SQL.

• MODIFY - changes the values of one or more attributes in one or more tuples of arelation, as identified by a condition operating on the attributes of the relation.This is equivalent to SQL UPDATE.

Operators –Retrieval

There 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 4: Relational algebra  calculus

Relational SELECT

• SELECT 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:

SELECTdob '01/JAN/1950'(employee)

Relational PROJECT

• The PROJECT operation is used to select a subset of the attributes of a relationby specifying the names of the required attributes.

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

PROJECTsurname,empno(employee)

Page 5: Relational algebra  calculus

• SELECT and PROJECT

SELECT and PROJECT can be combined together.

For example, to get a list of employee numbers for employees in department number 1:

Page 6: Relational algebra  calculus

JOIN Operator

• JOIN is used to combine related tuples from two relations:

• In its simplest form the JOIN operator is just the cross product of the two relations.

• As the join becomes more complex, tuples are removed within the cross product to make the result of the join more meaningful.

• JOIN allows you to evaluate a join condition between the attributes of the relations on which the join is undertaken.

The notation used is

R JOINjoin condition S

Page 7: Relational algebra  calculus

The relational model is based on the principle of relational algebra.Relational algebra is a collection of operators that operate onrelations. Each operator takes one or two relations as its input andproduces a new relation as its output.

RESTRICT:

The Restrict operator extracts specified tuples or rows from a given relation, based on a condition.

For example, a student table has the attributes ROLL NO[ roll number], STUDNAME [ studentname], AGE and GENDER. The condition is to extract the tuples of only those students whoseage is more than 25. The resultant relation appears, as follows:

Page 8: Relational algebra  calculus

PROJECT :

• The Project operator extracts specified attributes or columnsfrom a given relation

For example, if only the name and age of the students is to beextracted, the resultant relation appears, as follows [assumethat the student table has only six tuples]

Page 9: Relational algebra  calculus

PRODUCT :

The product operator builds a relation from two specifiedrelations. It consists of an possible combinations of tuples, onefrom each of the two relations.

To be product compatible, the two tables must have common attributes. The Product operator creates Cartesian product between the two tables.

Page 10: Relational algebra  calculus

UNION :• The Union operator builds a relation from tuples appearing in either or both

of the specified relations.

• To be union compatible, the two tables should have the same types ofattributes [sets of columns having the same data types]

• For example, Consider two tables A and B.

• A contains the roll numbers and names of all students whose majordiscipline is computer science. B contains the roll numbers and names of allstudents whose major discipline is mathematics. These tables are unioncompatible because they have the same types of attributes.

Page 11: Relational algebra  calculus

Relational Calculus

Relational algebra is a procedural language, in which user has to write the steps orprocedure to obtain the required results but in general a user should not have to beconcerned with the details of how to obtain information. In relational calculus useris not concerned with the procedure to obtain the results, he/she just tell his/herrequirements and the output is available without knowing the method about itsretrieval

In relational calculus, a query is expressed as a formula consisting of a number ofvariables and an expression involving these variables. It is up to the DBMS totransform these nonprocedural queries into equivalent, efficient, proceduralqueries.

The relational calculus is used to measure the selective power of relationallanguages. A language that can be used to produce any relation that can be derivedusing the relational calculus is said to be relationally complete.

Page 12: Relational algebra  calculus

Relation calculus, which in effect means calculating with relations, is based onpredicate calculus, which is calculating with predicates. It is a formal language usedto symbolize logical arguments in mathematics. Propositions specifying a propertyconsist of an expression that names an individual object, and another expression,called the predicate, that stands for the property that the individual objectpossesses.

If for instance, p and q are propositions, we can build other propositions "not p", "p or q", "p and q" and so on. In predicate calculus, propositions may be built not only out of other propositions but also out of elements that are not themselves propositions. In this manner we can build a proposition that specifies a certain property or characteristic of an object.

Relational Calculus

Page 13: Relational algebra  calculus

Relational Calculus can be classified in two categories:

• Tuple Oriented relational Calculus

• Domain Oriented relational calculus

In relational tuple calculus, the variables represent the tuplesfrom specified relations; in relational domain calculus, thevariables represent values drawn from specified domains.

Page 14: Relational algebra  calculus

Tuple Oriented Relational Calculus

The tuple relational calculus is based on specifying a number of tuple variables. Eachsuch tuple variable normally ranges over a particular database relation. This meansthat the variable may take any individual tuple from that relation as its value.

A simple tuple relational calculus query is of the form { t I COND(t)·}, where '1' is atuple variable and COND(t) is a conditional expression involving '1'. The result of sucha query is a relation that contains all the tuples (rows) that satisfy COND(t)

For example,

The relational calculus query {t I BOOK(t) and t.PRICE>lOO} will get you all the bookswhose price is greater than 100. In the above example, the condition 'BOOK(t)'specifies that the range relation of the tuple variable '1' is BOOK. Each BOOK tuple 't'that satisfies the condition 't.PRICE> 100' will be retrieved. Note that 't.PRICE'references the attribute PRICE of the tuple variable '1'.

Page 15: Relational algebra  calculus

Domain Oriented Relational Calculus

The domain calculus differs from the tuple calculus in the type of variables usedin formulas. In domain calculus the variables range over single values fromdomains of attributes rather than ranging over tuples. To form a relation ofdegree 'n' for a query result, we must have 'n' of these domain variables-one foreach attribute.

An expression of the domain calculus is of the following form:

• {Xl, X2, ... , Xn I COND(XI, X2, .. ·, Xn, Xn+b Xn+2, , Xn+m)}

In the above expression Xl, X2, ... , Xn, Xn+b Xn+2, , Xn+m are domain variables that range over domains of attributes and COND is a condition or formula of the domain relational calculus.

Page 16: Relational algebra  calculus

Expression of the domain calculus are constructed from the following elements:

Domain variables Xl, X2, ... , Xn, Xn+b Xn+2, ... , Xn+m each domain variable is to range over some specified domain .

• Conditions, which can take two forms:

• Simple comparisons of the form x * y, as for the tuple calculus, except that x and yare now domain variables.

• Membership conditions, of the form R (term,

term ...).

Here, R is a relation, and each "term" is a pair AV, where A in turn is an attribute

Of R and V is either a domain variable or a constant.

For example EMP (empno: 100, ename: 'Ajay') is a membership condition (which evaluates to true if and only if there exists an EMP tuple having empno=100 and ename = 'Ajay') .