Aim
Introduction to SQL
|
Category
|
Purpose
|
|
DDL
|
Defines database structure
|
|
DML
|
Manipulates data
|
|
DCL
|
Controls permissions
|
|
TCL
|
Manages transactions
|
|
DQL
|
Retrieves data
|
What is DDL?
-
Database
-
Tables
-
Views
-
Indexes
-
Constraints
|
Command
|
Purpose
|
|
CREATE
|
Creates new objects
|
|
ALTER
|
Modifies existing objects
|
|
DROP
|
Deletes objects
|
|
TRUNCATE
|
Removes all rows
|
|
RENAME
|
Renames objects
|
Database Schema
-
Tables
-
Columns
-
Data Types
-
Constraints
-
Relationships
DDL Command 1
CREATE DATABASE
Syntax
|
Database created successfully.
|
|
Database changed.
|
CREATE TABLE
Syntax
|
Data Type
|
Description
|
|
INT
|
Integer values
|
|
VARCHAR(n)
|
Variable length
characters
|
|
CHAR(n)
|
Fixed length
characters
|
|
DATE
|
Stores date
|
|
FLOAT
|
Decimal values
|
|
BOOLEAN
|
True/False
|
Constraints
|
Constraint
|
Description
|
|
PRIMARY
KEY
|
Unique identifier
|
|
FOREIGN
KEY
|
References another table
|
|
UNIQUE
|
No duplicate values
|
|
NOT NULL
|
Cannot contain NULL
|
|
CHECK
|
Validates condition
|
|
DEFAULT
|
Assigns default value
|
Example 1
Create Department Table
|
Table created successfully.
|
Explanation
-
DeptID uniquely identifies departments.
-
PRIMARY KEY prevents duplicate IDs.
-
DeptName cannot be NULL.
-
UNIQUE prevents duplicate department names.
Example 2
Create Student Table
|
Table created successfully.
|
Explanation
-
Primary Key
-
Foreign Key
-
Unique constraint
-
NOT NULL constraint
Viewing Table Structure
|
Field
|
Type
|
Null
|
Key
|
|
StudentID
|
int
|
NO
|
PRI
|
|
StudentName
|
varchar(40)
|
NO
|
|
|
Age
|
int
|
YES
|
|
|
Email
|
varchar(50)
|
YES
|
UNI
|
|
DeptID
|
int
|
YES
|
MUL
|
Example 3
Create Faculty Table
|
Table created successfully.
|
ALTER TABLE:
Used to modify an existing table.
|
Query OK
|
Table Structure
Modify Column
|
Query OK
|
Rename Column
|
Query OK
|
|
Query OK
|
|
Query OK
|
|
Deletes all records.
|
|
Table truncated.
|
DROP TABLE
|
Table dropped.
|
DROP DATABASE
|
Database dropped.
|
-
2.Write SQL queries to perform basic DML operations(INSERT, UPDATE, DELETE).
1.Aim
-
INSERT
-
UPDATE
-
DELETE
2. Objective
-
1.Insert new records into a database table.
-
2.Update existing records in a table.
-
3.Delete records from a table.
-
4.Verify the changes using the SELECT command.
-
5.Understand the importance of the WHERE clause in UPDATE and DELETEoperations.
3. Introduction to DML
|
Command
|
Purpose
|
|---|---|
|
INSERT
|
Adds new records to a table
|
|
UPDATE
|
Modifies existing records
|
|
DELETE
|
Removes records from a table
|
|
SELECT
|
Retrieves data from a table
|
4. Database Table Used
Table Structure
|
Column Name
|
Data Type
|
Description
|
|---|---|---|
|
StudentID
|
INT
|
Unique ID of the student
|
|
StudentName
|
VARCHAR(50)
|
Name of the student
|
|
Department
|
VARCHAR(30)
|
Department name
|
|
Age
|
INT
|
Age of the student
|
|
Marks
|
INT
|
Marks obtained
|
5. Create the Student Table
SQL Query
Explanation
-
CREATE TABLE is used to create a new table.
-
StudentID is declared as the PRIMARY KEY.
-
VARCHAR(50) stores character data up to 50 characters.
-
INT stores integer values.
-
Output
6. INSERT Operation
Syntax
Example 1: Insert a Single Record
SQL Query
Explanation
Table After INSERT
|
StudentID
|
StudentName
|
Department
|
Age
|
Marks
|
|---|---|---|---|---|
|
101
|
Ananya
|
CSE
|
20
|
85
|
Example 2: Insert Multiple Records
|
StudentID
|
StudentName
|
Department
|
Age
|
Marks
|
|---|---|---|---|---|
|
101
|
Ananya
|
CSE
|
20
|
85
|
|
102
|
Rahul
|
ISE
|
21
|
78
|
|
103
|
Priya
|
ECE
|
20
|
88
|
|
104
|
Kiran
|
CSE
|
22
|
91
|
7. UPDATE Operation
Syntax
Example 1: Update Marks of a Student
SQL Query
|
StudentID
|
StudentName
|
Department
|
Age
|
Marks
|
|---|---|---|---|---|
|
102
|
Rahul
|
ISE
|
21
|
78
|
|
StudentID
|
StudentName
|
Department
|
Age
|
Marks
|
|---|---|---|---|---|
|
102
|
Rahul
|
ISE
|
21
|
90
|
|
StudentID
|
StudentName
|
Department
|
Age
|
Marks
|
|---|---|---|---|---|
|
103
|
Priya
|
CSE
|
20
|
95
|
Explanation
Output
8. DELETE Operation
Syntax
Example 1: Delete a Specific Record
SQL Query
|
StudentID
|
StudentName
|
Department
|
Age
|
Marks
|
|---|---|---|---|---|
|
104
|
Kiran
|
CSE
|
22
|
91
|
After DELETE
Explanation
Output
9. Display the Table Contents
|
StudentID
|
StudentName
|
Department
|
Age
|
Marks
|
|---|---|---|---|---|
|
101
|
Ananya
|
CSE
|
20
|
90
|
|
102
|
Rahul
|
ISE
|
21
|
90
|
|
103
|
Priya
|
CSE
|
20
|
100
|
-
PRIMARY KEY
-
FOREIGN KEY
-
UNIQUE
-
NOT NULL
-
INSERT
-
UPDATE
-
DELETE
Objectives
-
1.Create tables using SQL constraints.
-
2.Understand the importance of data integrity.
-
3.Insert valid data into related tables.
-
4.Update existing records.
-
5.Delete records while maintaining referential integrity.
-
6.Understand the relationship between a parent table and a child table.
Introduction
What are Constraints?
-
A student must have a unique ID.
-
A student name cannot be empty.
-
An email address should not be duplicated.
-
A student department should exist in the Department table.
Types of Constraints Used
PRIMARY KEY
Properties
-
Cannot contain NULL values.
-
Must contain unique values.
-
A table can have only one primary key constraint.
Example
FOREIGN KEY
Example
UNIQUE
NOT NULL
Database Schema
Parent Table
Create the Department Table
SQL Query
|
DeptID
|
DeptName
|
|---|---|
|
1
|
Computer Science
|
|
2
|
Information Science
|
|
3
|
Electronics and Communication
|
Explanation
StudentID INT PRIMARY KEY
StudentName VARCHAR(50) NOT NULL
Email VARCHAR(100) UNIQUE
FOREIGN KEY (DeptID)
INSERT Operation
Syntax
Example 1: Insert a Valid Student Record
|
StudentID
|
StudentName
|
Email
|
DeptID
|
|---|---|---|---|
|
101
|
Ananya
|
ananya@gmail.com
|
1
|
|
102
|
Rahul
|
rahul@gmail.com
|
2
|
|
103
|
Priya
|
priya@gmail.com
|
1
|
|
104
|
Kiran
|
kiran@gmail.com
|
3
|
Testing Constraints
Testing PRIMARY KEY Constraint
Query
Testing UNIQUE Constraint
Query
Explanation
Testing NOT NULL Constraint
Query
Testing FOREIGN KEY Constraint
Query
UPDATE Operation
Syntax
|
StudentID
|
StudentName
|
Email
|
|---|---|---|
|
102
|
Rahul
|
rahul@gmail.com
|
|
StudentID
|
StudentName
|
Email
|
|---|---|---|
|
102
|
Rahul
|
rahul_updated@gmail.com
|
Invalid UPDATE Example
DELETE Operation
Syntax
|
StudentID
|
StudentName
|
Email
|
DeptID
|
|---|---|---|---|
|
101
|
Ananya
|
ananya@gmail.com
|
1
|
|
102
|
Rahul
|
rahul_updated@gmail.com
|
2
|
|
103
|
Priya
|
priya@gmail.com
|
1
|
|
104
|
Kiran
|
kiran@gmail.com
|
3
|
|
StudentID
|
StudentName
|
Email
|
DeptID
|
|---|---|---|---|
|
101
|
Ananya
|
ananya@gmail.com
|
1
|
|
102
|
Rahul
|
rahul_updated@gmail.com
|
2
|
|
103
|
Priya
|
priya@gmail.com
|
1
|
Referential Integrity and DELETE
Complete SQL Program
|
DeptID
|
DeptName
|
|---|---|
|
1
|
Computer Science
|
|
2
|
Information Science
|
|
3
|
Electronics and Communication
|
|
StudentID
|
StudentName
|
Email
|
DeptID
|
|---|---|---|---|
|
101
|
Ananya
|
ananya@gmail.com
|
1
|
|
102
|
Rahul
|
rahul_updated@gmail.com
|
2
|
|
103
|
Priya
|
priya@gmail.com
|
3
|
|
SQL Command
|
Purpose
|
|---|---|
|
CREATE TABLE
|
Creates a new table
|
|
PRIMARY KEY
|
Uniquely identifies records
|
|
FOREIGN KEY
|
Establishes a relationship between tables
|
|
UNIQUE
|
Prevents duplicate values
|
|
NOT NULL
|
Prevents empty values
|
|
SQL Command
|
Purpose
|
|---|---|
|
INSERT
|
Adds new records
|
|
UPDATE
|
Modifies existing records
|
|
DELETE
|
Removes records
|
|
SELECT
|
Displays records
|
Aim
Theory
SQL Operators
-
BETWEEN: Selects values within a specified range.
-
IN: Selects records matching any value in a given list.
-
LIKE: Searches for a specified pattern in a column.
-
ORDER BY: Sorts query results in ascending (ASC) or descending (DESC)order.
DML Commands
-
INSERT–Adds new records.
-
UPDATE–Modifies existing records.
-
DELETE–Removes records.
Example Table
Create Table
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
72
|
|
103
|
Priya
|
ECE
|
90
|
|
104
|
Kiran
|
CSE
|
65
|
|
105
|
Meena
|
ISE
|
88
|
1. BETWEEN Operator
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
72
|
|
103
|
Priya
|
ECE
|
90
|
|
105
|
Meena
|
ISE
|
88
|
2. IN Operator
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
72
|
|
104
|
Kiran
|
CSE
|
65
|
|
105
|
Meena
|
ISE
|
88
|
3. LIKE Operator
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
4. ORDER BY Operator
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
103
|
Priya
|
ECE
|
90
|
|
105
|
Meena
|
ISE
|
88
|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
72
|
|
104
|
Kiran
|
CSE
|
65
|
5. UPDATE Operation
6. DELETE Operation
7.Display Final Table
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
80
|
|
103
|
Priya
|
ECE
|
90
|
|
105
|
Meena
|
ISE
|
88
|
Aim
Objective
-
Understand the purpose of SQL aggregate functions.
-
Calculate the total number of records using COUNT().
-
Find the total using SUM().
-
Calculate the average using AVG().
-
Determine the maximum value using MAX().
-
Determine the minimum value using MIN().
Theory
|
Function
|
Purpose
|
|---|---|
|
COUNT()
|
Counts the number of rows
|
|
SUM()
|
Calculates the total of a numeric column
|
|
AVG()
|
Calculates the average value
|
|
MAX()
|
Finds the highest value
|
|
MIN()
|
Finds the lowest value
|
Example Table
Create Table
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
72
|
|
103
|
Priya
|
ECE
|
90
|
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
104
|
Kiran
|
CSE
|
65
|
|
105
|
Meena
|
ISE
|
88
|
1. COUNT() Function
|
Total_Students
|
|---|
|
5
|
2. SUM() Function
|
Total_Marks
|
|---|
|
400
|
3. AVG() Function
|
Average_Marks
|
|---|
|
80
|
4. MAX() Function
|
Highest_Marks
|
|---|
|
90
|
5. MIN() Function
|
Lowest_Marks
|
|---|
|
65
|
|
Function
|
SQL Query
|
Result
|
|---|---|---|
|
COUNT()
|
SELECT COUNT(*) FROM Student;
|
5
|
|
SUM()
|
SELECT SUM(Marks) FROM Student;
|
400
|
|
AVG()
|
SELECT AVG(Marks) FROM Student;
|
80
|
|
MAX()
|
SELECT MAX(Marks) FROM Student;
|
90
|
|
MIN()
|
SELECT MIN(Marks) FROM Student;
|
65
|
7.
Write SQL queries using GROUP BY and HAVING
clauses.
Aim
Theory
GROUP BY
HAVING
Example Table
Create Table
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
72
|
|
103
|
Priya
|
CSE
|
90
|
|
104
|
Kiran
|
ECE
|
65
|
|
105
|
Meena
|
ISE
|
88
|
1. GROUP BY Clause
|
Department
|
TotalStudents
|
|---|---|
|
CSE
|
2
|
|
ECE
|
1
|
|
ISE
|
2
|
2. GROUP BY with SUM()
|
Department
|
TotalMarks
|
|---|---|
|
CSE
|
175
|
|
ECE
|
65
|
|
ISE
|
160
|
3. HAVING Clause
|
Department
|
TotalStudents
|
|---|---|
|
CSE
|
2
|
|
ISE
|
2
|
Aim
Theory
-
UNION: Combines the results of two queries and removes duplicate rows.
-
INTERSECT: Returns only the common rows present in both queries.
-
EXCEPT(orMINUSin Oracle): Returns rows from the first query that are notpresent in the second query.
Example Tables
Create Table 1
|
StudentID
|
StudentName
|
|---|---|
|
101
|
Ananya
|
|
102
|
Rahul
|
|
103
|
Priya
|
|
StudentID
|
StudentName
|
|---|---|
|
102
|
Rahul
|
|
StudentID
|
StudentName
|
|---|---|
|
103
|
Priya
|
|
104
|
Kiran
|
1. UNION Operation
|
StudentID
|
StudentName
|
|---|---|
|
101
|
Ananya
|
|
102
|
Rahul
|
|
103
|
Priya
|
|
104
|
Kiran
|
2. INTERSECT Operation
|
StudentID
|
StudentName
|
|---|---|
|
102
|
Rahul
|
|
103
|
Priya
|
3. EXCEPT Operation
Output
|
StudentID
|
StudentName
|
|
|---|---|---|
|
101
|
Ananya
|
|
Aim
Theory
-
INNER JOIN–Returns only the matching records from both tables.
-
LEFT JOIN–Returns all records from the left table and matching records fromthe right table.
-
RIGHT JOIN–Returns all records from the right table and matching recordsfrom the left table.
Example Tables
1. Create Department Table
-
1.Create Student Table
3. Insert Records
Department Table
|
DeptID
|
DeptName
|
|---|---|
|
1
|
CSE
|
|
2
|
ISE
|
|
3
|
ECE
|
|
StudentID
|
StudentName
|
DeptID
|
|---|---|---|
|
101
|
Ananya
|
1
|
|
102
|
Rahul
|
2
|
|
103
|
Priya
|
1
|
1. INNER JOIN
Query
2. LEFT JOIN
Query
|
StudentID
|
StudentName
|
DeptName
|
|---|---|---|
|
101
|
Ananya
|
CSE
|
|
102
|
Rahul
|
ISE
|
|
103
|
Priya
|
CSE
|
3. RIGHT JOIN
Query
|
StudentID
|
StudentName
|
DeptName
|
|---|---|---|
|
101
|
Ananya
|
CSE
|
|
103
|
Priya
|
CSE
|
|
102
|
Rahul
|
ISE
|
|
NULL
|
NULL
|
ECE
|
|
Join
|
Result
|
|---|---|
|
INNER JOIN
|
Returns only matching records from both tables.
|
|
LEFT JOIN
|
Returns all rows from the left table and matching rows from the right
table.
|
|
RIGHT
JOIN
|
Returns all rows from the right table and matching rows from the left
table.
|
Aim
Theory
Advantages of Views
-
Simplifies complex SQL queries.
-
Provides data security by restricting access to specific columns.
-
Presents customized data to different users.
-
Makes data retrieval easier and reusable.
Example Table
1.
Create Student Table
-
2.Insert Records
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
72
|
|
103
|
Priya
|
CSE
|
90
|
1. Create a View
Query
2. Display Data from the View
Query
|
StudentName
|
Marks
|
|---|---|
|
Ananya
|
85
|
|
Rahul
|
72
|
|
Priya
|
90
|
3. Update the View
Query
Display Updated View
|
StudentName
|
Department
|
Marks
|
|---|---|---|
|
Ananya
|
CSE
|
85
|
|
Rahul
|
ISE
|
72
|
|
Priya
|
CSE
|
90
|
4. Drop the View
Query
|
SQL Command
|
Purpose
|
|---|---|
|
CREATE VIEW
|
Creates a new view
|
|
SELECT * FROM ViewName
|
Displays data from the view
|
|
CREATE OR REPLACE VIEW
|
Modifies an existing view
|
|
DROP VIEW
|
Deletes the view
|
Aim
Theory
Advantages of Stored Procedures
-
Reduces repeated SQL code.
-
Improves execution speed.
-
Enhances security by restricting direct table access.
-
Simplifies database operations.
Example Table
1.
Create Student Table
2.
Insert Records
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
72
|
|
103
|
Priya
|
ECE
|
90
|
1. Create a Stored Procedure
Query
2. Execute the Stored Procedure
Query
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
72
|
|
103
|
Priya
|
ECE
|
90
|
3. Stored Procedure with Parameter
Query
Execute the Procedure
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
4. Delete the Stored Procedure
Query
|
SQL Command
|
Purpose
|
|---|---|
|
CREATE PROCEDURE
|
Creates a stored procedure
|
|
EXEC
|
Executes a stored procedure
|
|
DROP PROCEDURE
|
Deletes a stored procedure
|
Aim
Theory
Steps in Using a Cursor
-
1.Declare the cursor.
-
2.Open the cursor.
-
3.Fetch rows from the cursor.
-
4.Process the fetched data.
-
5.Close the cursor.
-
6.Deallocate the cursor.
Example Table
1.
Create Student Table
-
2.Insert Records
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
|
102
|
Rahul
|
ISE
|
72
|
|
103
|
Priya
|
ECE
|
90
|
3. Declare and Use a Cursor
Query
-
DECLARE CURSOR creates the cursor.
-
OPEN activates the cursor.
-
FETCH NEXT retrieves one row at a time.
-
WHILE @@FETCH_STATUS = 0 continues until all rows are processed.
-
CLOSE closes the cursor.
-
DEALLOCATE releases the cursor from memory.
|
Command
|
Purpose
|
|---|---|
|
DECLARE CURSOR
|
Declares a cursor
|
|
OPEN
|
Opens the cursor
|
|
FETCH NEXT
|
Retrieves one row at a time
|
|
CLOSE
|
Closes the cursor
|
|
DEALLOCATE
|
Removes the cursor from memory
|
Aim
Theory
-
Maintaining audit records.
-
Enforcing business rules.
-
Validating data before insertion or update.
-
Automatically logging database changes.
Types of Triggers
-
INSERT Trigger–Executes after or before inserting a record.
-
UPDATE Trigger–Executes after or before updating a record.
-
DELETE Trigger–Executes after or before deleting a record.
Example Tables
1.
Create Student Table
2. Create Audit Table
3. Create an INSERT Trigger
Query
4. Test the Trigger
View Student Table
|
StudentID
|
StudentName
|
Department
|
Marks
|
|---|---|---|---|
|
101
|
Ananya
|
CSE
|
85
|
View Audit Table
|
StudentID
|
ActionPerformed
|
|---|---|
|
101
|
INSERT
|
5. Delete the Trigger
Query
|
SQL Command
|
Purpose
|
|---|---|
|
CREATE TRIGGER
|
Creates a trigger
|
|
AFTER INSERT
|
Executes after an insert operation
|
|
inserted
|
Temporary table containing newly inserted rows
|
|
DROP TRIGGER
|
Deletes a trigger
|
Advantages of Triggers
-
Automatically executes SQL statements.
-
Maintains audit logs.
-
Enforces business rules.
-
Improves data consistency.
-
Reduces repetitive application code.
Aim
Theory
Basic Components of an ER Model
-
Entity–A real-world object (e.g., Student, Course).
-
Attribute–Properties of an entity (e.g., StudentID, StudentName).
-
Relationship–Association between entities (e.g., Student enrolls in Course).
Problem Statement
Entities
Student
-
StudentID
-
StudentName
-
Department
Course
-
CourseID
-
CourseName
-
Credits
Enrollment
-
EnrollmentID
-
StudentID
-
CourseID
-
Semester
ER Model Explanation
-
AStudentcan enroll inmany Courses.
-
ACoursecan havemany Students.
-
TheEnrollmententity resolves the many-to-many relationship between Studentand Course.
Relational Schema
Student
SQL Implementation
1. Create Student Table
-
2.Create Course Table
-
3.Create Enrollment Table
Sample Data
Student
|
StudentID
|
StudentName
|
Department
|
|---|---|---|
|
101
|
Ananya
|
CSE
|
|
102
|
Rahul
|
ISE
|
|
CourseID
|
CourseName
|
Credits
|
|---|---|---|
|
C101
|
DBMS
|
4
|
|
C102
|
Operating Systems
|
3
|
|
EnrollmentID
|
StudentID
|
CourseID
|
Semester
|
|---|---|---|---|
|
1
|
101
|
C101
|
4th
|
|
2
|
101
|
C102
|
4th
|
|
3
|
102
|
C101
|
4th
|
|
Entity
|
Relationship
|
Entity
|
|---|---|---|
|
Student
|
Enrolls
|
Course
|
|
Student
|
1 : M
|
Enrollment
|
|
Course
|
1 : M
|
Enrollment
|
Advantages of ER Modeling
-
Provides a clear database design.
-
Identifies entities and relationships.
-
Minimizes data redundancy.
-
Helps convert the design into relational tables.
-
Improves database consistency and integrity.
Aim
Theory
Types of Normalization
-
First Normal Form (1NF):Removes repeating groups and ensures atomicvalues.
-
Second Normal Form (2NF):Removes partial dependency.
-
Third Normal Form (3NF):Removes transitive dependency.
-
Boyce-Codd Normal Form (BCNF):Ensures every determinant is a candidatekey.
Example Dataset (Unnormalized Form - UNF)
|
StudentID
|
StudentName
|
CourseID
|
CourseName
|
Faculty
|
|---|---|---|---|---|
|
101
|
Ananya
|
C101
|
DBMS
|
Dr. Ravi
|
|
101
|
Ananya
|
C102
|
OS
|
Dr. Meena
|
|
102
|
Rahul
|
C101
|
DBMS
|
Dr. Ravi
|
|
103
|
Priya
|
C103
|
Java
|
Dr. Kumar
|
Step 1: First Normal Form (1NF)
Rule
-
Remove repeating groups.
-
Each cell should contain only one value.
1NF Table
|
StudentID
|
StudentName
|
CourseID
|
CourseName
|
Faculty
|
|---|---|---|---|---|
|
101
|
Ananya
|
C101
|
DBMS
|
Dr. Ravi
|
|
101
|
Ananya
|
C102
|
OS
|
Dr. Meena
|
|
102
|
Rahul
|
C101
|
DBMS
|
Dr. Ravi
|
|
103
|
Priya
|
C103
|
Java
|
Dr. Kumar
|
Step 2: Second Normal Form (2NF)
Rule
-
Must be in1NF.
-
Removepartial dependency.
-
StudentNamedepends only onStudentID.
-
CourseNameandFacultydepend only onCourseID.
Student Table
|
StudentID
|
StudentName
|
|---|
|
StudentID
|
StudentName
|
|---|---|
|
101
|
Ananya
|
|
102
|
Rahul
|
|
103
|
Priya
|
|
CourseID
|
CourseName
|
Faculty
|
|---|---|---|
|
C101
|
DBMS
|
Dr. Ravi
|
|
C102
|
OS
|
Dr. Meena
|
|
C103
|
Java
|
Dr. Kumar
|
Enrollment Table
|
StudentID
|
CourseID
|
|---|---|
|
101
|
C101
|
|
101
|
C102
|
|
102
|
C101
|
|
103
|
C103
|
Step 3: Third Normal Form (3NF)
Rule
-
Must be in2NF.
-
Removetransitive dependency.
-
No non-key attribute depends on another non-key attribute.
-
Therefore, the tables are already in3NF.
3NF Tables
Step 4: Boyce-Codd Normal Form (BCNF)
Rule
-
StudentID uniquely identifies StudentName.
-
CourseID uniquely identifies CourseName and Faculty.
-
(StudentID, CourseID) uniquely identifies Enrollment.
|
Normal Form
|
Purpose
|
Result
|
|---|---|---|
|
1NF
|
Remove repeating groups
|
Atomic values
|
|
2NF
|
Remove partial dependency
|
Separate Student and Course details
|
|
3NF
|
Remove transitive dependency
|
No non-key dependency
|
|
BCNF
|
Every determinant is a candidate key
|
Fully normalized tables
|
Result
Aim
Theory
-
COMMIT–Saves all changes permanently.
-
ROLLBACK–Undoes all changes made during the transaction.
|
Property
|
Description
|
|---|---|
|
Atomicity
|
All operations are completed successfully or none are performed.
|
|
Consistency
|
The database remains in a valid state before and after the transaction.
|
|
Isolation
|
Transactions execute independently without interfering with each other.
|
|
Durability
|
Once committed, changes are permanently stored in the database.
|
Example Table
Create Table
|
AccountNo
|
CustomerName
|
Balance
|
|---|---|---|
|
101
|
Ananya
|
5000
|
|
102
|
Rahul
|
3000
|
Transaction Example using COMMIT
Step 1: Start Transaction
|
AccountNo
|
CustomerName
|
Balance
|
|---|---|---|
|
101
|
Ananya
|
4000
|
|
102
|
Rahul
|
4000
|
Transaction Example using ROLLBACK
Step 1: Start Transaction
|
AccountNo
|
CustomerName
|
Balance
|
|---|---|---|
|
101
|
Ananya
|
4000
|
|
102
|
Rahul
|
4000
|
Demonstration of ACID Properties
1. Atomicity
2. Consistency
3. Isolation
4. Durability
|
SQL Command
|
Purpose
|
|---|---|
|
START TRANSACTION
|
Begins a transaction
|
|
COMMIT
|
Saves changes permanently
|
|
ROLLBACK
|
Cancels all changes made during the transaction
|
Result