0% found this document useful (0 votes)
11 views31 pages

Unit III Relational Model 2

Uploaded by

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

Unit III Relational Model 2

Uploaded by

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

Unit III: Relational Model

Part-2

Dr. Anushree Tripathi


Department of Computer Science and Engineering
National Institute of Technology Patna (NITP)

19th September, 2024


Overview
• Relational algebra
➢ Select
➢ Project
➢ Union
➢ Set difference
➢ Cartesian product
➢ Rename
Relational Algebra
Basics

• Procedural query language


• Basic set of operations that
Relational Algebra take one or two relations as
input and produce a new
relation in result
• Specify basic retrieval
requests as relational algebra
expression

Relational algebra • Sequence of relational


expressions algebra operations
Relational Algebra
Need

• To perform calculations on relations of data


• Fundamental basis of SQL
Relational Algebra
Fundamental Operations

Operations

Unary Binary
Operate on one Operate on pairs of
relation relations

Set Cartesian
Select Project Rename Union
difference product
Relational Algebra
Additional operations

Set
intersection

Other Natural join


Assignment
operators

Division
Relational Algebra
Select operation
• Need
➢ Selects tuples that satisfy a given selection condition

• Basics
➢ Denoted by sigma (σ)
➢ σ<selection condition>(R)
➢ Visualized as horizontal partition of relation into two sets of tuples
➢ Allows comparisons =, , >, , <,  in the selection predicate
➢ Boolean expression specified <selection condition>, made up of a
number of clauses
<attribute name><comparison op><constant value>
Or
<attribute name><comparison op><attribute name>
<attribute name>: Name of attribute of R
<comparison op>: One of operator =, , >, , <, 
<constant value>: Constant value from attribute domain
Relational Algebra
Select operation
• Relation r
A B C D
  1 8
  5 8
  13 4
  21 11

• A=B ^ D > 7 (r)

A B C D

  1 8
  21 11
Relational Algebra
Select operation (contd.)
The loan relation
• Example:
➢Input: branch-name=“Perryridge”(loan)

➢Output:

Reference: Silberschatz−Korth−Sudarshan, Database System Concepts, Fourth Edition


Relational Algebra
Select operation (contd.)
• Include comparison between two attributes
• Combine several predicate into larger predicate using connectives
 (and),  (or),  (not)
➢ Example:

➢ Input: dept_name = “Physics” (instructor)


➢ Output:

•  dept_name = “Physics” ∧ salary>90000 (instructor) The instructor relation

•  salary>90000 (instructor)
Relational Algebra
Select operation (contd.)

SELECT in SQL

SELECT condition specified in WHERE clause

Example: σamount>1200 (loan)

Query: SELECT * loan WHERE amount>1200;


Relational Algebra
Project

• Need
➢Selects certain columns from the table and discards other
columns
• Basics
➢Denoted by pi (Π)
➢Visualized as vertical partition of relation into two
relations (required and discarded)
➢Duplicate elimination: Removes any duplicate tuples
• Notation: Π<attribute list>(R)
Relational Algebra
Project operation (contd.)
• Relation r
A B C
 11 21
 22 21
 33 21
 44 51

• A,C (r)
A C
A C
 21
 21
 21 =
 21
 21
 51
 51
Relational Algebra
Project operation (contd.)
• Example
Πloan-number, amount(loan)

Output:

Reference: Silberschatz−Korth−Sudarshan, Database System Concepts, Fourth Edition


Relational Algebra
Project operation (contd.)
• Input: ΠID, name, salary (instructor)
• Output:

The instructor relation

Reference: Silberschatz−Korth−Sudarshan, Database System Concepts, Sixth Edition


Relational Algebra
Project operation (contd.)

Query: Find the name of all instructors in the Physics department

Πname ( dept name = “Physics” (instructor))

• Argument of project operation: Expression instead of giving name of


relation
Relational Algebra
Project operation (contd.)

PROJECT in SQL

PROJECT condition specified in SELECT clause of


query

Example: Πloan-number, amount(loan)

Query: SELECT DISTINCT loan-number, amount


FROM loan;
Relational Algebra
Union operation
• Need
➢ To include all tuples that are either in R or in S or in both R and S
• Basics
➢ Applied to two sets (of tuples)
➢ Union taken between compatible relations
➢ Have same number of attributes/columns
➢ Domain of every column must be same
➢ Duplicate tuples are eliminated
• For r  s to be valid, two conditions required:
1. r, s must have the same arity (same number of attributes)
2. Attribute domains must be compatible (e.g., 2nd column
of r deals with the same type of values as does the 2nd
column of s)

• Example
➢ To find all customers with either an account or a loan

customer-name (depositor)  customer-name (borrower)


Relational Algebra
Union operation (contd.)

• Relations r, s R S

A B A B
 1  2
 2  5
 1

RS

A B
 1
 2
 1
 5
Relational Algebra The section relation

Union operation (contd.)


• Query: Find the set of all courses
taught in Fall 2009 semester, the
Spring 2010 semester, or both
• Find the set of all courses taught in
Fall 2009
course_id ( semester = “Fall” ∧
year=2009 (section))
• Find the set of all courses taught in
Spring 2010 semester
course_id ( semester = “Spring” ∧ year=2010
(section))
• Union of two sets
course_id ( semester = “Fall” ∧ year=2009
(section))  course_id ( semester = “Spring”
∧ year=2010 (section))

Reference: Silberschatz−Korth−Sudarshan, Database System Concepts, Sixth Edition


Relational Algebra
Set difference operation

• Need
➢ To find tuples that are in one relation (R) but are not in another (S)
• Basics
➢ Denoted by ‘-’
➢ Notation: R – S
➢ Not commutative
R-S  S-R
➢ Set differences must be taken between compatible relations
▪ r and s must have the same arity
▪ attribute domains of r and s must be compatible
Relational Algebra
Set difference operation (contd.)

• Relations R, S R
S
A B
A B
 1
 2
 2
 5
 1

R-S
A B
 1
 1
Relational Algebra
Set difference operation (contd.)
• Find all the courses taught in the Fall 2009 semester but
not in Spring 2010 semester
course_id ( semester = “Fall” ∧ year=2009 (section)) − course_id (
semester = “Spring” ∧ year=2010 (section))
The section relation

Reference: Silberschatz−Korth−Sudarshan, Database System Concepts, Sixth Edition


Relational Algebra
Set difference operation (contd.)
• Πcustomer-name(depositor) − Πcustomer-name(borrower)

The depositor relation The borrower relation

• Output:

Reference: Silberschatz−Korth−Sudarshan, Database System Concepts, Fourth Edition


Relational Algebra
Cartesian product operation
Need
➢ To combine information from any two relations
Basics
• Denoted by a cross (×)
• Cartesian product of relations r1×r2
• If n1 tuples in instructor and n2 in teaches, then
n1 ∗ n2 tuples in r
• Relations r1(R1) and r2(R2), then r1 × r2 is a relation whose schema is
the concatenation of R1 and R2
Relational Algebra
Cartesian product operation (contd.)
• Relations R, S R S

A B C D E

 1  10 a

 2  11 b
 21 c
A B C D E
 1  10 a
• R×S
 1  11 b
 1  21 c
 2  10 a
 2  11 b
 2  21 c
Relational Algebra
Cartesian product operation (contd.)
Example
• Find the names of all instructors in the Physics department together
with course_id of all courses
 dept_name = “Physics” (instructor × teaches)

Reference: Silberschatz−Korth−Sudarshan, Database System Concepts, Sixth Edition


Relational Algebra
Cartesian product operation (contd.)
• Find the names of all instructors in the Physics department together
with the course_id of all courses they taught
• name,course_id (instructor.ID = teaches.ID (dept_name = “Physics”(instructor ×
teaches)))

Result of instructor × teaches

Reference: Silberschatz−Korth−Sudarshan, Database System Concepts, Sixth Edition


Relational Algebra
Rename operation
• Need
➢ To provide name to relational algebra expressions
➢ Alternate: Positional notation
• Basics
➢ Denoted by rho (ρ)
Given a relational-algebra expression E

• ρx(E),
• Expression returns the result of expression E under the name x

If relational algebra expression E has arity n, expression

• ρx (A1 x A2 x … x An)(E)
• Returns the result of expression E under the name x and with
attributes renamed to A1,A2,…,An
Relational Algebra
Rename operation (contd.)
Example Query: Find the highest salary in university

Step 1: Compute a temporary relation consisting of salaries,


not largest.
Compute cartesian product instructor × instructor and form a
selection to campare value of any two salaries
 instructor.salary ( instructor.salary < d.salary (instructor × ρd (instructor)))

Step 2: Find the largest salary in university


Take the set difference between the relation
salary(instructor) and the computed temporary relation
 salary(instructor) −  instructor.salary ( instructor.salary < d.salary (instructor × ρd
(instructor)))
Relational Algebra
Rename operation (contd.)
•  instructor.salary ( instructor.salary < d.salary (instructor × ρd (instructor)))
Output: The instructor relation

•  salary(instructor) −  instructor.salary ( instructor.salary < d.salary (instructor × ρd


(instructor)))

Reference: Silberschatz−Korth−Sudarshan, Database System Concepts, Sixth Edition

You might also like