Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views70 pages

DBS 3rd Module Notes

The document outlines the syllabus for a Database Management System course, focusing on the Relational Data Model, integrity constraints, normalization, and SQL operations. It details various types of constraints, including domain, key, entity integrity, and referential integrity, as well as fundamental operations in relational algebra such as selection, projection, and joins. Additionally, it provides examples and explanations of relational algebra operations and their applications in querying databases.

Uploaded by

km3205421
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)
14 views70 pages

DBS 3rd Module Notes

The document outlines the syllabus for a Database Management System course, focusing on the Relational Data Model, integrity constraints, normalization, and SQL operations. It details various types of constraints, including domain, key, entity integrity, and referential integrity, as well as fundamental operations in relational algebra such as selection, projection, and joins. Additionally, it provides examples and explanations of relational algebra operations and their applications in querying databases.

Uploaded by

km3205421
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/ 70

DATABASE MANAGEMENT SYSTEM (23CA2403)

UNIT-III Syllabus

Relational Data Model: Relation, Integrity constraints-domain, entity and Referential integrity
constraints, Basic Relational Algebra operations, select, project and join operations
(Textbook1:Chapter6: 6.1 to 6.3). Functional dependencies and Normalization for Relational
Databases, Normalization concepts, first, second, third normal forms, Boyce-Codd normal
form.(Text book1:Chapter15:15.1to15.6) SQL: Queries, sub queries, correlated sub query, views,
updating of a database through views, Update, Delete.(Textbook1:Chapter4:4.1to4.5)

Relational Model:

In the Relational Data Model, a "relation" means a table.


This table contains rows and columns.
Each row in the table is called a "tuple."
A tuple holds related pieces of data.
These data pieces represent something real, like a person or an object.
The relational model uses these tables to organize and manage data in a database.

Constraints:

When designing a relational database, we can set rules or restrictions.


These rules control what values can be added, changed, or deleted in a table.
These rules are called constraints.
In older models like the Entity-Relationship model, such rules were not available.
There are three main types of constraints in relational databases:

o Implicit Constraints: Built into the data model automatically.


o Schema-Based (Explicit) Constraints: Written directly into the database using DDL (Data
Definition Language).
o Application-Based (Semantic) Constraints: Handled by the application, not written in the
database schema.

Relational constraints are rules added to the database.


These rules help keep the data correct and safe.
They check if actions like adding or changing data are done properly.
They protect the database from mistakes or damage.
There are four main types of relational constraints:

o Domain Constraints
o Key (Uniqueness) Constraints
o Entity Integrity Constraints
o Referential Integrity Constraints

Ms. Smita VK, Asst. Prof. , DSU Page 1


DATABASE MANAGEMENT SYSTEM (23CA2403)

Domain Constraint:
Domain constraints make sure each column has simple, single values.
-valued data is not allowed.

int, we can only enter whole numbers.


lps keep the data correct and consistent.

EID Name Phone

123456789
01 Bikash Dutta
234456678

Explanation: In the above relation, Name is a composite attribute and Phone is a multi-values attribute,
so it is violating domain constraint.
Key Constraints:
Key constraints make sure each row in a table is unique.
A table can have many candidate keys.
We choose one of the candidate keys as the primary key.
It’s better to choose a key with fewer columns as the primary key.
The primary key cannot have null values.
So, the Not Null rule is also part of the key constraint.

Example:
Ms. Smita VK, Asst. Prof. , DSU Page 2
DATABASE MANAGEMENT SYSTEM (23CA2403)

EID Name Phone

01 Bikash 6000000009

02 Paul 9000090009

01 Tuhin 9234567892

Explanation: In the above table, EID is the primary key, and the first and the last tuple have the same value
in EID ie 01, so it is violating the key constraint.

3. Entity Integrity Constraints


 Entity Integrity constraints say that no primary key can take a NULL value, since using the primary
key we identify each tuple uniquely in a relation.
Example:
EID Name Phone

01 Bikash 9000900099

02 Paul 600000009

NULL Sony 9234567892

Explanation: In the above relation, EID is made the primary key, and the primary key can’t take NULL
values but in the third tuple, the primary key is null, so it is violating Entity Integrity constraints.

4. Referential Integrity Constraints


 The Referential integrity constraint is specified between two relations or tables and used to maintain
the consistency among the tuples in two relations.
 This constraint is enforced through a foreign key, when an attribute in the foreign key of relation R1
has the same domain(s) as the primary key of relation R2, then the foreign key of R1 is said to
reference or refer to the primary key of relation R2.
 The values of the foreign key in a tuple of relation R1 can either take the values of the primary key for
some tuple in relation R2, or can take NULL values, but can’t be empty.
Example:
EID Name DNO

01 Divine 12

02 Dino 22

04 Vivian 14

Ms. Smita VK, Asst. Prof. , DSU Page 3


DATABASE MANAGEMENT SYSTEM (23CA2403)

Explanation: In the above tables, the DNO of Table 1 is the foreign key, and
DNO Place DNO in Table 2 is the primary key. DNO = 22 in the foreign key of Table 1 is
not allowed because DNO = 22 is not defined in the primary key of table 2.
Therefore, Referential integrity constraints are violated here.
12 Jaipur

13 Mumbai

14 Delhi

Introduction to Relational Algebra

Relational Algebra is a formal system for manipulating and querying relational databases. It
provides a set of operations that take one or more relations (tables) as input and produce a new
relation as output. Relational algebra is the theoretical foundation of relational databases and is used
for querying and managing data.

It was introduced by Edgar F. Codd in 1970 as part of his relational model for databases. The main
purpose of relational algebra is to offer a theoretical framework for querying databases, ensuring
that operations on data follow a set of principles that make querying efficient and logical.

Basic Concepts

 Relation: A relation is essentially a table in a database. It consists of a set of tuples (rows)


and attributes (columns). Each relation represents a different entity in the database (e.g.,
employees, departments).
 Tuple: A tuple is a single row in a relation, which contains a set of values corresponding to
the relation’s attributes.
 Attribute: An attribute is a column in a relation, representing a property or characteristic of
the entity represented by the relation.

In relational algebra, operations are used to manipulate relations (tables). These operations can be
classified into two types based on the number of relations they operate on: unary and binary
operations.

1. Unary Operations
Unary operations in relational algebra are operations that operate on only one relation at a time.
These operations are applied to a single relation and produce a new relation as the result.

Some common unary operations in relational algebra include:

a. SELECTION (σ):
o The selection operation is used to filter rows from a relation based on a given
condition.
o It selects a subset of rows that satisfy a specified predicate.
o Syntax: σ <selection condition> (Relation)

Ms. Smita VK, Asst. Prof. , DSU Page 4


DATABASE MANAGEMENT SYSTEM (23CA2403)

where,
 The symbol σ(sigma) is used to denote the SELECT operator
 The selection condition is a Boolean (conditional) expression specified on the attributes of
relation R
 Tuples that make the condition true are selected
 appear in the result of the operation
 Tuples that make the condition false are filtered out
 discarded from the result of the operation

Ms. Smita VK, Asst. Prof. , DSU Page 5


DATABASE MANAGEMENT SYSTEM (23CA2403)

Examples:
1. Select the EMPLOYEE tuples whose department number is 4.
σDNO = 4 (EMPLOYEE)

would to the following SQL query:


SELECT * FROM EMPLOYEE WHERE Dno=4;

2. Select the employee tuples whose salary is greater than $30,000.


σSALARY > 30,000 (EMPLOYEE)

would to the following SQL query:


SELECT * FROM EMPLOYEE WHERE Salary>30000;

3. Select the tuples for all employees who either work in department 4 and make over
$25,000 per year, or work in department 5 and make over $30,000
σ (Dno=4 AND Salary>25000) (EMPLOYEE)

would to the following SQL query:


SELECT * FROM EMPLOYEE WHERE Dno=4 AND Salary>25000;

b. PROJECTION (π):
o The projection operation is used to select specific columns from a relation.
o It removes duplicate rows and keeps only the specified attributes.
o Syntax: π <attribute list> (Relation)

where
 symbol π (pi)used to represent the PROJECT operation
 <attributelist> - desired sublist of attributes from the attributes of relation R.
 The result of the PROJECT operation has only the attributes specified in <attribute
list> in the same order as they appear in the list. Hence, its degree is equal to the
number of attributes in <attribute list>
Example:

 π Name,Age(Employees)
 would to the following SQL query:
SELECT Name, Age FROM Employees;

This means we are selecting the Name and Age columns from the Employees relation
and removing any duplicate rows.

Result of Projection:

Name Age
Alice 30
Bob 25
Charlie 28
David 35

Ms. Smita VK, Asst. Prof. , DSU Page 6


DATABASE MANAGEMENT SYSTEM (23CA2403)

Eve 29

Ms. Smita VK, Asst. Prof. , DSU Page 7


DATABASE MANAGEMENT SYSTEM (23CA2403)

c. RENAMING (ρ):
o The renaming operation is used to rename the attributes of a relation or even the
entire relation.
o This is useful when we need to avoid ambiguity or for clarity in further operations.
o Syntax: ρ_new_relation_name(new_attribute1, new_attribute2)(Relation)

Where:

 Symbol ρ(rho) used to represent the RENAME operation


 new_relation_name: The new name for the relation (table).
 new_attribute1, new_attribute2, ...: The new names for the attributes
(columns) of the relation.
 Relation: The original relation (table) that you want to rename.

Example:

ρ(Emp, Employee_Name, Employee_Age, Employee_Department)(Employees)

would to the following SQL query:


SELECT Name AS Employee_Name,
Age AS Employee_Age,
Department AS Employee_Department
FROM Employees AS Emp;
This operation would rename the Employees relation to Emp, and rename the attributes as
follows:
 Name → Employee_Name
 Age → Employee_Age
 Department → Employee_Department

Original Relation: Employees


Employee_ID Name Age Department
101 Alice 30 HR
102 Bob 25 IT
103 Charlie 28 IT
104 David 35 HR
105 Eve 29 Marketing

Result of SQL Query:

Employee_Name Employee_Age Employee_Department


Alice 30 HR
Bob 25 IT
Charlie 28 IT
David 35 HR
Eve 29 Marketing

Ms. Smita VK, Asst. Prof. , DSU Page 8


DATABASE MANAGEMENT SYSTEM (23CA2403)

2. Binary Operations

Binary operations in relational algebra involve two relations. These operations combine, compare,
or relate two different relations to produce a new relation as the result.

Some common binary operations in relational algebra include:


A. UNION (∪):

 The union operation combines the tuples of two relations, eliminating duplicate
tuples. Both relations must have the same number of attributes and corresponding
attributes must have the same domain.
 Syntax: Relation1 ∪ Relation2

B. INTERSECTION (∩):

 The intersection operation returns the tuples that appear in both relations. Similar to
union, the two relations must have the same number of attributes.
 Syntax: Relation1 ∩ Relation2

C. DIFFERENCE/MINUS (−):

 The difference operation returns the tuples that are in the first relation but not in the
second relation.
 Syntax: Relation1 − Relation2

Example: Consider the following two relations: STUDENT & INSTRUCTOR

Ms. Smita VK, Asst. Prof. , DSU Page 9


DATABASE MANAGEMENT SYSTEM (23CA2403)

Ms. Smita VK, Asst. Prof. , DSU Page 10


DATABASE MANAGEMENT SYSTEM (23CA2403)

D. CARTESIAN PRODUCT (×):

 The Cartesian product operation combines every tuple from the first relation with every
tuple from the second relation. This results in a relation that contains all possible pairs of
tuples from the two relations.
 Syntax: Relation1 × Relation2

Employees: Departments:

Employee_ID Name Age Department Department_ID Department_Name


101 Alice 30 HR 1 HR
2 IT
102 Bob 25 IT

Example: Employees × Departments

Let’s consider Employees and Departments:

Employee_ID Name Age Department Department_ID Department_Name


101 Alice 30 HR 1 HR
101 Alice 30 HR 2 IT
102 Bob 25 IT 1 HR
102 Bob 25 IT 2 IT

E. JOIN (𝔚):

In relational algebra, the join operation combines two relations based on a common attribute
or condition. This operation allows you to merge tuples from different relations in a meaningful
way, creating new relations. There are several types of joins in relational algebra, each serving
different purposes for combining data from multiple relations.

Types of Joins in Relational Algebra

1. Theta Join (𝔚θ)


2. Equi Join (𝔚eq)
3. Natural Join (𝔚)
4. Outer Joins (Left Outer Join, Right Outer Join, Full Outer Join)

Ms. Smita VK, Asst. Prof. , DSU Page 11


DATABASE MANAGEMENT SYSTEM (23CA2403)

1. Theta Join (𝔚θ)

A Theta Join is the most general type of join. It allows you to combine tuples from two relations
based on a condition (θ) that can be any comparison operator, such as =, >, <, !=, etc.

The condition can be any boolean expression that compares attributes from the two relations, not
necessarily equality.

Syntax: Relation1 𝔚θ <join condition>Relation2

Example:

Let’s consider two relations:

Employees: Departments:

Employee_ID Name Age Department_ID Department_ID Min_Age


101 Alice 30 1
1 25
102 Bob 40 2
2 35
103 Charlie 28 1

Relational Algebra: Employees 𝔚 (Employees.Age > Departments.Min_Age) Departments

Employee_ID Name Age Department_ID Min_Age

102 Bob 40 2 35

Ms. Smita VK, Asst. Prof. , DSU Page 12


DATABASE MANAGEMENT SYSTEM (23CA2403)

2. Equi Join (𝔚eq)

An Equi Join is a special case of the Theta Join where the condition is specifically an equality
condition (=) between attributes from both relations.

The condition used in an equi join is always equality (=) between attributes from the two
relations.

Syntax: Relation1 𝔚eq Relation2

Example:

Consider the following relations:

Employees: Departments:

Employee_ID Name Department_ID Department_ID Department_Name


101 Alice 1 1 HR
102 Bob 2
2 IT
103 Charlie 1

Relational Algebra: Employees 𝔚eq Departments

Employees.Department_ID = Departments.Department_ID:

Employee_ID Name Department_ID Department_Nam


e
101 Alice 1 HR

102 Bob 2 IT

103 Charlie 1 HR

Ms. Smita VK, Asst. Prof. , DSU Page 13


DATABASE MANAGEMENT SYSTEM (23CA2403)

3. Natural Join (𝔚)

A Natural Join is a type of Equi Join, but with two key differences:

1. It automatically joins the two relations on all attributes with the same name.
2. It removes duplicate columns that are common between the two relations.

The condition is based on equality of all common attributes between the two relations, and it
automatically eliminates duplicate columns in the result.

Syntax: Relation1 𝔚 Relation2

Example:

Let’s consider the following relations:

Employees: Departments:

Employee_ID Name Department_ID Department_ID Department_Name


101 Alice 1 1 HR
102 Bob 2
2 IT
103 Charlie 1

Relational Algebra: Employees 𝔚 Departments

Employee_ID Name Department_ID Department_Name

101 Alice 1 HR

102 Bob 2 IT

103 Charlie 1 HR

4. Outer Joins

Outer joins are extensions of the join operation that include unmatched tuples from one or both
relations in the result. There are three types of outer joins:

A. Left Outer Join (𝔚L)


B. Right Outer Join (𝔚R)
C. Full Outer Join (𝔚F)

Ms. Smita VK, Asst. Prof. , DSU Page 14


DATABASE MANAGEMENT SYSTEM (23CA2403)

A. Left Outer Join (𝔚L)

A left outer join returns all tuples from the left relation (Relation1), and the matching tuples from
the right relation (Relation2). If there is no match, the result will contain NULL values for
attributes of the right relation.

Syntax: Relation1 𝔚L Relation2


Example: Let’s perform a left outer join on Employees and Departments, assuming that some
employees may not belong to any department.

Employees: Departments:

Employee_ID Name Department_ID Department_ID Department_Name


101 Alice 1
1 HR
102 Bob 2
2 IT
103 Charlie 3

Relational Algebra: Employees 𝔚L Departments


Employee_ID Name Department_ID Department_Name
101 Alice 1 HR
102 Bob 2 IT
103 Charlie 3 NULL

B. Right Outer Join (𝔚R)

A right outer join returns all tuples from the right relation (Relation2), and the matching tuples
from the left relation (Relation1). If there is no match, the result will contain NULL values for
attributes of the left relation.

Syntax: Relation1 𝔚R Relation2

Relational Algebra: Employees 𝔚R Departments


Employee_ID Name Department_ID Department_Name
101 Alice 1 HR
102 Bob 2 IT
NULL NULL 3 IT

Ms. Smita VK, Asst. Prof. , DSU Page 15


DATABASE MANAGEMENT SYSTEM (23CA2403)

C. Full Outer Join (𝔚F)


A full outer join combines the results of both left outer join and right outer join. It returns all
tuples from both relations. If there is no match, the result will contain NULL values for attributes
from the relation that does not have a match.
Syntax: Relation1 𝔚F Relation

Relational Algebra: Employees 𝔚F Departments

Employee_ID Name Department_ID Department_Name


101 Alice 1 HR
102 Bob 2 IT
103 Charlie 3 NULL
NULL NULL 3 IT

Let's combine selection (σ) and projection (π) operations with the binary operations (union,
intersection, difference) in relational algebra to see how these can be applied together. This will
allow us to filter data (selection) and choose specific columns (projection) while combining or
manipulating relations.

1. Selection (σ) and Union (∪)


Selection (σ) is used to filter rows based on a condition, and Union (∪) combines all tuples from
two relations. When combined, you can filter data first, then perform a union operation between two
filtered relations.
Example:Let's assume you have two relations:

Employees: Contractors:

Employee_ID Name Age Department Employee_ID Name Age Department


101 Alice 30 HR 103 Charlie 28 IT
104 David 35 HR
102 Bob 25 IT 105 Eve 29 Marketing
103 Charlie 28 IT
104 David 35 HR

Relational Algebra: σ(Age > 30)(Employees) ∪ σ(Department = 'IT')(Contractors)

Employee_ID Name Age Department

104 David 35 HR

103 Charlie 28 IT

105 Eve 29 Marketing

Ms. Smita VK, Asst. Prof. , DSU Page 16


DATABASE MANAGEMENT SYSTEM (23CA2403)

2. Selection (σ) and Intersection (∩)


The intersection (∩) operation returns tuples that are common in both relations. By combining it
with selection (σ), you can first filter relations, then find the intersection of the filtered results.

Example: Let’s select employees older than 30 and contractors from the HR department, and then
find the common tuples between these two relations.

Relational Algebra: σ(Age > 30)(Employees) ∩ σ(Department = 'HR')(Contractors)

Employee_ID Name Age Department


104 David 35 HR

3. Selection (σ) and Difference (−)

The difference (−) operation returns tuples that are in the first relation but not in the second. By
applying selection first, you can filter the data before performing the difference.

Example: Let’s find employees who are older than 30 but not contractors in the IT department.
Relational Algebra: σ(Age > 30)(Employees) − σ(Department = 'IT')(Contractors)

Employee_ID Name Age Department


104 David 35 HR

Division Operation in Relational Algebra

The Division operation in relational algebra is used to handle queries that involve finding tuples
from one relation that are related to all tuples in another relation. It is a bit more specialized than
other operations like Selection or Union. Division is typically used to solve queries like "find all
employees who work in all departments" or "retrieve all products that have been supplied by every
supplier."

Concept of Division

In simple terms, division is used when you want to retrieve rows from one relation that are associated
with all tuples in another relation. It is applied between two relations: one representing the dividend
(the relation from which you want to select) and the other representing the divisor (the relation
whose tuples must be fully matched with those in the dividend).

The result of a division operation is a new relation that contains all tuples from the dividend relation
that are associated with every tuple in the divisor relation.
Syntax: Dividend ÷ Divisor
Where:
 Dividend is the relation containing the tuples you are interested in.
 Divisor is the relation whose tuples should be matched against all tuples in the dividend.

Ms. Smita VK, Asst. Prof. , DSU Page 17


DATABASE MANAGEMENT SYSTEM (23CA2403)

Steps Involved in Division

1. Projection: First, you project the attributes from the dividend that are not present in the
divisor. These are the attributes you are interested in finding.
2. Difference: Then, you find the tuples in the dividend that have all the corresponding values
in the divisor.

Example of Division
Let's take an example to better understand how division works.

Relation 1 (Employee_Department): This relation contains tuples where each employee is


associated with a department they work in.

Employee_ID Department_ID
1 D1
1 D2
2 D1
2 D2
3 D1

Relation 2 (Departments): This relation contains the departments we are interested in.

Department_ID
D1
D2

Problem: Find all employees who work in all departments (i.e., both D1 and D2).

Solution using Division:


We can perform the Division operation to find the employees who work in both D1 and D2.

Step 1: Project the attributes of the Employee_Department relation that are not in the
Departments relation. Here, the only attribute we care about is Employee_ID.
Step 2: Divide Employee_Department by Departments.

Relational Algebra: Employee_Department ÷ Departments

Employee_ID
1
2

Explanation:

 Employee 1 works in both D1 and D2.


 Employee 2 works in both D1 and D2.
 Employee 3 only works in D1 and is excluded from the result because they do not work in
D2.

Ms. Smita VK, Asst. Prof. , DSU Page 18


DATABASE MANAGEMENT SYSTEM (MMC103)

Functional Dependency in DBMS


Functional Dependency (FD) is a key concept in Database Management Systems (DBMS),
particularly in Normalization. It describes a relationship between attributes in a relation (table),
ensuring data consistency and eliminating redundancy.
Definition

A functional dependency is a constraint between two sets of attributes in a relation. It is


denoted as:

X→Y

This means:

 If two tuples (rows) have the same values for X, they must have the same values for Y.
 X is called the determinant (left-hand side).
 Y is called the dependent (right-hand side).

Example

Consider a relation STUDENT (Roll_No, Name, Course, Age) with the following data:

Roll_No Name Course Age


101 Alex CS 20
102 Bob IT 22
103 Charlie CS 21
101 Alex CS 20

Here, we observe that Roll_No → Name, Course, Age because:

 If two students have the same Roll_No, they must have the same Name, Course, and
Age.

Thus, Roll_No functionally determines Name, Course, and Age.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
Types of Functional Dependencies

1. Trivial Functional Dependency

 A dependency is trivial if the right-hand side (Y) is a subset of the left-hand side (X).
 Example:
o {A,B}→A is trivial because A is already in the left-hand side.

2. Non-Trivial Functional Dependency

 A dependency is non-trivial if Y is not a subset of X.


 Example:
o Roll_No→ Name is non-trivial because Name is not part of Roll_No.

3. Fully Functional Dependency

 A functional dependency X → Y is fully dependent if removing any attribute from X


breaks the dependency.
 Example:
o {Roll_No, Course}→Instructor
o If Instructor depends on both Roll_No and Course, removing Course will break the
dependency.

4. Partial Functional Dependency

 If Y depends on a part of X, not the whole set, it is partial.


 Example:
o {Roll_No, Course}→Instructor, but Instructor depends only on Course, not on
Roll_No.
o This leads to 2NF (Second Normal Form) violation in Normalization.

5. Transitive Functional Dependency

 If X→YX → YX→Y and Y→ZY → ZY→Z, then X→ZX → ZX→Z.


 Example:
o Roll_No→Course and Course→Instructor, then Roll_No→Instructor.
o This leads to 3NF (Third Normal Form) violation.

Importance of Functional Dependency

 Normalization (1NF, 2NF, 3NF, BCNF)


 Eliminating Redundancy
 Ensuring Data Integrity
 Finding Candidate Keys

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

Armstrong’s Axioms/ Inference rules in DBMS


Armstrong’s Axioms are a set of inference rules used to derive all functional dependencies in a
relational database. These axioms were introduced by William W. Armstrong in 1974 and are
sound and complete, meaning they generate only valid functional dependencies and cover all
possible dependencies.

They are essential in database normalization, helping to minimize redundancy and ensure data
consistency.

Armstrong’s Axioms

1. Primary Axioms (Basic Rules)

1. Reflexivity

 If Y is a subset of X, then X → Y (i.e., X functionally determines Y).


 Example:
o If X = {A, B, C} and Y = {A, B}, then {A, B, C} → {A, B} holds true.

2. Augmentation

 If X → Y then X Z → Y Z (adding attributes to both sides does not change dependency).


 Example:
o If A → B, then AC → BC is also valid.

3. Transitivity

 If X → Y and Y → Z, then X → Z (dependency can be transferred through an


intermediate attribute).
 Example:
o If A → B and B → C, then A → C follows.

2. Additional Axioms (Derived Rules)

4. Union (Combination)

 If X → Y and X → Z then X → YZ (dependencies can be combined).


 Example:
o If A → B and A → C, then A → BC.

5. Decomposition (Projection)

 If X → YZ, then X → Y and X → Z (dependencies can be split).


 Example:
o If A → BC, then A → B and A → C are valid.

6. Pseudo transitivity
Ms. Smita VK, Asst. Prof. , DSU
DATABASE MANAGEMENT SYSTEM (MMC103)

 If X → Y and WY → Z, then WX → Z.
 Example:
o If A → B and CB → D, then CA → D.

7. Composition
 If X → Y and A→ B then XA→ YB

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

Finding Candidate Keys in DBMS

What is a Candidate Key?

A Candidate Key is a minimal set of attributes that uniquely identify each tuple (row) in a
relation (table). It is the potential Primary Key, but only one candidate key is chosen as the
Primary Key.

Steps to Find Candidate Keys

1. List all attributes in the relation.


2. Find all Functional Dependencies (FDs).
3. Find the Closure of different attribute sets.
4. Identify the Super Keys (sets of attributes that determine all attributes).
5. Find the Minimal Super Keys (removing unnecessary attributes to get minimal keys).
6. These Minimal Super Keys are Candidate Keys.

Example of Finding Candidate Keys

Given Relation:
R (A, B, C, D, E)

Functional Dependencies (FDs):

1. A→B
2. A→C
3. CD→E
4. E→D

Step 1: Find the Closure of Attributes

The closure of an attribute set X (denoted as X⁺) is the set of attributes functionally determined
by X.

Find A⁺ (Closure of A)

 Given: A→B and A→C, we get A⁺ = {A, B, C}


 A⁺ does not include D or E, so A is not a super key.

Find CD⁺ (Closure of CD)

 CD→E (given)
 E→D (given, so E also determines D)
 CD⁺ = {C, D, E}
 CD⁺ does not include A or B, so CD is not a super key.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

Find ACD⁺ (Closure of ACD)

 ACD+ =A,C,D (start with A, C, and D)


 A→B→ Add B → ACD+=A,B,C,D
 CD→E → Add E → ACD+=A,B,C,D,E
 Since ACD⁺ includes all attributes, ACD is a Super Key.

Step 2: Find Minimal Super Keys (Candidate Keys)

 ACD is a Super Key.


 If we remove any attribute from ACD, it no longer remains a Super Key.
 Hence, ACD is a Candidate Key.

Prime Attribute and Non-Prime Attribute in DBMS

Attribute Type Definition

Prime Attribute An attribute that is part of any candidate key.

Non-Prime An attribute that is not part of any candidate


Attribute key.

Prime and Non-Prime attributes are important in Normalization (2NF, 3NF, BCNF) to eliminate
redundancy and avoid anomalies.

Example to Identify Prime and Non-Prime Attributes

Consider a relation R (A, B, C, D) with the following functional dependencies (FDs):

1. A→B
2. B→C
3. A,D→C

Step 1: Find Candidate Keys

 Find the Closure of A:


o A+={A,B,C} (From A→B and B→C)
o A is not a super key (D is missing).
 Find Closure of (A, D):
o (A,D)+={A,B,C,D} (Includes all attributes → Super Key)
o Candidate Key = {A, D}

Step 2: Identify Prime and Non-Prime Attributes

 Prime Attributes (part of candidate key {A, D}) → A, D


 Non-Prime Attributes (not in the candidate key) → B, C

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

Introduction to Database Normalization

Normalization is a process in database design that organizes data in a way that reduces
redundancy and ensures data integrity. The goal of normalization is to decompose a large, complex
table into smaller, more manageable tables while minimizing data anomalies such as update
anomalies, insert anomalies, and delete anomalies.

In relational databases, normalization helps achieve the following:

1. Reduce Data Redundancy: Avoid storing the same data multiple times.
2. Improve Data Integrity: Maintain consistency and accuracy by organizing data
logically.
3. Simplify Maintenance: Makes it easier to maintain, update, and delete data in a
structured way.

Why is Normalization Important?

Without normalization, databases tend to have redundant data, leading to inconsistency, and
issues like:

 Update Anomalies: Updating one piece of data in one place and forgetting to update it
elsewhere.
 Insert Anomalies: Unable to insert certain data because the table structure does not
allow it.
 Delete Anomalies: Deleting data can lead to loss of other important information.

Example for Anomalies

Suppose we have the following Employee-Department table that contains employee details
along with department information:

Employee_Department Table (Before Normalization):

Eno Ename Dept Dname


101 John D1 HR
102 Alice D2 Finance
103 Bob D1 HR
104 Carol D3 IT
105 David D1 HR

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

1. Update Anomaly

An update anomaly occurs when updating a piece of data in one place, but forgetting to update it
elsewhere. This happens when redundant data is stored in multiple rows of the table, leading to
inconsistency.

Example of Update Anomaly:

Suppose the HR department changes its name to Human Resources. In the original table, there
are multiple employees in the HR department. If we forget to update the department name in one
of the rows, the data becomes inconsistent.

Updating the Department Name (Intended Update):

Let's say we want to update the department name HR to Human Resources.

UPDATE Employee_Department
SET Dname = 'Human Resources'
WHERE Dept = 'D1';

However, imagine we forget to update the row for Bob, and his department name remains HR
while others are updated to Human Resources.

Result after the update:


Eno Ename Dept Dname
101 John D1 Human Resources
102 Alice D2 Finance
103 Bob D1 HR
104 Carol D3 IT
105 David D1 Human Resources

 Inconsistent data: We have HR and Human Resources in the Dname column for the
same department, D1.

This is an update anomaly because updating the department name in some rows but not others
leads to inconsistent data.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

2. Insert Anomaly

An insert anomaly occurs when certain data cannot be inserted into the table due to the way the
table is structured.

Example of Insert Anomaly:

Let’s say we want to add a new department, Sales (with a department code D4), but no employee
currently belongs to that department.

In our original table, we are required to insert a dummy employee to insert the new department,
which is an undesirable behavior.

Trying to Insert a New Department (No Employee):


INSERT INTO Employee_Department (Dept, Dname)
VALUES ('D4', 'Sales');

 Insert fails because the table has a non-null Eno and Ename (employee-related
columns), and we cannot insert a row with null values for these columns.

To Work Around:

We would need to insert a dummy employee like this:

INSERT INTO Employee_Department (Eno, Ename, Dept, Dname)


VALUES (999, 'Dummy', 'D4', 'Sales');

Result (After Insert with Dummy Employee):


Eno Ename Dept Dname
101 John D1 HR
102 Alice D2 Finance
103 Bob D1 HR
104 Carol D3 IT
105 David D1 HR
999 Dummy D4 Sales

 Unnecessary data insertion: A dummy employee (ID 999) was inserted to allow the
Sales department to be recorded.

This is an insert anomaly because we had to insert redundant data (a dummy employee) just to
add a new department. The table structure forces us to insert unnecessary data to maintain the
integrity of the table.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

3. Delete Anomaly

A delete anomaly occurs when deleting data results in the unintentional loss of other important
data. This happens when related data is stored together in the same table.

Example of Delete Anomaly:

Let’s say we want to delete the record for John (employee Eno = 101) who works in the HR
department.

If we delete John's row, we lose not only his information but also the department information
(since it's in the same table).

Deleting John’s Record:


DELETE FROM Employee_Department
WHERE Eno = 101;

Result after deletion:


Eno Ename Dept Dname
102 Alice D2 Finance
103 Bob D1 HR
104 Carol D3 IT
105 David D1 HR

 Lost Department Information: After deleting John, the HR department information (i.e.,
Dept = D1, Dname = HR) is still present for other employees (Bob, David), but we lose
the knowledge that HR exists because we didn’t store it separately.

If Bob and David were also deleted, the HR department would be lost completely, causing a
delete anomaly.

This happens because HR is redundantly stored for every employee in the department. Deleting
one employee (or a few) might unintentionally remove important department information.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

Solutions to Anomalies (Normalization)

To avoid these anomalies, the table should be normalized. Here’s how normalization can solve
these issues:

1. Update Anomaly: By splitting the department information into a separate table, we avoid
having to repeat it for each employee. The department name only needs to be updated in
one place.
2. Insert Anomaly: By separating the department information, we can insert a new
department without needing to create a dummy employee.
3. Delete Anomaly: By separating the department information into its own table, we can
delete employee data without accidentally losing department data.

Normalized Tables Example

After normalization, we can split the data into two tables:

Employee Table:
Eno Ename Dept
101 John D1
102 Alice D2
103 Bob D1
104 Carol D3
105 David D1

Department Table:
Dept Dname
D1 HR
D2 Finance
D3 IT

With this structure:

 We can update the department name in one place.


 We can insert a department without needing to insert an employee.
 We can delete an employee without losing department information.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

1st Normal Form (1NF): Eliminate Repeating Groups

A table is in 1st Normal Form (1NF) if:

 All columns contain atomic (indivisible) values.


 Each column contains only one value for each record (no repeating groups or arrays).
 The order in which data is stored does not matter.

Example:

Consider the following Unnormalized Table for students and their courses:

Student_ID Student_Name Courses


101 John Math, Science
102 Alice History, English
103 Bob Math, Chemistry

In this table, the Courses column contains multiple values (a list of courses), which violates the
1NF rule.

To convert this into 1NF, we split the multiple values into separate rows:

Table in 1NF:
Student_ID Student_Name Course
101 John Math
101 John Science
102 Alice History
102 Alice English
103 Bob Math
103 Bob Chemistry

 Each row has atomic values.


 There are no repeating groups.

This is now in 1st Normal Form (1NF).

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
2nd Normal Form (2NF): Remove Partial Dependencies

A table is in 2nd Normal Form (2NF) if:

 It is in 1st Normal Form.


 It does not have partial dependency (i.e., non-prime attributes should depend on the
whole primary key, not just part of it).

Partial dependency happens when a non-prime attribute is dependent on part of a composite


primary key (a key made up of multiple columns).

Example:

Consider a table storing student course registrations, where Student_ID and Course_Code
together form the primary key:

Student_ID Course_Code Instructor Instructor_Phone


101 M101 Dr. Smith 12345
101 S101 Dr. Brown 67890
102 M101 Dr. Smith 12345
103 S101 Dr. Brown 67890

 Primary Key: (Student_ID, Course_Code)

In this case, Instructor and Instructor_Phone depend only on the Course_Code, not on the
entire primary key. This violates 2NF.

To convert this into 2NF, we split the table into two:

Table 1: Student Course Registration Table 2: Course Instructor Information


Student_ID Course_Code
101 M101 Course_Code Instructor Instructor_Phone
101 S101 M101 Dr. Smith 12345
102 M101 S101 Dr. Brown 67890
103 S101

The Instructor and Instructor_Phone are now dependent only on Course_Code, and not on
Student_ID.

Now, the tables are in 2nd Normal Form (2NF).

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
3rd Normal Form (3NF): Remove Transitive Dependencies

A table is in 3rd Normal Form (3NF) if:

 It is in 2nd Normal Form.


 It does not have transitive dependency (i.e., non-prime attributes should not depend on
other non-prime attributes).

Transitive dependency occurs when a non-prime attribute depends on another non-prime


attribute.

Example:

Consider the following table storing employee information:

Employee_ID Employee_Name Department Dept_Location Dept_Manager


101 John HR New York Mr. Smith
102 Alice Finance London Ms. Johnson
103 Bob HR New York Mr. Smith

 Primary Key: Employee_ID

Here, Dept_Location and Dept_Manager depend on the Department, not directly on the
Employee_ID. Therefore, Dept_Location and Dept_Manager are transitively dependent on
Employee_ID through Department.
To convert this into 3NF, we separate the department information into its own table:
Table 1: Employee Information

Employee_ID Employee_Name Department


101 John HR
102 Alice Finance
103 Bob HR

Table 2: Department Information

Department Dept_Location Dept_Manager


HR New York Mr. Smith
Finance London Ms. Johnson

Now, there are no transitive dependencies, and the tables are in 3rd Normal Form (3NF).
Ms. Smita VK, Asst. Prof. , DSU
DATABASE MANAGEMENT SYSTEM (MMC103)
Boyce-Codd Normal Form (BCNF): Eliminate All Functional Dependencies

A table is in Boyce-Codd Normal Form (BCNF) if:

 It is in 3rd Normal Form.


 For every non-trivial functional dependency, the left side (determinant) is a candidate
key.

In other words, in BCNF, every determinant must be a candidate key.

Example:

Consider the following table with functional dependencies:

Student_ID Course_ID Instructor Room


101 M101 Dr. Smith R1
102 M102 Dr. Brown R2
103 M101 Dr. Smith R1

 Primary Key: (Student_ID, Course_ID)


 Functional Dependencies:
o (Student_ID, Course_ID) → Instructor, Room
o Course_ID → Instructor, Room

In this case, Course_ID determines Instructor and Room, but Course_ID is not a candidate
key (because the primary key is a composite of Student_ID and Course_ID).

To convert this into BCNF, we split the table into two:

Table 1: Student Course Information


Student_ID Course_ID
101 M101
102 M102
103 M101

Table 2: Course Information


Course_ID Instructor Room
M101 Dr. Smith R1
M102 Dr. Brown R2

Now, the Course_ID in Table 2 is a candidate key, and the tables are in BCNF

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

Higher Normal Forms in DBMS

Normalization is a process used in Database Management Systems (DBMS) to reduce


redundancy and eliminate anomalies by structuring tables systematically. Higher normal forms
go beyond 3NF and Boyce-Codd Normal Form (BCNF) to address complex dependencies,
such as multi-valued dependencies (MVDs) and join dependencies (JDs).

Fourth Normal Form (4NF)


Introduction to 4NF

The Fourth Normal Form (4NF) is an advanced level of database normalization that deals with
Multi-Valued Dependencies (MVDs). Even if a table is in BCNF, it may still have redundancy
due to MVDs, which can lead to data anomalies.
Conditions for 4NF

A table is in 4NF if:

1. It is in Boyce-Codd Normal Form (BCNF)


2. It has no multi-valued dependencies (MVDs)

A multi-valued dependency (MVD) occurs when:

 One attribute determines multiple independent values of another attribute, regardless of


other attributes in the table.
 Table should have at least 3 columns
 For the table A,B,C columns, B and C should be independent

MVD Notation:

If A →→ B, then for each A value, there exist multiple independent values of B, regardless of
other attributes.
Example: USN, Name, and Course (Before 4NF)

Consider a university database where students (USN, Name) can enroll in multiple courses. If
stored improperly, this can lead to redundancy.

USN Name Course


U101 Sneha DBMS
U101 Sneha AI
U102 Swati DBMS

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
U102 Swati ML

Checking for Multi-Valued Dependency (MVD)

 A student can enroll in multiple courses → USN →→ Course


 There is no dependency between one course and another, meaning storing them together
cause’s redundancy.

Converting to 4NF

To remove multi-valued dependencies, we separate the information into two normalized


tables:

Decomposed Tables (After 4NF)

Student Table (USN, Name)

USN Name
U101 Sneha
U102 Swati

Student_Course Table (USN, Course)

USN Course
U101 DBMS
U101 AI
U102 DBMS
U102 ML

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

Fifth Normal Form (5NF) in DBMS

Introduction to 5NF (Project-Join Normal Form - PJNF)

The Fifth Normal Form (5NF), also known as Project-Join Normal Form (PJNF), is used to
eliminate redundancy caused by join dependencies (JDs). Even if a table is in 4NF, it may still
have redundancy due to complex relationships between attributes.

Condition for 5NF

A table is in 5NF if:

1. It is in 4NF
2. It has no join dependencies (JDs) that cause redundancy

Join Dependency in DBMS

What is Join Dependency (JD)?

A Join Dependency (JD) occurs when a relation (table) can be decomposed into two or more
smaller relations and reconstructed by joining them without losing any data.

A relation R satisfies a Join Dependency if it can be perfectly reconstructed by joining its


decomposed tables.

Formal Definition of Join Dependency

A relation R(X, Y, Z) has a Join Dependency (JD) if:

 It can be decomposed into R1(X, Y) and R2(Y, Z)


 The original relation R can be obtained by joining R1 and R2 without loss of
information

Notation of Join Dependency:

A Join Dependency is denoted as: R1⋈R2⋈...⋈Rn = R

Where R1, R2, ..., Rn are the decomposed tables of R.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
EXAMPLE:

Step 1: Understanding the Need for 5NF

A relation is in Fifth Normal Form (5NF) if it is decomposed to remove redundancy while


preserving all original information through natural joins—without introducing spurious
tuples (incorrect extra records).

Step 2: Original Table (Before Decomposition)

Let's say we have the following table for students, courses, and instructors:

USN Course Instructor


U101 DBMS Prof. A
U101 AI Prof. B
U101 DBMS Prof. C
U102 DBMS Prof. A
U102 ML Prof. D

Observations:

 A student (USN) can enroll in multiple courses.


 A course can have multiple instructors.
 A student is not directly dependent on an instructor, but on a course-instructor pair.

Join Dependency (JD) exists, meaning we can decompose this table into smaller tables
without losing information.

Step 3: Incorrect Decomposition (Leads to Spurious Tuples)

A wrong decomposition is splitting the table into just two tables:

1. Student_Course Table (USN, Course) 2. Course_Instructor Table (Course, Instructor)

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

USN Course Course Instructor

U101 DBMS DBMS Prof. A

U101 AI DBMS Prof. C

U102 DBMS AI Prof. B

U102 ML ML Prof. D

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

Problem: Spurious Tuples!

When we join Student_Course and Course_Instructor, we get extra incorrect records


(spurious tuples):

USN Course Instructor


U101 DBMS Prof. A
U101 DBMS Prof. C
U101 AI Prof. B
U102 DBMS Prof. A
U102 DBMS Prof. C
U102 ML Prof. D

This incorrectly assigns students to instructors they never had! For example, U102 was
never taught DBMS by Prof. C, but this tuple appears in the incorrect result.

Step 4: Correct Decomposition (Using Three Tables)

To correctly normalize into 5NF and avoid spurious tuples, we need three tables:

1. Student_Course Table (USN, Course)

USN Course
U101 DBMS
U101 AI
U102 DBMS
U102 ML

2. Course_Instructor Table (Course, Instructor)

Course Instructor
DBMS Prof. A
DBMS Prof. C
AI Prof. B
ML Prof. D

3. Student_Course_Instructor Table (USN, Course, Instructor)

USN Course Instructor


U101 DBMS Prof. A
U101 AI Prof. B
U101 DBMS Prof. C
U102 DBMS Prof. A
U102 ML Prof. D

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
This preserves the original relationships correctly!

Step 5: Correct Reconstruction (Without Spurious Tuples)

To rebuild the original table:

1. Join Student_Course_Instructorwith Student_Course

 Since USN and Course match in both tables, the join correctly preserves original records.

USN Course Instructor


U101 DBMS Prof. A
U101 AI Prof. B
U101 DBMS Prof. C
U102 DBMS Prof. A
U102 ML Prof. D

2. Join the result with Course_Instructor

 Since Course and Instructor match in both tables, no extra tuples appear.

Final Correctly Reconstructed Table:

USN Course Instructor


U101 DBMS Prof. A
U101 AI Prof. B
U101 DBMS Prof. C
U102 DBMS Prof. A
U102 ML Prof. D

This exactly matches the original table, confirming the decomposition is correct!

Step 6: Why This Works (Avoiding Spurious Tuples)

1. If we only use Student_Course and Course_Instructor, we lose the USN-Instructor


connection, leading to spurious tuples.

2. By introducing the third table (Student_Course_Instructor), we retain the original three-


way relationship, allowing perfect reconstruction without spurious tuples.

3. 5NF ensures that there are no extra or missing records while eliminating redundancy.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

SQL: Queries, sub queries, correlated sub query, views, updating of a database through views,
Update, Delete.
OVERVIEW OF SQL QUERY LANGUAGE

SQL (Structured Query Language) is the standard language for managing and manipulating
relational databases. It is a declarative language, meaning users specify what data they want, rather
than how to get it. SQL allows users to interact with databases by performing operations such as
data retrieval, insertion, updating, deletion, and database management.

SQL is used for tasks such as:

 Querying databases
 Inserting, updating, and deleting records
 Creating and modifying database structures (tables, schemas)
 Creating views, triggers, and stored procedures

Basic Structure of SQL Queries:


SELECT column1, column2, ...
FROM table_name
WHERE condition ORDER
BY column1 GROUP BY
column2 HAVING
condition;

KEY COMPONENTS:

 SELECT: Specifies the columns to retrieve.


 FROM: this clause is mandatory in every SQL query, as it tells SQL where to look for the
data.
 WHERE: Filters records based on a condition.,Only the rows that satisfy the condition
specified in the WHERE clause will be included in the result set. This clause can use
operators like =, >, <, BETWEEN, LIKE, etc
 ORDER BY: Sorts the result set, You can specify ascending (ASC) or descending (DESC)
order for sorting. By default, sorting is done in ascending order.
 GROUP BY: This clause is used to aggregate data into groups based on the specified
columns.
 HAVING: Filters groups after applying GROUP BY. Typically used with aggregate
functions to filter the result set after the grouping operation.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

Data Types in SQL

SQL supports various data types that define what kind of data a column in a table can store.
Different data types help in maintaining the integrity of data by ensuring that only appropriate
types of values are stored in each column. These data types are typically categorized into numeric
types, character types, date and time types, boolean types, and binary types.

Here’s a detailed overview of the common SQL data types:

1. Numeric Data Types

These types are used to store numbers, both integers and floating-point numbers.

 INT (Integer): Used to store whole numbers (positive, negative, or zero) without decimals.
o Example: INT is typically used for storing employee IDs or age.
o Range: Typically from -2,147,483,648 to 2,147,483,647 (depends on the database
system).
 FLOAT: Used to store approximate numeric values with floating decimal points.
o Example: Used to store scientific data or measurements with decimals, like 3.14 or
-0.006.
o Range: Typically stores up to 7 decimal digits.
 DECIMAL or NUMERIC: Stores exact numeric values with a specified precision and
scale. Unlike FLOAT, it ensures precise calculations.
o Example: Used to store financial data, like monetary values (DECIMAL(10, 2)
means 10 digits in total, with 2 digits after the decimal point).
o Syntax: DECIMAL(precision, scale) where precision is the total number of digits,
and scale is the number of digits to the right of the decimal.
 DOUBLE (Double Precision): Used to store approximate numeric values, with a larger
precision than FLOAT.
o Example: Used for precise scientific calculations that require more decimal places
than FLOAT.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

2. Character Data Types

These types are used to store text data, such as names, descriptions, and other strings.

 CHAR (Fixed Length): Used to store fixed-length strings. If the string is shorter than the
defined length, it will be padded with spaces.
o Example: CHAR(10) will always store 10 characters. If the string is shorter, the
remaining space will be filled with spaces.
o Use Case: Ideal for storing fixed-length values, like state abbreviations (e.g., NY,
CA).
 VARCHAR (Variable Length): Used to store variable-length strings. It stores only the
characters that are actually used, saving space.
o Example: VARCHAR(100) can store a string of any length up to 100 characters.
o Use Case: Used for text fields like names, addresses, and email addresses.
 TEXT: Used to store large amounts of text. Unlike CHAR or VARCHAR, the TEXT type
is used for storing long strings, such as large descriptions or articles.
o Example: Used for storing comments or large textual content.
o Use Case: Ideal for storing long notes, articles, or descriptions that exceed the
limits of VARCHAR.

2. Date and Time Data Types

These types are used to store date, time, or timestamp values.

 DATE: Used to store date values in the format YYYY-MM-DD.


o Example: 2024-12-25 (for Christmas Day).
o Use Case: Used for birth dates, product expiration dates, etc.
 TIME: Used to store time values in the format HH:MM:SS.
o Example: 15:30:00 (3:30 PM).
o Use Case: Used for storing times, such as working hours or event times.
 DATETIME: Used to store both date and time values together in the format YYYY-MM-
DD HH:MM:SS.
o Example: 2024-12-25 15:30:00.
o Use Case: Used when both the date and the time of an event or transaction are
important (e.g., order timestamps).
 TIMESTAMP: Similar to DATETIME, but it also includes information about time zones
and is typically used to record the exact time a record was created or updated.
o Example: 2024-12-25 15:30:00 with the time zone information.
o Use Case: Commonly used for auditing purposes to track when a record was
created or last modified.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

3. Boolean Data Types

These types are used to store binary (true/false) values.

 BOOLEAN: Stores boolean values, typically TRUE or FALSE.


o Example: TRUE (1) or FALSE (0) representing whether a condition is met.
o Use Case: Ideal for flags, such as indicating if a user is active or if an order is
shipped.
 BIT: Another data type that can be used to store boolean values, typically stored as 1
(TRUE) or 0 (FALSE).
o Example: BIT can store values like 0 or 1.
o Use Case: Commonly used in flags or binary decisions.

4. Binary Data Types

These types are used to store binary data, such as images, files, or multimedia.

 BLOB (Binary Large Object): Used to store large binary objects like images, audio files,
or any kind of file data.
o Example: Storing an image file or a PDF document.
o Use Case: Ideal for storing files that need to be retrieved and used within the
application.
 VARBINARY: Similar to BLOB, but with a variable length. Used to store binary data
where the length of the data can vary.
o Example: Used to store encrypted data or file paths in binary format.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

SQL CATEGORIES:

Categories Purpose Key Commands


DDL Defining and modifying database CREATE, ALTER, DROP, TRUNCATE,
structure. RENAME
DML Manipulating data (insert, update, INSERT, SELECT, UPDATE, DELETE
delete, retrieve).
DCL Controlling access to the database GRANT, REVOKE
(permissions).
TCL Managing transactions (ensuring COMMIT, ROLLBACK, SAVEPOINT, SET
data integrity). TRANSACTION
DQL Querying data from the database. SELECT

1. DDL Commands (Data Definition Language)

DDL commands are used to define and manage the structure of the database objects (such as
tables, schemas, and indexes). They do not manipulate the data itself, but rather the schema and its
components.

A. CREATING A DATABASE

The CREATE DATABASE statement is a foundational SQL command used to create new
databases in SQL-based Database Management Systems (DBMS), including MySQL, PostgreSQL,
SQL Server, and others. Understanding how to use this command effectively is crucial for
developers, database administrators, and anyone working with relational databases.

CREATE DATABASE in SQL


The CREATE DATABASE command establishes a new database within your SQL ecosystem.
A database is a repository that organizes data in structured formats through tables, views, stored
procedures, and other components.

There are two main types of databases:

1. Relational Databases (e.g., MySQL, PostgreSQL, SQLite)

2. Non-relational Databases (e.g., MongoDB, Firebase, Cassandra)

Syntax:
CREATE DATABASE database_name;

Example:
CREATE DATABASE College;

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

key points for using the CREATE DATABASE statement in SQL:

1. Naming Conventions: Use unique, descriptive names without spaces (use underscores).
Example: my_database.
2. Max Length: Database names are typically limited to 128 characters.
3. Permissions: You need administrative privileges to create a database.
4. Case Sensitivity: Most SQL systems are case-insensitive, but it’s best to use lowercase
consistently.
5. Cross-DB Compatibility: While the syntax is similar across systems (e.g., MySQL,
PostgreSQL, SQL Server), some systems have specific options (like character set or
storage engine).

CREATE A DATABASE in MongoDB

In MongoDB, you don’t need to explicitly create a database; just use the use command to switch
to a database, and MongoDB will create it when you insert data.

Syntax:
use database_name;
Example:
use College;

List Databases in SQL

We use the SHOW DATABASES command and it will return a list of databases that exist in our
system. Now we will look for the name of the database (GeeksForGeeks) that we have just created.
SQL Query:
SHOW DATABASES;

USE Database in SQL


Once your database is created, we can switch to that database to begin adding tables, inserting data,
and performing queries. To do this, use the USE command.

Select the Database to Use

After creating the database, switch to it:

Syntax: USE database_name;


Example:
use College;

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

CREATE TABLE
SQL Syntax to Create a Table:

CREATE TABLE table_name (


column1 datatype [constraint],
column2 datatype [constraint],
column3 datatype [constraint],
...
);

Example: Create a students table

CREATE TABLE students (


student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
date_of_birth DATE,
);

Steps:

1. Define the table's structure: List the columns with their data types.
2. Specify constraints: Indicate primary keys, unique fields, or not-null constraints.

B. ALTER TABLE
The most common use of ALTER is to modify a table. Here's how you can use it in different
scenarios
ADD A COLUMN
You can add one or more columns to an existing table using the ALTER TABLE statement with
the ADD keyword.

Syntax: ALTER TABLE table_name ADD column_name datatype;

Example: ALTER TABLE students ADD email VARCHAR(100);

 This adds a new column named email with a VARCHAR(100) data type to the students
table.

DROP A COLUMN
You can remove a column from a table using the ALTER TABLE statement with the DROP COLUMN
keyword.

Syntax: ALTER TABLE table_name DROP COLUMN column_name;

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
Example: ALTER TABLE students DROP COLUMN email;
 This removes the email column from the students table.

MODIFY A COLUMN (CHANGE DATA TYPE OR SIZE)

You can change the data type or size of an existing column using the ALTER TABLE statement with
the MODIFY or CHANGE keyword (depending on the DBMS).

Syntax (MySQL): ALTER TABLE table_name MODIFY column_name new_datatype;

Example: ALTER TABLE students MODIFY age INT;

 This changes the age column data type to INT (if it was something else previously).

Syntax (SQL SERVER OR POSTGRESQL):

ALTER TABLE table_name ALTER COLUMN column_name SET DATA TYPE new_datatype;

Example (POSTGRESQL):

ALTER TABLE students ALTER COLUMN age TYPE INT;

RENAME A COLUMN

You can rename an existing column using the ALTER TABLE statement with the RENAME COLUMN
keyword.

Syntax (PostgreSQL):

ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;

Example:

ALTER TABLE students RENAME COLUMN email TO contact_email;

RENAME A TABLE

You can rename a table using the ALTER TABLE statement with the RENAME TO keyword.

Syntax: ALTER TABLE old_table_name RENAME TO new_table_name;

Example: ALTER TABLE students RENAME TO student_info;

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

C. DELETE/DROP A DATABASE IN SQL


If you ever need to remove a database, the DROP DATABASE command can be used to delete
the database and all its contents:

Syntax: Drop database database_name

Important Considerations:

 Irreversible: Dropping a database will remove all data and structures associated with it, so
ensure that you have a backup if necessary.
 Permissions: You need administrative privileges to drop a database.

Before Dropping a Database:

 Make sure that no active connections are using the database, as some systems may prevent
dropping a database if it is in use.

Drop table

To drop a table in SQL, you use the DROP TABLE statement. This command permanently
deletes the table along with all the data and structure it contains.

SQL Syntax to Drop a Table:

DROP TABLE table_name;

Example: Drop a Table

DROP TABLE students;

Important Considerations:

 Irreversible: Dropping a table will permanently remove the table and all its data. Make
sure to back up the data if needed.
 Permissions: You need appropriate privileges to drop a table.
 Dependencies: If there are foreign keys or other dependencies (e.g., views, triggers), you
may need to drop them first or adjust them before dropping the table.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

D. TRUNCATE IN SQL

The TRUNCATE command in SQL is used to remove all rows from a table quickly, while keeping
the table structure intact. This means that the table itself remains available for use, but all the data
within it is deleted.

Syntax for TRUNCATE: TRUNCATE TABLE table_name;

Example:If we have a table students and we want to remove all records from it, we would use:

TRUNCATE TABLE students;

2. DML COMMANDS

DML (Data Manipulation Language) commands in SQL are used to manipulate and manage data
within existing database tables. These commands are focused on inserting, updating, deleting, and
querying the data. DML operations are typically non-structural and affect only the data in the table,
not the schema or the table structure itself.

Key DML Commands:

1. INSERT
2. SELECT
3. UPDATE
4. DELETE

1. INSERT

The INSERT command is used to add new rows of data into a table.

Syntax:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

Example:

INSERT INTO students (student_id, first_name, last_name, age, email)


VALUES (1, 'Alice', 'Johnson', 22, '[email protected]');

 This adds a new row to the students table with the provided values.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

2. SELECT

The SELECT command is used to retrieve data from one or more tables. You can use different
clauses like WHERE, ORDER BY, GROUP BY, etc., to filter, sort, or group the results.

Syntax:

SELECT column1, column2, column3, ...


FROM table_name
WHERE condition;

Example:

SELECT first_name, last_name, age FROM students WHERE age > 21;

 This retrieves the first_name, last_name, and age of students whose age is greater than
21.

3. UPDATE

The UPDATE command is used to modify the existing data in a table. It allows you to change values
in one or more columns for specific rows.

Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Example:

UPDATE students
SET age = 23
WHERE student_id = 1;

 This updates the age of the student with student_id = 1 to 23.

Note: Always use the WHERE clause to specify which records should be updated. Without it, all
rows in the table will be updated.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

4. DELETE

The DELETE command is used to remove rows from a table. You can use the WHERE clause to
specify which rows to delete.

Syntax:

DELETE FROM table_name


WHERE condition;

Example:

DELETE FROM students WHERE student_id = 1;

 This deletes the row where student_id = 1.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

DML Command Examples with students Table:

1. INSERT: To add a new student:

INSERT INTO students (student_id, first_name, last_name, age, email)


VALUES (4, 'David', 'Brown', 24, '[email protected]');

student_id first_name last_name age email


1 Alice Johnson 22 [email protected]
2 Bob Smith 23 [email protected]
3 Carol Lee 21 [email protected]
4 David Brown 24 [email protected]

2. SELECT: To get the names and ages of students who are 22 or older:

SELECT first_name, last_name, age FROM students WHERE age >= 22;

first_name last_name age


Alice Johnson 22
Bob Smith 23
David Brown 24

3. UPDATE: To update the age of the student with student_id = 3 to 22:

UPDATE students
SET age = 22
WHERE student_id = 3;

student_id first_name last_name age email


1 Alice Johnson 22 [email protected]
2 Bob Smith 23 [email protected]
3 Carol Lee 22 [email protected]
4 David Brown 24 [email protected]

4. DELETE: To delete the student with student_id = 2:

DELETE FROM students WHERE student_id = 2;

student_id first_name last_name age email


1 Alice Johnson 22 [email protected]
3 Carol Lee 22 [email protected]
4 David Brown 24 [email protected]

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

CLAUSES IN SQL
In SQL, clauses are the components of a query that define its structure and functionality. They
specify the conditions, filters, sorting, grouping, and relationships between data. Here are the main
SQL clauses

1. SELECT Clause

Used to retrieve specific columns from a table.

SELECT column1, column2 FROM table_name;

2. FROM Clause

Specifies the table from which to retrieve the data.

SELECT * FROM employees;

3. WHERE Clause

The WHERE clause in SQL filters rows based on conditions. It supports comparison operators (=, >,
<, etc.), logical operators (AND, OR, NOT), and special operators like IN, BETWEEN, LIKE, and IS
NULL for advanced filtering.

Example:
SELECT *
FROM employees
WHERE salary > 50000;

 Output: Retrieves employees whose salary is greater than 50,000.


4. GROUP BY Clause

The GROUP BY clause in SQL is used to group rows that have the same values in specified columns.
It is often used with aggregate functions like COUNT(), SUM(), AVG(), MAX(), and MIN() to
summarize data within each group.
The GROUP BY clause must be used after the WHERE clause and before the ORDER BY clause. It
can be combined with the HAVING clause to filter grouped data based on conditions. Only columns
listed in the GROUP BY clause or used in aggregate functions can appear in the SELECT statement.
Syntax

SELECT column1, aggregate_function(column2)


FROM table_name
GROUP BY column1;

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

Example 1: Count Employees in Each Department

SELECT department, COUNT(*) AS employee_count


FROM employees
GROUP BY department;

 Output: Groups employees by department and counts the number of employees in each.

Example 2: Average Salary by Department

SELECT department, AVG(salary) AS avg_salary


FROM employees
GROUP BY department;

 Output: Groups rows by department and calculates the average salary for each.

5. HAVING Clause

The HAVING clause in SQL is used to filter grouped data after applying the GROUP BY clause. Unlike
the WHERE clause, which filters rows before grouping, the HAVING clause filters groups after
aggregation. It is often used with aggregate functions like COUNT(), SUM(), and AVG() to apply
conditions to grouped results.

Syntax

SELECT column1, aggregate_function(column2)


FROM table_name
GROUP BY column1
HAVING condition;

Example 1: Departments with More Than 5 Employees

SELECT department, COUNT(*) AS employee_count


FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

 Output: Displays departments with more than 5 employees.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

6. ORDER BY Clause

The ORDER BY clause in SQL is used to sort the result set in either ascending (ASC) or
descending (DESC) order. By default, the sorting is in ascending order. It can be applied to one
or more columns to arrange the data in a specific sequence.

Syntax
SELECT column1, column2
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC];

Example 1: Sorting by a Single Column

SELECT *
FROM employees
ORDER BY salary DESC;

 Output: Sorts employees by salary in descending order.

Example 2: Sorting by Multiple Columns

SELECT *
FROM employees
ORDER BY department ASC, salary DESC;

 Output: Sorts employees first by department in ascending order, then by salary in


descending order.

SET OPERATIONS

Set operations in SQL are used to combine the results of two or more SELECT queries. They allow
you to perform operations like union, intersection, and difference on result sets. The most common
set operations in SQL are:

1. UNION
2. UNION ALL
3. INTERSECT
4. EXCEPT (or MINUS in some databases)

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
Example Tables

Consider the following two tables:

Employees table Customers table

EmployeeID EmployeeName Department CustomerID CustomerName Country


1 Alice HR 1 Alice USA
2 Bob IT 2 John Canada
3 Charlie Marketing 3 Charlie UK
4 David IT 4 Helen Australia
5 Eve HR 5 Eve USA

1. UNION

The UNION operator combines the results of two SELECT queries and removes duplicates. The
columns must have the same names and data types in both queries.

Example: Using UNION


We want to combine the names of employees and customers. If there are any duplicates (e.g.,
Alice and Charlie), they will be removed.

SELECT EmployeeName Name


FROM Employees Alice
UNION Bob
Charlie
SELECT CustomerName
David
FROM Customers;
Eve
John
Helen

 Alice, Charlie, and Eve appear in both tables, but only one instance of each is included in
the result.

2. UNION ALL

The UNION ALL operator combines the result sets of two queries but does not remove duplicates.

Example: Using UNION ALL


This will return all names from both tables, including duplicates.

SELECT EmployeeName
FROM Employees
UNION ALL

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
SELECT CustomerName
FROM Customers;
Name
Alice
Bob
Charlie
David
Eve
Alice
John
Charlie
Helen  Notice that Alice, Charlie, and Eve appear twice, as UNION ALL
Eve doesn't eliminate duplicates.

3. UNION

The UNION operator combines the results of two SELECT queries and removes duplicates. The
columns must have the same names and data types in both queries.

Example: Using UNION


We want to combine the names of employees and customers. If there are any duplicates (e.g.,
Alice and Charlie), they will be removed.

SELECT EmployeeName Name


FROM Employees Alice
UNION Bob
Charlie
SELECT CustomerName
David
FROM Customers;
Eve
John
Helen

 Alice, Charlie, and Eve appear in both tables, but only one instance of each is included in
the result.

4. UNION ALL

The UNION ALL operator combines the result sets of two queries but does not remove duplicates.

Example: Using UNION ALL


This will return all names from both tables, including duplicates.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
SELECT EmployeeName
Name
FROM Employees
Alice
UNION ALL
Bob
SELECT CustomerName Charlie
FROM Customers; David
Eve
Alice
John
Charlie
Helen
Eve

 Notice that Alice, Charlie, and Eve appear twice, as UNION ALL doesn't eliminate
duplicates.

5. INTERSECT

The INTERSECT operator returns only the rows that are present in both result sets.

Example: Using INTERSECT

We want to find the common names between employees and customers.

SELECT EmployeeName Name


FROM Employees
Alice
INTERSECT
Charlie
SELECT CustomerName Eve
FROM Customers;

 Only Alice, Charlie, and Eve appear in both the Employees and Customers tables, so
they are returned.

6. EXCEPT (or MINUS in some databases)

The EXCEPT operator returns the rows from the first query that do not exist in the second query. It
removes any common rows.

Example: Using EXCEPT

We want to find employees who are not customers.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
SELECT EmployeeName Name
FROM Employees Bob
EXCEPT
David
SELECT CustomerName
FROM Customers;

Only Bob and David appear in the Employees table but not in the Customers table, so they
are returned.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

AGGREGATE FUNCTIONS
Aggregate functions in SQL are used to perform calculations on a set of values and return a single
value. These functions are commonly used with the GROUP BY clause to summarize or group data,
but they can also be used without grouping.

Common Aggregate Functions in SQL:

1. COUNT()
2. SUM()
3. AVG()
4. MIN()
5. MAX()

Let’s go through each function with examples.

1. COUNT(): The COUNT() function returns the number of rows that match a specified condition
or the total number of rows in a column.

Syntax:

COUNT(column_name)

Example:
SaleID Product Amount
1 A 100
2 B 150
3 A 200
4 C 120
5 A 180

To find out how many sales transactions occurred:


TotalSales
SELECT COUNT(*) AS TotalSales
FROM Sales; 5

 COUNT(*) counts all rows, including rows with NULL values.

If you want to count how many sales of product "A" there were:

SELECT COUNT(*) AS ProductACount ProductACount


FROM Sales
3
WHERE Product = 'A';

 COUNT() ignores NULL values in columns.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

2. SUM(): The SUM() function returns the total sum of a numeric column.

Syntax:
SUM(column_name)

Example:
To find the total amount of sales in the Sales table:
TotalAmount
SELECT SUM(Amount) AS TotalAmount
FROM Sales; 750

 SUM() only works on numeric columns and ignores NULL values.

3. AVG(): The AVG() function returns the average value of a numeric column.

Syntax:
AVG(column_name)

Example:
To find the average sales amount:

SELECT AVG(Amount) AS AverageAmount AverageAmount


FROM Sales;
150

 AVG() calculates the average of non-NULL values.

4. MIN(): The MIN() function returns the smallest value in a column.

Syntax:
MIN(column_name)

Example:
To find the minimum sales amount:

SELECT MIN(Amount) AS MinAmount MinAmount


FROM Sales; 100

 MIN() works on numeric, date, or text columns and returns the smallest value.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

5. MAX(): The MAX() function returns the largest value in a column.

Syntax:
MAX(column_name)

Example:
To find the maximum sales amount:
MaxAmount
SELECT MAX(Amount) AS MaxAmount 200
FROM Sales;

 MAX() works similarly to MIN(), returning the largest value.

TRIGGERS
A trigger in SQL is a special kind of stored procedure that automatically executes (or fires) in
response to certain events on a table or view. Triggers can be set to run before or after an INSERT,
UPDATE, or DELETE operation is performed on a table.

Trigger Types:

1. AFTER INSERT activated after data is inserted into the table.

2. AFTER UPDATE: activated after data in the table is modified.

3. AFTER DELETE: activated after data is deleted/removed from the table.

4. BEFORE INSERT: activated before data is inserted into the table.

5. BEFORE UPDATE: activated before data in the table is modified.

6. BEFORE DELETE: activated before data is deleted/removed from the table.

Syntax for Creating a Trigger

CREATE TRIGGER trigger_name


{BEFORE | AFTER | INSTEAD OF} {INSERT | UPDATE | DELETE}
ON table_name
FOR EACH ROW
BEGIN
-- Trigger logic here
END;

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

EXAMPLE

SELECT *FROM constituency;

Create a TRIGGER to UPDATE the count of “ Number_of_voters” of the respective


constituency

DELIMITER //
Create trigger T
after insert on voter
for each row
begin
update constituency set no_of_voters=no_of_voters+1 where
cons_id=new.cons_id;
end
//
insert into voter values(300,'suresh',25,'chikodi',111,121);
//
select *from constituency;
//

VIEWS
A view in SQL is a virtual table that provides a way to simplify complex queries, encapsulate logic,
and re-use SQL code. It doesn't store data itself but displays data from one or more tables or other
views. Views are commonly used to simplify complex queries, aggregate data, or hide specific data
from the end user.

Basic Concepts of Views:

 Virtual Table: A view behaves like a table but doesn't physically store data. Instead, it
Ms. Smita VK, Asst. Prof. , DSU
DATABASE MANAGEMENT SYSTEM (MMC103)
stores a query that retrieves data when accessed.
 Simplifying Queries: Views can simplify complex queries, so users don't need to write the
same SQL repeatedly.
 Security: Views can be used to restrict access to specific columns or rows of a table,
providing an abstraction layer.

Creating a View

Syntax:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example 1: Simple View
Suppose we have the following Employees table:

EmployeeID Name Department Salary


1 Alice HR 50000
2 Bob IT 60000
3 Charlie Marketing 45000
4 David IT 65000
5 Eve HR 55000

To create a view that only shows the names and salaries of employees:

CREATE VIEW EmployeeSalaries AS


SELECT Name, Salary
FROM Employees;

This creates a view EmployeeSalaries which can be queried like a table:

SELECT * FROM EmployeeSalaries;

Name Salary
Alice 50000
Bob 60000
Charlie 45000
David 65000
Eve 55000

Example 2: View with Filtering

You can create views that include filters. For example, let's create a view for employees with a
salary greater than 55,000.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)
CREATE VIEW HighEarners AS
SELECT Name, Salary
FROM Employees
WHERE Salary > 55000;

Now, querying the HighEarners view will only return employees with a salary above 55,000:

SELECT * FROM HighEarners; Name Salary


Bob 60000
David 65000

Dropping a View

If you no longer need a view, you can remove it using the DROP VIEW statement:

DROP VIEW EmployeeSalaries;

Limitations of Views

1. Read-Only Views: Some views are read-only if they involve certain operations (such as
joins or aggregations). You cannot update or insert data into these views directly.
2. Performance: Since views are virtual and executed at runtime, they may impact
performance when dealing with complex queries.
3. No Indexing: Views do not have indexes, although indexes on underlying tables can still
affect performance.
4. Not Persistent Data: Views do not store data themselves; they are always generated on the
fly when queried.

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

NESTED SUB QUERIES


A nested subquery (or simply subquery) is a query within another query. The subquery is typically
used to return a value that is then used by the outer query to filter results or perform other operations.
Subqueries can be nested in various places, such as in the SELECT, WHERE, FROM, and HAVING
clauses of an SQL query.

Important Rule:
 A subquery can be placed in a number of SQL clauses like WHERE clause, FROM clause,
HAVING clause.
 You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along with
the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
 A subquery is a query within another query. The outer query is known as the main query,
and the inner query is known as a subquery.
 Subqueries are on the right side of the comparison operator.
 A subquery is enclosed in parentheses.
 In the Subquery, ORDER BY command cannot be used. But GROUP BY command can be
used to perform the same function as ORDER BY command.

1. SUBQUERIES WITH THE SELECT STATEMENT

SQL subqueries are most frequently used with the Select statement.
Syntax
SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );

Example
Consider the EMPLOYEE table have the following records:

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

6 Harry 42 China 4500.00

7 Jackson 25 Mizoram 10000.00

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

The subquery with a SELECT statement will be:

SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
WHERE SALARY > 4500);
This would produce the following result:

ID NAME AGE ADDRESS SALARY

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

7 Jackson 25 Mizoram 10000.00

2. SUBQUERIES WITH THE INSERT STATEMENT

 SQL subquery can also be used with the Insert statement. In the insert statement, data returned
from the subquery is used to insert into another table.
 In the subquery, the selected data can be modified with any of the character, date functions.

Syntax:

INSERT INTO table_name (column1, column2, column3. .. )


SELECT *
FROM table_name
WHERE VALUE OPERATOR

Example
Consider a table EMPLOYEE_BKP with similar as EMPLOYEE.

Now use the following syntax to copy the complete EMPLOYEE table into the EMPLOYEE_BKP
table.

INSERT INTO EMPLOYEE_BKP


SELECT * FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE);

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

3. SUBQUERIES WITH THE UPDATE STATEMENT

The subquery of SQL can be used in conjunction with the Update statement. When a subquery is used
with the Update statement, then either single or multiple columns in a table can be updated.

Syntax

UPDATE table
SET column_name = new_value
WHERE VALUE OPERATOR
(SELECT COLUMN_NAME
FROM TABLE_NAME
WHERE condition);
Example

Let's assume we have an EMPLOYEE_BKP table available which is backup of EMPLOYEE table.
The given example updates the SALARY by .25 times in the EMPLOYEE table for all employee
whose AGE is greater than or equal to 29.

UPDATE EMPLOYEE
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 29);

This would impact three rows, and finally, the EMPLOYEE table would have the following records.

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

4 Alina 29 UK 1625.00

5 Kathrin 34 Bangalore 2125.00

6 Harry 42 China 1125.00

7 Jackson 25 Mizoram 10000.00

Ms. Smita VK, Asst. Prof. , DSU


DATABASE MANAGEMENT SYSTEM (MMC103)

4. SUBQUERIES WITH THE DELETE STATEMENT

The subquery of SQL can be used in conjunction with the Delete statement just like any other
statements mentioned above.
Syntax

DELETE FROM TABLE_NAME


WHERE VALUE OPERATOR
(SELECT
COLUMN_NAME FROM
TABLE_NAME
WHERE condition);
Example
Let's assume we have an EMPLOYEE_BKP table available which is backup of EMPLOYEE
table. The given example deletes the records from the EMPLOYEE table for all EMPLOYEE
whose AGE is greater than or equal to 29

DELETE FROM EMPLOYEE


WHERE AGE IN (SELECT AGE FROM EMPLOYEE_BKP
WHERE AGE >= 29 );

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

7 Jackson 25 Mizoram 10000.00

Ms. Smita VK, Asst. Prof. , DSU

You might also like