Notes of Database Management System Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY Page 1 UNIT -2 RELATIONAL ALGEBRA A query language is a language in which a user requests information from the database. These languages are usually on a level higher than that of a standard programming language. Query languages can be categorized as either procedural or nonprocedural. In a procedural language, the user instructs the system to perform a sequence of operations on the database to compute the desired result. In a nonprocedural language, the user describes the desired information without giving a specific procedure for obtaining that information. The relational algebra is a procedural query language. It 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. In addition to the fundamental operations, there are several other operations—namely, set intersection, natural join, division, and assignment. Standard Ways: – Two formal "languages": • Relational Algebra - allows the description of queries as a series of operations; • Relational Calculus - allows the description of the desired result. – One real language: • SQL - the standard relational database manipulation language. It is a collection of operations on relations .If you apply relational algebra on some relations, the result will be another relation You can say a relation- a table. So, operation on tables = another table Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory –Binary Relational Operations –Additional Relational Operations Relational Calculus –Tuple Relational Calculus –Domain Relational Calculus
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Notes of Database Management System
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY Page 1
UNIT -2
RELATIONAL ALGEBRA
A query language is a language in which a user requests information from the database. These languages
are usually on a level higher than that of a standard programming language. Query languages can be
categorized as either procedural or nonprocedural. In a procedural language, the user instructs the system to perform
a sequence of operations on the database to compute the desired result. In a nonprocedural language, the user
describes the desired information without giving a specific procedure for obtaining that information.
The relational algebra is a procedural query language. It 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. In addition to the fundamental
operations, there are several other operations—namely, set intersection, natural join, division, and assignment.
Standard Ways:
– Two formal "languages":
• Relational Algebra - allows the description of queries as a series of operations;
• Relational Calculus - allows the description of the desired result.
– One real language:
• SQL - the standard relational database manipulation language.
It is a collection of operations on relations .If you apply relational algebra on some relations, the result will
be another relation You can say a relation- a table. So, operation on tables = another table
Relational Algebra
–Unary Relational Operations
–Relational Algebra Operations From Set Theory
–Binary Relational Operations
–Additional Relational Operations
Relational Calculus
–Tuple Relational Calculus
–Domain Relational Calculus
Notes of Database Management System
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY Page 2
Refreshment of Memory:
� A relation is a set of attributes with values for each attribute such that:
� Each attribute value must be a single value only (atomic).
� All values for a given attribute must be of the same type (or domain).
� Each attribute name must be unique.
� The order of attributes is insignificant
� No two rows (tuples) in a relation can be identical.
� The order of the rows (tuples) is insignificant.
Relational Algebra Operations:
� Principal relation operations:
– select - pick rows from a relation by some condition;
– project - pick columns by name;
– join - connect two relations usually by a Foreign Key.
– Division – divide any two queries.
� Set theory operations:
– union - make the table containing all the rows of two relations;
– intersection - pick the rows which are common to two relations;
– difference - pick the rows which are in one table but not another;
– Cartesian product - pair off each of the tuples in one relation with those in another - creating a
double sized row for each pair.
– Rename – to get the same relation with new name
Note: All of the operations return relations
Set theory operations:
Referenced Tables -
Notes of Database Management System
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY Page 3
UNION -
� RUS means, a union operation on R and S that will produce another table which will have all the rows of R
and S except the duplicates
� There are 2 Sally in R and S, so, just take 1 from them.
DIFFERENCE –
� R-S means, a difference operation on R and S that will produce another table which will have all the rows
which is in R but not in S.
� As Sally is in R and in S, so, she has been omitted.
INTERSECTION –
� R∩S means, an intersection operation on R and S that will produce another table which will have all the rows
which are in both R and S.
� As Sally is in R and in S, so, she has been selected.
Notes of Database Management System
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY Page 4
More Union –
More Intersection –
More Difference –
1. Attributes of relations need not be identical to perform union, intersection and difference operations.
2. However, they must have the same number of attributes and the domains for corresponding attributes must be
identical.
3. Union, Intersection and difference operators may only be applied to Union Compatible relations.
4. Union and Intersection are commutative operations
RUS= SUR && R∩S = S∩R
5. Difference operation is NOT commutative.
R - S not equal S - R
Notes of Database Management System
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY Page 5
Things to do--
Find out RUT , Find out R∩T , Find out R-T , Find out T-R
Cartesian Product—
It provides all the combinations of rows from two tables. If A and B are 2 tables and A has 3 rows and
B has 4 rows, then A×B is-
A1-B1 A1-B2 A1-B3 A1-B4 ,
A2-B1 A2-B2 A2-B3 A2-B4 ,
A3-B1 A3-B2 A3-B3 A3-B4.
Notes of Database Management System
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY Page 6
RENAME – describe later
Principal relation operations:
Unary operations:
SELECTION-
Selection and Projection are unary operators. The selection operator is sigma (σ) .The selection operation
acts like a filter on a relation by returning only a certain number of rows. The resulting relation will have
the same number of attributes as the original relation. The resulting relation may have fewer rows
than the original relation. The rows to be returned are dependent on a condition that is part of the selection operator.
σC (R) Returns only those rows in R that satisfy condition C . A condition C can be made up of any combination of
comparison or logical operators that operate on the attributes of R. 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.
Properties:
The SELECT operation σ<selection condition>(R) produces a relation S that has the same schema as R