You are on page 1of 10

UNIT 3

Introduction of Relational Algebra in DBMS


Relational Algebra is procedural query language, which takes Relation as input and
generate relation as output. Relational algebra mainly provides theoretical
foundation for relational databases and SQL.

Types of Relational operation

Operators in Relational Algebra

Projection (π)
Projection is used to project required column data from a relation.
It is denoted by ∏.

Notation: ∏ A1, A2, An (r)

Where

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

Example :
customer
Name Address contact
Mary kohima 1223
Lily peren 2343
John kohima 3455
Samuel dimapur 6778
Mary Kohima 7689

Input:
∏ Name, Address(customer)
Output:
customer
Name Address
Mary kohima
Lily peren
John kohima
Samuel dimapur

Note: By Default projection removes duplicate data.

Selection (σ)
Selection is used to select required tuples of the relations.
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).
o 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 =, ≠, ≥, <, >, ≤.

Note: selection operator only selects the required tuples but does not display them.
For displaying, data projection operator is used.

Example:
student
Roll_No Name Age
1 A 20
2 B 21
3 A 22

Input:

σ NAME="A" (student)

output:
Roll_no Name Age
1 A 20
3 A 22

Union (U)
Union operation in relational algebra is same as union operation in set theory, only
constraint is for union of two relation both relation must have same set of Attributes.
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.

Example: (R U S)

Name Roll_no
A 1
B 2
C 3
R

Name course
A C1
E C1
S
∏ NAME (S) ∪ ∏ NAME (R)

Output:
Name
A
B
C
E

Set Difference (-)


Set Difference in relational algebra is same set difference operation as in set theory
with the constraint that both relation should have same set of attributes.
Set difference=(A-B) i.e: (student-employee)
Example:
Roll_No Name
1 A
2 B
3 C
Student

Name Emp_No
B E1
E E2
F E3
Employee

Output:
Roll_No Name
1 A
3 C

Rename (ρ)
Rename is a unary operation used for renaming attributes of a relation.
ρ (a/b)R will rename the attribute ‘b’ of relation by ‘a’.

Example: We can use the rename operator to rename STUDENT relation to


STUDENT1.

ρ(STUDENT1, STUDENT)

Cross Product (X)


Cross product between two relations let say A and B, so cross product between A X B
will results all the attributes of A followed by each attribute of B. Each record of A will
pairs with every record of B.
below is the example
A B
(Name Age Sex ) (Id Course)
------------------ -------------
Ram 14 M 1 DS
Sona 15 F 2 DBMS
kim 20 M

AXB
Name Age Sex Id Course
---------------------------------
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS

Note: if A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘n*m’ tuples.

Functional dependencies:
A functional dependency is a constraint that specifies the relationship
between two sets of attributes where one set can accurately determine the value of
other sets. It is denoted as X → Y, where X is a set of attributes that is capable of
determining the value of Y. The attribute set on the left side of the arrow, X is
called Determinant, while on the right side, Y is called the Dependent. Functional
dependencies are used to mathematically express relations among database
entities and are very important to understand advanced concepts in Relational
Database System and understanding problems in competitive exams like Gate.

Example:
Roll_no Name Dept_name Dept_building
1 A CO A4
2 B IT A3
3 C CO A4
4 C IT A4
5 D EC B2
5 E ME B2

From the above table we can conclude some valid functional dependencies:
• roll_no → { name, dept_name, dept_building },→ Here, roll_no can
determine values of fields name, dept_name and dept_building, hence a
valid Functional dependency
• roll_no → dept_name , Since, roll_no can determine whole set of {name,
dept_name, dept_building}, it can determine its subset dept_name also.
• dept_name → dept_building , Dept_name can identify the dept_building
accurately, since departments with different dept_name will also have a
different dept_building
• More valid functional dependencies: roll_no → name, {roll_no, name} ⇢
{dept_name, dept_building}, etc.

Here are some invalid functional dependencies:


• name → dept_name Students with the same name can have different
dept_name, hence this is not a valid functional dependency.
• dept_building → dept_name There can be multiple departments in the
same building, For example, in the above table departments ME and EC
are in the same building B2, hence dept_building → dept_name is an
invalid functional dependency.
• More invalid functional dependencies: name → roll_no, {name,
dept_name} → roll_no, dept_building → roll_no, etc.

Types of Functional dependencies in DBMS:


1. Trivial functional dependency
2. Non-Trivial functional dependency

1. Trivial Functional Dependency


In Trivial Functional Dependency, a dependent is always a subset of the
determinant.
i.e.
• If X → Y and Y is the subset of X, then it is called trivial functional
dependency.
• The following dependencies are also trivial like: A → A, B → B
For example:
Roll_no Name Age
1 A 17
2 B 18
3 C 18

{Roll_no, name} → name is a trivial functional dependency, since the


dependent name is a subset of determinant set {roll_no, name}.
Similarly, Roll_no → roll_no is also an example of trivial functional dependency.

2. Non-trivial Functional Dependency


In Non-trivial functional dependency, the dependent is strictly not a subset of
the determinant.
i.e.
• If X → Y and Y is not a subset of X, then it is called Non-trivial functional
dependency.
• When A intersection B is NULL {(A∩B) →Ø}, then A → B is called as complete
non-trivial.

For example:
Roll_no Name Age
1 A 11
2 B 12
3 C 12

Roll_no → name is a non-trivial functional dependency, since the


dependent name is not a subset of determinant roll_no.
Similarly, {roll_no, name} → age is also a non-trivial functional dependency,
since age is not a subset of {roll_no, name}

Armstrong’s axioms/Properties of functional dependencies/ Inference Rule:

1.Reflexivity:
X→X
X→Y, Y⊆X
Example: Roll_no→ name
Roll_no→Roll_no

2.Augmentation:
If X → Y is a valid dependency, then XZ → YZ is also valid by the
augmentation rule. i.e
If(X→Y) then XZ→YZ
For example, If Roll_no → dept_building is valid, then Roll_no, dept_name} →
{dept_building, dept_name} is also valid.
3.Transitivity:
If X → Y and Y → Z are both valid dependencies, then X→Z is also valid by
the Transitivity rule. i.e if (If X → Y & Y → Z ) then X→Z
For example, roll_no → dept_name & dept_name → dept_building, then roll_no →
dept_building is also valid.

4.Union:
If (X→Y & X→Z) then X→YZ
Example: Roll_no→name, Roll_no→marks then Roll_no→name,marks

5. Decomposition/splitting:
If (X→YZ) then X→Y & X→Z

6. Composition:
If (X→Y & A→B) then XA→YB

Normalization
o Normalization is the process of organizing the data in the database.
o Normalization is used to minimize the redundancy from a relation or set of
relations. It is also used to eliminate the undesirable characteristics like
Insertion, Update and Deletion Anomalies.
o Normalization divides the larger table into the smaller table and links them
using relationship.
o The normal form is used to reduce redundancy from the database table.

Types of Normal Forms

1. First Normal Form or 1NF


2. Second Normal Form or 2NF
3. Third Normal Form or 3NF
4. Fourth Normal Form or 4NF
5. Fifth Normal Form or 5NF

First Normal Form (1NF)

1NF is the First Normal Form. It is the simplest type of normalization that you can
implement in a database. The primary objectives of 1NF are to:

• Every column must have atomic (single value)


• To Remove duplicate columns from the same table
• Create separate tables for each group of related data and identify each row
with a unique column.
• A relation will be 1NF if it contains an atomic value.
• It states that an attribute of a table cannot hold multiple values. It must hold
only single-valued attribute.
• First normal form disallows the multi-valued attribute, composite attribute,
and their combinations.
• It is partial functional dependency.

Second Normal Form (2NF)

2NF is the Second Normal Form. A table is said to be 2NF if it follows the following
conditions:

• In the 2NF, relational must be in 1NF.


• In the second normal form, all non-key attributes are fully functional
dependent on the primary key.
• There should be no partial dependency.

Third Normal Form (3NF)

3NF stands for Third Normal Form. A database is called in 3NF if it satisfies the following
conditions:

• A relation will be in 3NF if it is in 2NF.


• It should not contain any transitive partial dependency.
• 3NF is used to reduce the data duplication. It is also used to achieve the data
integrity.
• If there is no transitive dependency for non-prime attributes, then the
relation must be in third normal form.
• For example: X->Z
Where:
X->Y
Y does not -> X
Y->Z so, X->Z

A relation is in third normal form if it holds atleast one of the following conditions for
every non-trivial function dependency X → Y.

1. X is a super key.
2. Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Boyce Codd normal form (BCNF)

BCNF stands for Boyce-Codd Normal Form. It is an advanced version of 3NF, so it is


also referred to as 3.5NF. BCNF is stricter than 3NF.

A table complies with BCNF if it satisfies the following conditions:

• It is in 3NF.
• For every functional dependency X->Y, X should be the super key of the
table. It merely means that X cannot be a non-prime attribute if Y is a prime
attribute.
• For BCNF, the table should be in 3NF, and for every FD, LHS is super key.

Fourth normal form (4NF)

o A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-
valued dependency.
o For a dependency A → B, if for a single value of A, multiple values of B exists,
then the relation will be a multi-valued dependency.

Fifth normal form (5NF)

o A relation is in 5NF if it is in 4NF and not contains any join dependency and
joining should be lossless.
o 5NF is satisfied when all the tables are broken into as many tables as possible
in order to avoid redundancy.
o 5NF is also known as Project-join normal form (PJ/NF).

Normal Description
Form

1NF A relation is in 1NF if it contains an atomic value.

2NF A relation will be in 2NF if it is in 1NF and all non-key attributes are fully
functional dependent on the primary key.

3NF A relation will be in 3NF if it is in 2NF and no transition dependency exists.


4NF A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-
valued dependency.

5NF A relation is in 5NF if it is in 4NF and not contains any join dependency and
joining should be lossless.

You might also like