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

Class 9 Relational Algebra

Relational Algebra is a procedural query language used for querying relational databases. It uses operators like selection, projection, union, set difference, and Cartesian product to manipulate relations and produce new relations as output. Some key points: - Relational Algebra expressions specify queries by describing an algorithm for determining the result through a sequence of operations. - The basic operations include selection to retrieve rows, projection to retrieve columns, cross product to combine relations, union to combine rows from relations, and set difference to retrieve rows in one relation but not another. - Additional operations like intersection, join, division, and renaming are derived from the basic operations and make queries more powerful and flexible.

Uploaded by

Vijay Prime
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Class 9 Relational Algebra

Relational Algebra is a procedural query language used for querying relational databases. It uses operators like selection, projection, union, set difference, and Cartesian product to manipulate relations and produce new relations as output. Some key points: - Relational Algebra expressions specify queries by describing an algorithm for determining the result through a sequence of operations. - The basic operations include selection to retrieve rows, projection to retrieve columns, cross product to combine relations, union to combine rows from relations, and set difference to retrieve rows in one relation but not another. - Additional operations like intersection, join, division, and renaming are derived from the basic operations and make queries more powerful and flexible.

Uploaded by

Vijay Prime
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Relational Algebra

Concepts + Exercise and Solutions

Prof.G.R.Karpagam
PSG-CSE
Relational query languages
• Relational query languages are languages for describing queries on a relational
database. Introduced by Edgar 'Ted' Codd of IBM Research in 1970. Concept of
mathematical relation as the underlying basis. The standard database model for
most transactional databases today.

• Three variants:
– Relational Algebra
– Relational Calculus
– SQL

• Query Languages != programming languages!


• QLs not expected to be “Turing complete”.
• QLs not intended to be used for complex calculations.
• QLs support easy, efficient access to large data sets.

• Turing Machine is a mathematical model of a hypothetical computing machine A Turing Complete system means a
system in which a program can be written that will find an answer although with no guarantees regarding runtime or
memory
What is Algebra?
• A language based on operators and a domain of values
• Operators map values taken from the domain into other
domain values
• Hence, an expression involving operators and arguments
produces a value in the domain
• Consider arithmetic operations +, - and * over integers.
– Algebra expressions: 2+3, (46–3)+3, (7*x)+(3*x)
• Relational algebra:
– Domain: the set of all relations
– Expression: referred to as a query
Relational Algebra
Relational Algebra Vs Relational Calculus

• Relational Algebra and Relational Calculus are the formal query languages for a relational model.
Both form the base for the SQL language which is used in most of the relational DBMSs.

• Relational Algebra is a procedural language. On the other hands, Relational


Calculus is a declarative language

• Procedural: In a procedural language, you define the whole process and provide the steps how to
do it. You just provide orders and define how the process will be served.
– Go to kitchen
– Get sugar, milk, and tea,
– Mix them, and heat over the fire till it boils
– Put that in a cup and bring it to me

• Declarative: In a declarative language, you just set the command or order, and let it be on the
system how to complete that order. You just need your result without digging into how it should be
done.
– Get me a cup of tea.
Basis for
Comparison Relational Algebra Relational Calculus

Basic Procedural language. Declarative language.


States Relational Algebra states how to obtain Relational Calculus states
the result. what result we have to
obtain.

Order Relational Algebra describes the order Relational Calculus does


in which operations have to be not specify the order of
performed. operations.

Domain Relational Algebra is not domain Relation Calculus can be


dependent. domain dependent.

Related It is close to a programming language. It is close to the natural


language.
Relational calculus
• The sequence of relational calculus operations is called relational
calculus expression that also produces a new relation as a result.

• The Relational Calculus has two variations namely Tuple Relational


Calculus and Domain Relational Calculus.
• The Tuple Relational Calculus list the tuples to selected from a
relation, based on a certain condition provided. It is formally
denoted as:
{ t | P(t) }

• Where t is the set of tuples for which the condition P is true.


Relational calculus
• The next variation is Domain Relational Calculus, which
in contrast to Tuple Relational Calculus list the
attributes to be selected from a relation, based on
certain condition.

• The formal definition of Domain Relational Calculus is


as follow:
{<X1, X2, X3, . . . Xn> | P(X1, X2, X3, . . . Xn)}
• Where X1, X2, X3, . . . Xn are the attributes and P is the
certain condition.
Relational Algebra
• Domain: set of relations
• Basic operators: select, project, union, set
difference, Cartesian product
• Derived operators: set intersection, division, join
• Unary Operator, Binary Operators
• Procedural: Relational expression specifies query
by describing an algorithm for determining the
result of an expression
Relational Algebra
• Basic operations:
– Selection (  ) Selects a subset of rows from relation.
– Projection (  ) Deletes unwanted columns from relation.


Cross-product ( ) Allows us to combine two relations.

– Union ( 

Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
) Tuples in reln. 1 and in reln. 2.
• Additional operations:
– Intersection, join, division, renaming: Not essential, but (very!)
useful.
• Since each operation returns a relation, operations can be
composed: algebra is “closed”.
Relational Algebra

• A relation schema is given by R(A1,…,Ak), the name of the


relation and the list of the attributes in the relation

• A relation is a set of tuples that are valid instances of its schema

• Relational algebra expressions take as input relations and


produce as output new relations.

• After each operation, the attributes of the participating


relations are carried to the new relation. The attributes may be
renamed, but their domain remains the same.
Select operator Retrieves subsets of rows from
a relational table

A B C D E
1 A 212 Y 2
2 C 45 N 84
3 B 8656 N 4
4 D 324 N 56
5 C 5656 Y 34
6 A 445 N 4
7 B 546 Y 55
Selection

SELECTION: selection-condition ( R )
 Select from R all tuples that satisfy the selection-
condition

 The condition only refers to the attributes in R

 An atomic-selection-condition is of the form


 relation-attribute operation constant, or
 relation-attribute operation relation-attribute

 A selection-condition is obtained by boolean


combination of atomic selection conditions by means
of connectives AND, OR, and NOT.
Selection

s Enrollment>MaxEnrollment AND Enrollment>100 (CLASS)


s Grade=‘A’ (TRANSCRIPT)
s Year=2005 AND (Semester=‘odd’ OR Semester=‘even’) (CLASS)
s Name like ‘%gam’ (FACULTY)
The project operator retrieves subsets of columns from a
relational table removing duplicate rows from the result

A B C D E
1 A 212 Y 2
2 C 45 N 84
3 B 8656 N 4
4 D 324 N 56
5 C 5656 Y 34
6 A 445 N 4
7 B 546 Y 55
Projection
PROJECT: attribute-list (R)

• Return from R all tuples, but remove from all tuples


any attribute that is not in the attribute list

• The attribute list can only refer to attributes in R

Examples:
  ID (FACULY)
  ID ( Name like ‘%gam’ FACULTY )
 CrsCode, Textbook (COURSE)
  PrereqCrsCode ( CrsCode=‘CSCI4380’ REQUIRES )
Unary Relational Operations: PROJECT

• 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 first and last name and


salary, the following is used:
LNAME, FNAME,SALARY(EMPLOYEE)

Slide 6- 23
Unary Relational Operations: PROJECT (contd.)

• 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.
Slide 6- 24
Unary Relational Operations: PROJECT (contd.)

• 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 <list1>

Slide 6- 25
Employee
Unary Relational Operations: SELECT and PROJECT

Slide 6- 27
Unary Relational Operations: RENAME

• 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.

Slide 6- 28
Rename
• Rename all the attributes in the relation

– Given a relation with schema R(A1,…,An)


– The expression R[B1,…,Bn] is used to rename
attribute A1 as B1, …, An as Bn.
– The rename operator does not change the domain
of the attributes.
– The rename operator does not change the
number of attributes in a relation (that can be
done by the projection operation).
Unary Relational Operations: RENAME (contd.)

• 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.
Slide 6- 30
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_EMPS)


Set Operations
• Relational tables are equivalent to sets
• Operations that can be performed on sets can be performed on relational

tables

INTERSECTION DIFFERENCE
UNION
Set Operators
• Set operators connect two relations by set
operations.
• However, for a set operation R1 op R2 to be valid, R1
and R2 should be union-compatible, that is
– R1, R2 have the same number of columns
– The names of the attributes are the same in both relations
– Attributes with the same name in both relations have the
same domain
Set Operators
• Given two relations R1, R2 that are union-
compatible, we have that
 R1  R2 returns the set of tuples that are in R1 or R2.
[UNION]
∆ R1  R2 returns the set of tuples that are both in R1 and
R2. [INTERSECTION]
∆ R1 - R2 returns the set of tuples that are in R1, but not in
R2. [SET DIFFERENCE]
∆ Note that set difference is the only negative operator, i.e.
not in R2.
Example Database
STUDENT(Id, Name, Password, Address)
FACULTY(Id, Name, DeptId, Password, Address)
COURSE(CrsCode, DeptId, CrsName, CreditHours)
REQUIRES(CrsCode, PrereqCrsCode, EnforcedSince)
CLASS(CrsCode, SectionNo, Semester, Year,Textbook, ClassTime,
Enrollment, MaxEnrollment, ClassroomId, InstructorId)
CLASSROOM(ClassroomId, Seats)
TRANSCRIPT(StudId, CrsCode, SectionNo, Semester, Year, Grade)
STUDENT(Id, Name, Password, Address)
FACULTY(Id, Name, DeptId, Password, Address)

Set Operators
  Name (FACULY)   Name (STUDENT)
  Address (FACULY)   Address (STUDENT)
  CrsCode (CLASS) -  CrsCode (TRANSCRIPT)

  Id (STUDENT) -  StudId (TRANSCRIPT)


 Problem: The two relations in this case are not union
compatible (even though the attribute numbers and
domains match, the names do not).
 Solution For this?
Rename
Corrected Expression
  Id (STUDENT) - ( StudId (TRANSCRIPT))[Id]

Example:
 Temp:=  InstructorId (Year=2020 AND Semester=‘odd’ COURSE)  
InstructorId (Year=2020 AND Semester=‘even’ COURSE)

‘All faculty who taught courses both in ‘odd’ and ‘even’ 2020.’

  Id (FACULTY) - Temp[Id]
All faculty who did not teach courses both in ‘odd’ and ‘even’
2020.’’
Relational Algebra Operations from
Set Theory: UNION
• UNION Operation
– The result of this operation, denoted by R  S, is a
relation that includes all tuples that are either in R
or in S or in both R and S. Duplicate tuples are
eliminated.

Slide 6- 38
Relational Algebra Operations from
Set Theory: UNION
• Example:
– To retrieve the social security numbers of all employees who either
work in department 5 or directly supervise an employee who
works in department 5
– We can use the union operation as follows:
DEP5_EMPS  DNO=5 (EMPLOYEE)
RESULT1   SSN(DEP5_EMPS)
RESULT2(SSN)  SUPERSSN(DEP5_EMPS)
RESULT  RESULT1  RESULT2
– The union operation produces the tuples that are in either RESULT1
or RESULT2 or both.
– The two operands must be “type compatible”.

Slide 6- 39
Relational Algebra Operations from
Set Theory
• Type Compatibility
– The operand relations R1(A1, A2, ..., An) and R2(B1,
B2, ..., Bn) must have the same number of
attributes, and the domains of corresponding
attributes must be compatible; that is,
dom(Ai)=dom(Bi) for i=1, 2, ..., n.
– The resulting relation for
• R1R2, R1R2, or R1-R2
– has the same attribute names as the first operand
relation R1 (by convention).
Slide 6- 40
Relational Algebra Operations from
Set Theory: UNION (contd.)
• UNION Example

STUDENTINSTRUCTOR

Slide 6- 41
Relational Algebra Operations from Set Theory
UNION (contd.) – use Fig. 6.4

Slide 6- 42
Intersection

k x y A Intersect B
1 A 2
2 B 4
Table A k x y
3 C 6 1 A 2
4 D 8
5 E 10

k x y Table B
1 A 2
Relational Algebra Operations from Set Theory:
INTERSECTION
• The result of this operation, denoted by R  S, is a
relation that includes all tuples that are in both R and S.
• The two operands must be “type compatible”
– Example: The result of the intersection operation (figure
below) includes only those who are both students and
instructors.

STUDENT  INSTRUCTOR

Slide 6- 44
MINUS
The difference of two relational tables is a third that contains those
rows that occur in the first table but not in the second. The Difference
operation requires that the tables be union compatible.
The notation for difference is A MINUS B or A-B. As with arithmetic, the
order of subtraction matters. That is, A - B is not the same as B - A.

k x y k x y
1 A 2 Table A A MINUS B 2 B 4
2 B 4 3 C 6
3 C 6

k x y k x y
B MINUS A
1 A 2 4 D 8
Table B
4 D 8 5 E 10
5 E 10
Relational Algebra Operations from Set Theory:
DIFFERENCE (cont.)
• Set Difference (or MINUS) Operation
• The result of this operation, denoted by R - S, is a
relation that includes all tuples that are in R but
not in S. The two operands must be "type
compatible”.
– Example: The figure shows the names of students
who are not instructors, and the names of instructors
who are not students.

STUDENT-INSTRUCTOR INSTRUCTOR-STUDENT
Slide 6- 46
Relational Algebra Operations from Set Theory

• Notice that both union and intersection are commutative


operations; that is
– R  S = S  R, and R  S = S  R
• Both union and intersection can be treated as n-ary
operations applicable to any number of relations as both are
associative operations; that is
– R  (S  T) = (R  S)  T
– (R  S)  T = R  (S  T)
• The minus operation is not commutative; that is, in general
– R-S≠S–R

Slide 6- 47
Cartesian Product
• Given two sets, A={a1,a2,a3}, B={b1,b2}, the Cartesian
Product (A  B) returns the set of tuples
A  B={(a1,b1), (a1,b2), (a2,b1), (a2,b2), (a3,b1), (a3,b2)}

• The Cartesian product for relations is a generalization of this


concept.

You might also like