0% found this document useful (0 votes)
358 views28 pages

Relational Algebra in Dbms

The document introduces relational algebra, which is a procedural query language consisting of basic operations that take one or two relations as input and produce a new relation as output. The fundamental operations are select, project, union, set difference, Cartesian product, and rename. Select and project are unary operations that operate on a single relation, while the others are binary operations that operate on two relations. Examples of each operation are provided.

Uploaded by

sonal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
358 views28 pages

Relational Algebra in Dbms

The document introduces relational algebra, which is a procedural query language consisting of basic operations that take one or two relations as input and produce a new relation as output. The fundamental operations are select, project, union, set difference, Cartesian product, and rename. Select and project are unary operations that operate on a single relation, while the others are binary operations that operate on two relations. Examples of each operation are provided.

Uploaded by

sonal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

INTRODUCTION

TO
RELATIONAL
ALGEBRA
Relational Algebra
• 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.
• These operations enable a user to specify basic
retrieval requests (or queries)
Cont.
• The fundamental operations in the relational
algebra are select, project, union, set difference,
Cartesian product, and rename

• The select, project, and rename operations are


called unary operations, because they operate on
one relation

• The other three operations operate on pairs of


relations and are, therefore, called binary
operations
Unary Relational Operations
 SELECT (symbol:  (sigma))
 Selects a subset of rows from relation

 PROJECT (symbol:  (pi))


 Selects columns from relation

 RENAME (symbol:  (rho))


SELECTION
EXAMPLE
• Select the EMPLOYEE tuples whose department number is
4:
 DNO = 4 (EMPLOYEE)
• Select the employee tuples whose salary is greater than
$30,000:

SALARY > 30,000 (EMPLOYEE)

• Select the instructors in Physics with a salary


greater than $90,000, (and (∧), or (∨), and not
( ¬ ))

 dept name =“Physics” ∧ salary>90000 (instructor )


PROJECTION
EXAMPLE
• Example: To list each employee’s first and last
name and salary, the following is used:

LNAME, FNAME,SALARY(EMPLOYEE)
Composition of Relational Operations
Example
• “Find the name of all instructors in the
Physics department.
 name ( dept name =“Physics”
(instructor))
RENAME
• The general RENAME operation  can be
expressed by any of the following
forms:
– S(R) changes:
• the relation name only to S
– (B1, B2, …, Bn )(R) changes:
• the column (attribute) names only to B1, B1,
…..Bn
– S (B1, B2, …, Bn )(R) changes both:
• the relation name to S, and
Relational Algebra Operations
Set Theory
• Union
• Intersection
• Set Difference / Minus
• Cartesian Product
UNION
• It is a Binary operation, denoted by 

– The result of 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.


UNION
– The two operand relations R and S must be
“type compatible” (or UNION compatible)

• Two relations are union compatible if


– Relation R and S should have same arity, Both
have same number of columns
– Names of attributes and the domain type are
the same in both
Example
Tables:
Person (SSN, Name, Address, Hobby)
Professor (Id, Name, Office, Phone)
are not union compatible. However

 Name (Person) and  Name (Professor)


are union compatible and

15
INTERSECTION
• INTERSECTION is denoted by 

• The result of the operation R  S, is a relation


that includes all tuples that are in both R and
S
– The attribute names in the result will be the
same as the attribute names in R

• The two operand relations R and S must be


“type compatible
• Consider a query to find the set of all courses
taught in the Fall 2009 semester, the Spring
2010 semester, or both

• To find the set of all courses taught in the


particular year, we
• We can find all the courses taught in the Fall
2009 semester but not in Spring 2010
semester by writing:
Cartesian-Product Operation
• Denoted by x
• Also known as product or cross-join
• Notation r x s
• Combines tuples members of one relation to
other relation
• R (a1, a2 … an) x S(b1, b2 … bn)
– T (a1, a2 … an, b1, b2 … bn)
• If we need the all borrowers and loan holder
in chennai
•  branch = “chennai” (borrower x loan)
Cartesian-Product Operation
 Relations r, s:

 r x s:
SQL Built in Functions
NUMERIC FUNCTIONS:
• abs(n) – returns absolute value of n
SQL> select abs(-2) from dual; - 2
• ceil(n) – ceil(3.78) - 4
• floor(n) – floor(2.4) - 2
• sqrt(n)
• power(n,m)
SQL> select power(5,2) from dual;

- 25
• mod(a,b)
SQL Built in Functions
String/Character FUNCTIONS:
• initcap(string)
SQL > select initcap(‘hello’) from dual; - Hello
• upper(string)
• lower(string)
• ltrim(string)
SQL > select ltrim(‘ hello’) from dual; - hello
• rtrim(string)
• replace( )
SQL > select replace(‘hello’, ’ll’, ’r’) from dual; - hero
• length( )
SQL > select length(‘hello’) from dual; - 5

You might also like