You are on page 1of 36

Course: Database Management System

Lecture # 5
Relational algebra
Relational algebra operations work on one or more relations to define
another relation leaving the original intact. It means that the input for relational algebra
can be one or more relations and the output would be another relation, but the original
participating relations will remain unchanged and intact. Both operands and results are
relations, so output from one operation can become input to another operation. It means
that the input and output both are relations so they can be used iteratively in different
requirements.
Relational Algebra

Unary Operations Binary Operations

Selection Union

Projection Set Difference

Rename Cartesian Product


Relational Algebra

Relational algebra is a procedural query language. It gives a step by step process to

obtain the result of the query. It uses operators to perform queries.


• Types of Relational operation
Relational algebra
The Select Operation:

The select operation is performed to select certain rows or tuples


of a table, so it performs its action on the table horizontally. The
tuples are selected through this operation using a predicate or
condition. This command works on a single table and takes rows
that meet a specified condition, copying them into a new table.

Lower Greek letter sigma ( σ) is used to denote the selection. The

predicate appears as subscript to σ . The argument relation is


given in parenthesis following the σ . As a result of this operation
a new table is formed, without changing the original table.
Relational algebra
• Select Operation:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).
• Notation: σ p(r)
Where:
• σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR and
NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.

For example: LOAN Relation

BRANCH_NAME LOAN_NO AMOUNT

Downtown L-17 1000

Redwood L-23 2000

Perryride L-15 1500

Downtown L-14 1500

Mianus L-13 500

Roundhill L-11 900

Perryride L-16 1300


Relational algebra
Input:
σ BRANCH_NAME="perryride" (LOAN)
Output:
BRANCH_NAME LOAN_NO AMOUNT

Perryride L-15 1500

Perryride L-16 1300


Relational algebra
The Project Operator The Select operation works horizontally on
the table on the other hand the Project operator operates on a
single table vertically, that is, it produces a vertical subset of the
table, extracting the values of specified columns, eliminating
duplicates, and placing the values in a new table. It is unary
operation that returns its argument relation, with certain
attributes left out. Since relation is a set any duplicate rows are
eliminated. Projection is denoted by a Greek letter (П ). While
using this operator all the rows of selected attributes of a relation
are part of new relation
Relational algebra
• Project Operation:
o This operation shows the list of those attributes that we wish to
appear in the result. Rest of the attributes are eliminated from
the table.

o It is denoted by ∏.

• Notation: ∏ A1, A2, An (r)

• Where

• A1, A2, A3 is used as an attribute name of relation r.


Relational algebra
NAME STREET CITY
Jones Main Harrison

Smith North Rye

Hays Main Harrison

Curry North Rye

Johnson Alma Brooklyn

Brooks Senator Brooklyn

∏ NAME, CITY (CUSTOMER)

NAME CITY
Jones Harrison

Smith Rye

Hays Harrison

Curry Rye

Johnson Brooklyn

Brooks Brooklyn
Relational algebra
Rename Operation:

• The rename operation is used to rename the output relation. It is


denoted by rho (ρ).

Example: We can use the rename operator to rename STUDENT


relation to STUDENT1.
• ρ(STUDENT1, STUDENT)
Relational algebra
Union Operation:
o Suppose there are two tuples R and S. The union operation
contains all the tuples that are either in R or S or both in R & S.

o It eliminates the duplicate tuples. It is denoted by ∪.

• Notation: R ∪ S

A union operation must hold the following condition:


o R and S must have the attribute of the same number.

o Duplicate tuples are eliminated automatically.


Relational algebra

CUSTOMER_NAME ACCOUNT_NO

Johnson A-101

Smith A-121

Mayes A-321

Turner A-176

Johnson A-273

Jones A-472

Lindsay A-284

CUSTOMER_NAME LOAN_NO

Jones L-17

Smith L-23

Hayes L-15

Jackson L-14

Curry L-93

Smith L-11

Williams L-17
Relational algebra
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)

CUSTOMER_NAME

Johnson

Smith

Hayes

Turner

Jones

Lindsay

Jackson

Curry

Williams

Mayes
Relational algebra
Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in both R & S.

o It is denoted by intersection ∩.

• Notation: R ∩ S

• Example: Using the above DEPOSITOR table and BORROW table

• Input:
• ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)

• Output:
CUSTOMER_NAME

Smith

Jones
Relational algebra
Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in R but not in S.

o It is denoted by intersection minus (-).

• Notation: R - S

• Example: Using the above DEPOSITOR table and BORROW table

• Input:
• ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)

• Output:
CUSTOMER_NAME

Jackson

Hayes

Willians

Curry
Relational algebra
Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the
other table. It is also known as a cross product.
o It is denoted by X. EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME

o Notation: E X D 1 Smith A A Marketing

2 Harry C B Sales

3 John B C Legal
Input:
EMPLOYEE X DEPARTMENT
Output:
EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME

1 Smith A A Marketing

1 Smith A B Sales

1 Smith A C Legal

2 Harry C A Marketing

2 Harry C B Sales

2 Harry C C Legal

3 John B A Marketing

3 John B B Sales

3 John B C Legal
Join Operations
Join Operation: Join is a special form of cross product of
two tables. In Cartesian product we join a tuple of one table with
the tuples of the second table. But in join there is a special
requirement of relationship between tuples. For example if there
is a relation STUDENT and a relation BOOK then it may be
required to know that how many books have been issued to any
particular student. Now in this case the primary key of STUDENT
that is stId is a foreign key in BOOK table through which the join
can be made.
Join Operations
• A Join operation combines related tuples from different relations, if and only if a given
join condition is satisfied. It is denoted by ⋈.
• Example:

EMPLOYEE SALARY
EMP_CODE EMP_NAME EMP_CODE SALARY

101 Stephan 101 50000

102 Jack 102 30000

103 Harry 103 25000

1. Operation: (EMPLOYEE ⋈ SALARY)


• Result:
EMP_CODE EMP_NAME SALARY

101 Stephan 50000

102 Jack 30000

103 Harry 25000


Join Operations
Types of Join operations:
Outer Join
Inner Join
1. Left Outer Join
1. Natural Join
2. Right Outer Join
2. Theta Join
3. Full Outer Join
3. Equi Join
Join Operations
Inner Join:
• Contain only those tuples that satisfy the matching condition
(Theta Join, Equi Join, Natural Join)
Natural Join:
o A natural join is the set of tuples of all combinations in R and S that are equal on
their common attribute names.
o It is denoted by ⋈.
• Example: Let's use the above EMPLOYEE table and SALARY table:
• Input:
1. ∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
• Output:
EMP_NAME SALARY

Stephan 50000

Jack 30000

Harry 25000
Join Operations
Natural Join:
• Natural Join can only be performed if there is at least one common attribute
that exist between two relations. In addition, the attributes must have the
same name and domain.

• Natural Join does not use any comparison operator

• Notation: denoted by A⋈B


The natural join of two relations can be obtained by applying a Projection
operation to Equi Join of two relations. In terms of basic Operators:

Natural Join= Cartesian Product + Selection +Projection


Natural join is by default inner join because the tuples which does not satisfy
the conditions of join does not appear in result set
Join Operations
Natural Join Example:
Join Operations
Join Operations
Join Operations
Example of Theta Join
Join Operations
Equi join:
• It is also known as an inner join. It is the most common join. It is based on matched data
as per the equality condition. The equi join uses the comparison operator(=).
Example:
Join Operations
Outer Join:
• Extension of Join
• Contains matching tuples that satisfy the matching condition,
along with some or all tuples that do not satisfy the matching
condition
• Contains all rows from either one or both
• It uses null values (NULL signifies that the value is unknown or
does not exist).
Outer Join = Natural Join + Extra Information
(From Left, Right or both tables)
Join Operations
Outer Join:
• The outer join operation is an extension of the join operation. It
is used to deal with missing information.

• An outer join is basically of three types:

a. Left outer join

b. Right outer join

c. Full outer join


Join Operations
Left outer join:
o Left outer join contains the set of tuples of all combinations in R and S that are equal
on their common attribute names.
o In the left outer join, tuples in R have no matching tuples in S.
o It is denoted by ⟕.
Courses HOD
CID Course CID Name
100 Database 100 Rashid
101 Mechanics
Input: 102 Electronics
102 Saleem

Courses ⟕ HOD 104 Jameel

Output:
CID Course Name

100 Database Rashid

101 Mechanics NULL

102 Electronics Saleem


Join Operations
• Right outer join:
o Right outer join contains the set of tuples of all combinations in R and S that are
equal on their common attribute names.
o In right outer join, tuples in S have no matching tuples in R.
o It is denoted by ⟖.
Courses HOD
CID Course CID Name
100 Database 100 Rashid
101 Mechanics
102 Saleem
102 Electronics
104 Jameel
Input:
Courses ⟖ HOD
Output:
CID Course Name

100 Database Rashid

102 Electronics Saleem

104 NULL Jameel


Join Operations
Full outer join:
o In Full Outer Join, all the tuples from both left relation R1 and right relation R2 are
included in the resulting relation. The tuples of both relations R1 and R2 which do
not satisfy join condition, their respective unmatched attributes are made NULL.
o It is denoted by ⟗
Courses HOD
CID Course CID Name
100 Database
100 Rashid
101 Mechanics
102 Saleem
102 Electronics
104 Jameel
• Input:
1. Courses ⟗ HOD
• Output:
CID Course Name

100 Database Rashid

101 Mechanics NULL

102 Electronics Saleem

104 NULL Jameel


Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of
information.

o Integrity constraints ensure that the data insertion, updating, and other processes
have to be performed in such a way that data integrity is not affected.

o Thus, integrity constraint is used to guard against accidental damage to the


database.

Types of Integrity Constraints


Integrity Constraints
Domain constraints

o Domain constraints can be defined as the definition of a valid set of values for an
attribute.

o The data type of domain includes string, character, integer, time, date, currency, etc.
The value of the attribute must be available in the corresponding domain.

Example:
Integrity Constraints
Entity integrity constraints

o The entity integrity constraint states that primary key value can't be null.

o This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify those
rows.

o A table can contain a null value other than the primary key field.

Example:
Integrity Constraints
Referential Integrity Constraints

o A referential integrity constraint is specified between two tables.

o In the Referential integrity constraints, if a foreign key in Table 1 refers to the


Primary Key of Table 2, then every value of the Foreign Key in Table 1 must be null
or be available in Table 2.

Example:
Integrity Constraints
Key constraints

o Keys are the entity set that is used to identify an entity within its entity set
uniquely.

o An entity set can have multiple keys, but out of which one key will be the
primary key. A primary key can contain a unique and null value in the
relational table.

• Example:

You might also like