upload_hdwq5aisjtdrlbxabvhtittl3p.pdf

1.Write SQL Queries to Create Database Schema Using DDL
Commands

Aim

To understand and implement
Data Definition Language (DDL)
commands for creating
a database schema including databases, tables, constraints, and modifying database
structures.

Introduction to SQL

Structured Query Language (SQL) is the standard language used to communicate with
Relational Database Management Systems (RDBMS). SQL allows users to create,
modify, retrieve, and manipulate data stored in databases.

SQL is divided into several categories:

Category

Purpose

DDL

Defines database structure

DML

Manipulates data

DCL

Controls permissions

TCL

Manages transactions

DQL

Retrieves data

What is DDL?

Data Definition Language (DDL) consists of SQL commands used to define, modify, and
remove database objects.

These objects include

  • Database
  • Tables
  • Views
  • Indexes
  • Constraints

DDL commands automatically commit changes.

Command

Purpose

CREATE

Creates new objects

ALTER

Modifies existing objects

DROP

Deletes objects

TRUNCATE

Removes all rows

RENAME

Renames objects

Database Schema

A schema is the logical structure of a database.

It defines

  • Tables
  • Columns
  • Data Types
  • Constraints
  • Relationships

Example:
College Database

Student

Faculty

Department

Course

DDL Command 1

CREATE DATABASE

Creates a new database.

Syntax

CREATE DATABASE database_name;

Example

CREATE DATABASE CollegeDB;

Expected Output

Database created successfully.

Selecting Database

USE CollegeDB;

Output

Database changed.

CREATE TABLE

Used to create a new table.

Syntax

CREATE TABLE table_name

(

ColumnName DataType Constraints

);

Common Data Types

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

Constraints enforce data integrity.

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

CREATE TABLE Department

(

DeptID INT PRIMARY KEY,

DeptName VARCHAR(40) NOT NULL UNIQUE

);

Expected Output

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

CREATE TABLE Student

(

StudentID INT PRIMARY KEY,

StudentName VARCHAR(40) NOT NULL,

Age INT,

Email VARCHAR(50) UNIQUE,

DeptID INT,

FOREIGN KEY (DeptID)

REFERENCES Department(DeptID)

);

Expected Output

Table created successfully.

Explanation

Student table contains

  • Primary Key
  • Foreign Key
  • Unique constraint
  • NOT NULL constraint

Department table becomes the parent table.

Student becomes the child table.

Viewing Table Structure

DESCRIBE Student;

Output

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

CREATE TABLE Faculty

(

FacultyID INT PRIMARY KEY,

FacultyName VARCHAR(50) NOT NULL,

Salary DECIMAL(10,2),

DeptID INT,

FOREIGN KEY (DeptID)

REFERENCES Department(DeptID)

);

Output

Table created successfully.

ALTER TABLE:
Used to modify an existing table.

Add Column

ALTER TABLE Student

ADD Phone VARCHAR(15);

Output

Query OK

Table Structure

| StudentID |
| StudentName |
| Age |
| Email |
| DeptID |
| Phone |

Modify Column

ALTER TABLE Student

MODIFY Phone VARCHAR(20);

Output

Query OK

Rename Column

ALTER TABLE Student

RENAME COLUMN Phone TO MobileNo;

Output

Query OK

Drop Column

ALTER TABLE Student

DROP COLUMN MobileNo;

Output

Query OK

RENAME TABLE

RENAME TABLE Student TO Students;

Output

Query OK

TRUNCATE TABLE

Deletes all records.

TRUNCATE TABLE Students;

Output

Table truncated.

DROP TABLE

Deletes complete table.

DROP TABLE Students;

Output

Table dropped.

DROP DATABASE

DROP DATABASE CollegeDB;

Output

Database dropped.

  • 2.
    Write SQL queries to perform basic DML operations
    (INSERT, UPDATE, DELETE).

1.Aim

To write and execute SQL queries to perform basic
Data Manipulation Language
(DML)
operations such as:

  • INSERT
  • UPDATE
  • DELETE

using a relational database table.

2. Objective

After completing this experiment, students will be able to:

  • 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 DELETE
    operations.

3. Introduction to DML

Data Manipulation Language (DML)
is a subset of SQL used to manage and
manipulate data stored in database tables.

The major DML commands are:

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

We will create a table named Student.

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

CREATE TABLE Student (

StudentID INT PRIMARY KEY,

StudentName VARCHAR(50),

Department VARCHAR(30),

Age INT,

Marks INT

);

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

Table Student created successfully.

6. INSERT Operation

The INSERT command is used to add new records to a table.

Syntax

INSERT INTO table_name

(column1, column2, column3, ...
)

VALUES

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

Example 1: Insert a Single Record

SQL Query

INSERT INTO Student

(StudentID, StudentName, Department, Age, Marks)

VALUES

(101, 'Ananya', 'CSE', 20, 85);

Output

1 row inserted successfully.

Explanation

A new student record is inserted into the Student table.

Table After INSERT

StudentID

StudentName

Department

Age

Marks

101

Ananya

CSE

20

85

Example 2: Insert Multiple Records

INSERT INTO Student

(StudentID, StudentName, Department, Age, Marks)

VALUES

(102, 'Rahul', 'ISE', 21, 78),

(103, 'Priya', 'ECE', 20, 88),

(104, 'Kiran', 'CSE', 22, 91);

Output

3 rows inserted successfully.

Table After Inserting 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

The UPDATE command is used to modify existing records in a table.

Syntax

UPDATE table_name

SET column_name = new_value

WHERE condition;

Important:
The WHERE clause specifies which record should be updated.

Example 1: Update Marks of a Student

SQL Query

UPDATE Student

SET Marks = 90

WHERE StudentID = 102;

Output

1 row updated successfully.

Before UPDATE

StudentID

StudentName

Department

Age

Marks

102

Rahul

ISE

21

78

After UPDATE

StudentID

StudentName

Department

Age

Marks

102

Rahul

ISE

21

90

Example 2: Update Multiple Columns

UPDATE Student

SET Department = 'CSE',

Marks = 95

WHERE StudentID = 103;

Output

1 row updated successfully.

Updated Record

StudentID

StudentName

Department

Age

Marks

103

Priya

CSE

20

95

Example 3: Update Multiple Records

UPDATE Student

SET Marks = Marks + 5

WHERE Department = 'CSE';

Explanation

This query increases the marks of all students belonging to the CSE department by 5
marks.

Output

CSE student records updated successfully.

8. DELETE Operation

The DELETE command is used to remove existing records from a table.

Syntax

DELETE FROM table_name

WHERE condition;

Important:
Always use the WHERE clause carefully with DELETE.

Example 1: Delete a Specific Record

SQL Query

DELETE FROM Student

WHERE StudentID = 104;

Output

1 row deleted successfully.

Before DELETE

StudentID

StudentName

Department

Age

Marks

104

Kiran

CSE

22

91

After DELETE

The record with StudentID = 104 is removed from the table.

Example 2: Delete Records Based on a Condition

DELETE FROM Student

WHERE Marks < 80;

Explanation

This query deletes all student records where the marks are less than 80.

Output

Records satisfying the condition deleted successfully.

9. Display the Table Contents

The SELECT command can be used to verify the results of DML operations.

SELECT * FROM Student;

Sample Output

StudentID

StudentName

Department

Age

Marks

101

Ananya

CSE

20

90

102

Rahul

ISE

21

90

103

Priya

CSE

20

100

3.Write SQL queries using constraints (PRIMARY KEY,
FOREIGN KEY, UNIQUE, NOT NULL).

Aim

To write and execute SQL queries using the following constraints:

  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE
  • NOT NULL

and to perform basic Data Manipulation Language (DML) operations:

  • INSERT
  • UPDATE
  • DELETE

Objectives

After completing this experiment, students will be able to:

  • 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?

Constraints are rules applied to columns in a database table to maintain the
accuracy,
validity, and integrity of data
.

For example:

  • 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.

SQL constraints help prevent invalid data from being inserted into the database.

Types of Constraints Used

PRIMARY KEY

A PRIMARY KEY uniquely identifies each record in a table.

Properties

  • Cannot contain NULL values.
  • Must contain unique values.
  • A table can have only one primary key constraint.

Example

DeptID INT PRIMARY KEY

FOREIGN KEY

A FOREIGN KEY establishes a relationship between two tables.

It refers to the PRIMARY KEY of another table.

Example

FOREIGN KEY (DeptID)

REFERENCES Department(DeptID)

Here, DeptID in the Student table refers to DeptID in the Department table.

UNIQUE

The UNIQUE constraint ensures that all values in a column are different.

For example, two students cannot have the same email address.

Email VARCHAR(100) UNIQUE

NOT NULL

The NOT NULL constraint ensures that a column cannot contain an empty or NULL
value.

StudentName VARCHAR(50) NOT NULL

Database Schema

In this experiment, two tables are created:

Parent Table

Department

Child Table

Student

The relationship is:

Department

|

| 1

|

|------< Many

Student

A department can have many students.

Create the Department Table

SQL Query

CREATE TABLE Department (

DeptID INT PRIMARY KEY,

DeptName VARCHAR(50) UNIQUE NOT NULL

);

Expected Outpu
t

Table Department created successfully.

Insert Records into the Department Table

INSERT INTO Department

(DeptID, DeptName)

VALUES

(1, 'Computer Science');

INSERT INTO Department

(DeptID, DeptName)

VALUES

(2, 'Information Science');

INSERT INTO Department

(DeptID, DeptName)

VALUES

(3, 'Electronics and Communication');

Display the Department Table

SELECT * FROM Department;

Output

DeptID

DeptName

1

Computer Science

2

Information Science

3

Electronics and Communication

Create the Student Table

CREATE TABLE Student (

StudentID INT PRIMARY KEY,

StudentName VARCHAR(50) NOT NULL,

Email VARCHAR(100) UNIQUE,

DeptID INT,

FOREIGN KEY (DeptID)

REFERENCES Department(DeptID)

);

Explanation

StudentID INT PRIMARY KEY

Each student must have a unique StudentID.

101 → Student 1

102 → Student 2

103 → Student 3

Two students cannot have the same StudentID.

StudentName VARCHAR(50) NOT NULL

Every student must have a name.

The following is invalid:

INSERT INTO Student

VALUES (104, NULL, 'student@email.com', 1);

This will produce an error because StudentName cannot be NULL.

Email VARCHAR(100) UNIQUE

Every student's email address must be different.

The following is invalid if the email already exists:

INSERT INTO Student

VALUES (105, 'Ravi', 'student@email.com', 1);

FOREIGN KEY (DeptID)

The DeptID entered in the Student table must already exist in the Department table.

For example:

DeptID = 1 → Valid

DeptID = 2 → Valid

DeptID = 10 → Invalid if DeptID 10 does not exist

Expected Output

Table Student created successfully.

INSERT Operation

The INSERT command is used to add new records to a table.

Syntax

INSERT INTO table_name

(column1, column2, column3)

VALUES

(value1, value2, value3);

Example 1: Insert a Valid Student Record

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(101, 'Ananya', 'ananya@gmail.com', 1);

Output

1 row inserted successfully.

Example 2: Insert Multiple Student Records

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(102, 'Rahul', 'rahul@gmail.com', 2);

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(103, 'Priya', 'priya@gmail.com', 1);

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(104, 'Kiran', 'kiran@gmail.com', 3);

Display Student Records

SELECT * FROM Student;

Output

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

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(101, 'Ravi', 'ravi@gmail.com', 1);

Output

ERROR: Duplicate primary key value.

Explanation

StudentID = 101 already exists.

Testing UNIQUE Constraint

Query

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(105, 'Ravi', 'ananya@gmail.com', 1);

Output

ERROR: Duplicate value violates UNIQUE constraint.

Explanation

The email address already exists in the table.

Testing NOT NULL Constraint

Query

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(106, NULL, 'newstudent@gmail.com', 1);

Output

ERROR: StudentName cannot be NULL.

Testing FOREIGN KEY Constraint

Query

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(107, 'Meena', 'meena@gmail.com', 10);

Output

ERROR: Foreign key constraint violation.

Explanation

DeptID = 10 does not exist in the Department table.

UPDATE Operation

The UPDATE command is used to modify existing records.

Syntax

UPDATE table_name

SET column_name = new_value

WHERE condition;

Example 1: Update Student Email

UPDATE Student

SET Email = 'rahul_updated@gmail.com'

WHERE StudentID = 102;

Output

1 row updated successfully.

Before UPDATE

StudentID

StudentName

Email

102

Rahul

rahul@gmail.com

After UPDATE

StudentID

StudentName

Email

102

Rahul

rahul_updated@gmail.com

Example 2: Change the Department of a Student

UPDATE Student

SET DeptID = 3

WHERE StudentID = 103;

Output

1 row updated successfully.

This update is valid because DeptID = 3 exists in the Department table.

Invalid UPDATE Example

UPDATE Student

SET DeptID = 10

WHERE StudentID = 103;

Output

ERROR: Foreign key constraint violation.

Reason

DeptID = 10 does not exist in the Department table.

DELETE Operation

The DELETE command is used to remove records from a table.

Syntax

DELETE FROM table_name

WHERE condition;

Example 1: Delete a Student Record

DELETE FROM Student

WHERE StudentID = 104;

Output

1 row deleted successfully.

Student Table Before DELETE

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

Student Table After DELETE

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

Suppose a department has students associated with it.

For example:

Department 1 → Computer Science

Student 101 → Department 1

Student 103 → Department 1

If we try to delete Department 1:

DELETE FROM Department

WHERE DeptID = 1;

The database may reject the operation because students still refer to that department.

Possible Output

ERROR: Cannot delete parent record because child records exist.

This protection is provided by the FOREIGN KEY constraint.

Complete SQL Program

-- Create Department table

CREATE TABLE Department (

DeptID INT PRIMARY KEY,

DeptName VARCHAR(50) UNIQUE NOT NULL

);

-- Insert records into Department

INSERT INTO Department

(DeptID, DeptName)

VALUES

(1, 'Computer Science');

INSERT INTO Department

(DeptID, DeptName)

VALUES

(2, 'Information Science');

INSERT INTO Department

(DeptID, DeptName)

VALUES

(3, 'Electronics and Communication');

-- Create Student table

CREATE TABLE Student (

StudentID INT PRIMARY KEY,

StudentName VARCHAR(50) NOT NULL,

Email VARCHAR(100) UNIQUE,

DeptID INT,

FOREIGN KEY (DeptID)

REFERENCES Department(DeptID)

);

-- INSERT operation

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(101, 'Ananya', 'ananya@gmail.com', 1);

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(102, 'Rahul', 'rahul@gmail.com', 2);

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(103, 'Priya', 'priya@gmail.com', 1);

INSERT INTO Student

(StudentID, StudentName, Email, DeptID)

VALUES

(104, 'Kiran', 'kiran@gmail.com', 3);

-- Display records

SELECT * FROM Student;

-- UPDATE operation

UPDATE Student

SET Email = 'rahul_updated@gmail.com'

WHERE StudentID = 102;

-- Update department

UPDATE Student

SET DeptID = 3

WHERE StudentID = 103;

-- DELETE operation

DELETE FROM Student

WHERE StudentID = 104;

-- Display final records

SELECT * FROM Student;

Final Output

Department Table

DeptID

DeptName

1

Computer Science

2

Information Science

3

Electronics and Communication

Final Student Table

StudentID

StudentName

Email

DeptID

101

Ananya

ananya@gmail.com

1

102

Rahul

rahul_updated@gmail.com

2

103

Priya

priya@gmail.com

3

The record of
Kiran
was deleted successfully.

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

4.Write SQL queries using operators (BETWEEN, IN, LIKE,
ORDER BY).

Aim

To write and execute SQL queries using
BETWEEN, IN, LIKE, ORDER BY
operators
and perform basic DML operations (
INSERT, UPDATE, DELETE
).

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

CREATE TABLE Student(

StudentID INT PRIMARY KEY,

StudentName VARCHAR(50),

Department VARCHAR(30),

Marks INT

);

Output

Table created successfully.

Insert Records

INSERT INTO Student VALUES

(101,'Ananya','CSE',85),

(102,'Rahul','ISE',72),

(103,'Priya','ECE',90),

(104,'Kiran','CSE',65),

(105,'Meena','ISE',88);

Output

5 rows inserted successfully.

Student 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

Displays students whose marks are between 70 and 90.

SELECT * FROM Student

WHERE Marks BETWEEN 70 AND 90;

Output

StudentID

StudentName

Department

Marks

101

Ananya

CSE

85

102

Rahul

ISE

72

103

Priya

ECE

90

105

Meena

ISE

88

2. IN Operator

Displays students belonging to
CSE
or
ISE
departments.

SELECT * FROM Student

WHERE Department IN ('CSE','ISE');

Output

StudentID

StudentName

Department

Marks

101

Ananya

CSE

85

102

Rahul

ISE

72

104

Kiran

CSE

65

105

Meena

ISE

88

3. LIKE Operator

Displays student names starting with
A
.

SELECT * FROM Student

WHERE StudentName LIKE 'A%';

Output

StudentID

StudentName

Department

Marks

101

Ananya

CSE

85

4. ORDER BY Operator

Display students in descending order of marks.

SELECT * FROM Student

ORDER BY Marks DESC;

Output

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

Increase Rahul's marks to
80
.

UPDATE Student

SET Marks = 80

WHERE StudentID = 102;

Output

1 row updated successfully.

6. DELETE Operation

Delete student with StudentID
104
.

DELETE FROM Student

WHERE StudentID = 104;

Output

1 row deleted successfully.

7.Display Final Table

SELECT * FROM Student;

Output

StudentID

StudentName

Department

Marks

101

Ananya

CSE

85

102

Rahul

ISE

80

103

Priya

ECE

90

105

Meena

ISE

88

5.Write SQL queries using aggregate functions (COUNT,
SUM, AVG, MAX, MIN).

Aim

To write and execute SQL queries using aggregate functions
COUNT(), SUM(), AVG(),
MAX(), and MIN()
to perform calculations on data stored in a table.

Objective

After completing this experiment, students will be able to:

  • 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

Aggregate functions perform calculations on a group of rows and return
a single result
.

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

CREATE TABLE Student(

StudentID INT PRIMARY KEY,

StudentName VARCHAR(50),

Department VARCHAR(30),

Marks INT

);

Output

Table created successfully.

Insert Records

INSERT INTO Student VALUES

(101,'Ananya','CSE',85),

(102,'Rahul','ISE',72),

(103,'Priya','ECE',90),

(104,'Kiran','CSE',65),

(105,'Meena','ISE',88);

Output

5 rows inserted successfully.

Student 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

Counts the total number of students.

Query

SELECT COUNT(*) AS Total_Students

FROM Student;

Output

Total_Students

5

2. SUM() Function

Calculates the total marks of all students.

Query

SELECT SUM(Marks) AS Total_Marks

FROM Student;

Output

Total_Marks

400

3. AVG() Function

Calculates the average marks.

Query

SELECT AVG(Marks) AS Average_Marks

FROM Student;

Output

Average_Marks

80

4. MAX() Function

Displays the highest marks.

Query

SELECT MAX(Marks) AS Highest_Marks

FROM Student;

Output

Highest_Marks

90

5. MIN() Function

Displays the lowest marks.

Query

SELECT MIN(Marks) AS Lowest_Marks

FROM Student;

Output

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

To write and execute SQL queries using the
GROUP BY
and
HAVING
clauses to group
records and filter grouped results.

Theory

GROUP BY

The GROUP BY clause groups rows that have the same values in one or more columns.
It is commonly used with aggregate functions such as COUNT(), SUM(), AVG(), MAX(),
and MIN().

HAVING

The HAVING clause is used to filter the grouped records after the GROUP BY operation.
It works similarly to the WHERE clause but is applied to groups instead of individual
rows.

Example Table

Create Table

CREATE TABLE Student(

StudentID INT PRIMARY KEY,

StudentName VARCHAR(50),

Department VARCHAR(20),

Marks INT

);

CREATE TABLE Student(

StudentID INT PRIMARY KEY,

StudentName VARCHAR(50),

Department VARCHAR(20),

Marks INT

);

Output

Table created successfully.

Insert Records

INSERT INTO Student VALUES

(101,'Ananya','CSE',85),

(102,'Rahul','ISE',72),

(103,'Priya','CSE',90),

(104,'Kiran','ECE',65),

(105,'Meena','ISE',88);

Output

5 rows inserted successfully.

Student 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

Display the
number of students
in each department.

SELECT Department, COUNT(*) AS TotalStudents

FROM Student

GROUP BY Department;

Output

Department

TotalStudents

CSE

2

ECE

1

ISE

2

2. GROUP BY with SUM()

Display the
total marks
obtained by students in each department.

SELECT Department, SUM(Marks) AS TotalMarks

FROM Student

GROUP BY Department;

Output

Department

TotalMarks

CSE

175

ECE

65

ISE

160

3. HAVING Clause

Display departments having more than one student
.

SELECT Department, COUNT(*) AS TotalStudents

FROM Student

GROUP BY Department

HAVING COUNT(*) > 1;

Output

Department

TotalStudents

CSE

2

ISE

2

7.Write SQL queries to perform set operations (UNION,
INTERSECT, EXCEPT).

Aim

To write and execute SQL queries using
UNION
,
INTERSECT
, and
EXCEPT
set
operations to combine and compare data from two tables.

Theory

Set operations are used to combine the results of two or more SELECT queries.

  • UNION
    : Combines the results of two queries and removes duplicate rows.
  • INTERSECT
    : Returns only the common rows present in both queries.
  • EXCEPT
    (or
    MINUS
    in Oracle): Returns rows from the first query that are not
    present in the second query.

Note:
Both SELECT statements must have the same number of columns with compatible
data types.

Example Tables

Create Table 1

CREATE TABLE CSE_Students (

StudentID INT,

StudentName VARCHAR(50)

);

Output

Table created successfully.

Create Table 2

CREATE TABLE ISE_Students (

StudentID INT,

StudentName VARCHAR(50)

);

Output

Table created successfully.

Insert Records

INSERT INTO CSE_Students VALUES

(101,'Ananya'),

(102,'Rahul'),

(103,'Priya');

INSERT INTO ISE_Students VALUES

(102,'Rahul'),

(103,'Priya'),

(104,'Kiran');

Output

3 rows inserted successfully into each table.

CSE_Students

StudentID

StudentName

101

Ananya

102

Rahul

103

Priya

ISE_Students

StudentID

StudentName

102

Rahul

StudentID

StudentName

103

Priya

104

Kiran

1. UNION Operation

Display all unique students from both tables.

SELECT * FROM CSE_Students

UNION

SELECT * FROM ISE_Students;

Output

StudentID

StudentName

101

Ananya

102

Rahul

103

Priya

104

Kiran

Explanation:
UNION combines the results of both tables and removes duplicate rows.

2. INTERSECT Operation

Display students common to both tables.

SELECT * FROM CSE_Students

INTERSECT

SELECT * FROM ISE_Students;

Output

StudentID

StudentName

102

Rahul

103

Priya

3. EXCEPT Operation

Display students present in CSE_Students but not in ISE_Students.

SELECT * FROM CSE_Students

EXCEPT

SELECT * FROM ISE_Students;

Output

StudentID

StudentName

101

Ananya

Explanation:
EXCEPT returns rows from the first table that do not exist in the second
table.

Note:
In Oracle SQL, use MINUS instead of EXCEPT.

8.Write SQL queries to implement joins (INNER JOIN,
LEFT JOIN, RIGHT JOIN).

Aim

To write and execute SQL queries using
INNER JOIN
,
LEFT JOIN
, and
RIGHT
JOIN
to retrieve data from two related tables.

Theory

A
JOIN
is used to combine data from two or more tables based on a related column.

The commonly used joins are:

  • INNER JOIN
    –
    Returns only the matching records from both tables.
  • LEFT JOIN
    –
    Returns all records from the left table and matching records from
    the right table.
  • RIGHT JOIN
    –
    Returns all records from the right table and matching records
    from the left table.

Example Tables

1. Create Department Table

CREATE TABLE Department (

DeptID INT PRIMARY KEY,

DeptName VARCHAR(30)

);

Output

Table Department created successfully.

  • 1.
    Create Student Table

CREATE TABLE Student (

StudentID INT PRIMARY KEY,

StudentName VARCHAR(30),

DeptID INT,

FOREIGN KEY (DeptID)

REFERENCES Department(DeptID)

);

Output

Table Student created successfully.

3. Insert Records

Department Table

INSERT INTO Department VALUES

(1,'CSE'),

(2,'ISE'),

(3,'ECE');

Student Table

INSERT INTO Student VALUES

(101,'Ananya',1),

(102,'Rahul',2),

(103,'Priya',1);

Output

Rows inserted successfully.

Department Table

DeptID

DeptName

1

CSE

2

ISE

3

ECE

Student Table

StudentID

StudentName

DeptID

101

Ananya

1

102

Rahul

2

103

Priya

1

1. INNER JOIN

Purpose:
Displays only the records that have matching values in both tables.

Query

SELECT Student.StudentID,

Student.StudentName,

Department.DeptName

FROM Student

INNER JOIN Department

ON Student.DeptID = Department.DeptID;

Output

Explanation:
Only matching department records are displayed.

2. LEFT JOIN

Purpose:
Displays all records from the
Student
table and matching records from the
Department
table.

Query

SELECT Student.StudentID,

Student.StudentName,

Department.DeptName

FROM Student

LEFT JOIN Department

ON Student.DeptID = Department.DeptID;

Output

StudentID

StudentName

DeptName

101

Ananya

CSE

102

Rahul

ISE

103

Priya

CSE

Explanation:
All students are displayed. If a student has no matching department,
NULL is shown for DeptName.

3. RIGHT JOIN

Purpose:
Displays all records from the
Department
table and matching records from the
Student
table.

Query

SELECT Student.StudentID,

Student.StudentName,

Department.DeptName

FROM Student

RIGHT JOIN Department

ON Student.DeptID = Department.DeptID;

Output

StudentID

StudentName

DeptName

101

Ananya

CSE

103

Priya

CSE

102

Rahul

ISE

NULL

NULL

ECE

Explanation:
All departments are displayed. Since no student belongs to the
ECE
department, the student columns contain NULL.

Difference Between Joins

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.

9.Write SQL queries to create and use views.

Aim

To create and use
SQL Views
for retrieving data from one or more database tables.

Theory

A
View
is a virtual table created from the result of a SQL SELECT query. It does not
store data physically but displays data from the underlying table(s).

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

CREATE TABLE Student (

StudentID INT PRIMARY KEY,

StudentName VARCHAR(30),

Department VARCHAR(20),

Marks INT

);

Output

Table Student created successfully.

  • 2.
    Insert Records

INSERT INTO Student VALUES

(101,'Ananya','CSE',85),

(102,'Rahul','ISE',72),

(103,'Priya','CSE',90);

Output

3 rows inserted successfully.

Student Table

StudentID

StudentName

Department

Marks

101

Ananya

CSE

85

102

Rahul

ISE

72

103

Priya

CSE

90

1. Create a View

Create a view to display only the student name and marks.

Query

CREATE VIEW Student_View AS

SELECT StudentName, Marks

FROM Student;

Output

View Student_View created successfully.

2. Display Data from the View

Query

SELECT * FROM Student_View;

Output

StudentName

Marks

Ananya

85

Rahul

72

Priya

90

Explanation:
The view displays only the selected columns (StudentName and Marks)
from the Student table.

3. Update the View

Modify the view to include the
Department
column.

Query

CREATE OR REPLACE VIEW Student_View AS

SELECT StudentName, Department, Marks

FROM Student;

Output

View replaced successfully.

Display Updated View

SELECT * FROM Student_View;

Output

StudentName

Department

Marks

Ananya

CSE

85

Rahul

ISE

72

Priya

CSE

90

4. Drop the View

Delete the view when it is no longer required.

Query

DROP VIEW Student_View;

Output

View dropped successfully.

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

10.Write SQL queries to implement stored procedures.

Aim

To create and execute
Stored Procedures
in SQL for performing database operations.

Theory

A
Stored Procedure
is a precompiled collection of SQL statements stored in the
database. It can be executed whenever required, improving code reusability and
performance.

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

CREATE TABLE Student (

StudentID INT PRIMARY KEY,

StudentName VARCHAR(30),

Department VARCHAR(20),

Marks INT

);

Output

Table Student created successfully.

2.
Insert Records

INSERT INTO Student VALUES

(101,'Ananya','CSE',85),

(102,'Rahul','ISE',72),

(103,'Priya','ECE',90);

Output

3 rows inserted successfully.

Student Table

StudentID

StudentName

Department

Marks

101

Ananya

CSE

85

102

Rahul

ISE

72

103

Priya

ECE

90

1. Create a Stored Procedure

Create a stored procedure to display all student records.

Query

CREATE PROCEDURE DisplayStudents

AS

BEGIN

SELECT * FROM Student;

END;

Output

Procedure created successfully.

2. Execute the Stored Procedure

Query

EXEC DisplayStudents;

Output

StudentID

StudentName

Department

Marks

101

Ananya

CSE

85

102

Rahul

ISE

72

103

Priya

ECE

90

Explanation:
The procedure retrieves all records from the Student table.

3. Stored Procedure with Parameter

Create a procedure to display details of a specific student using StudentID.

Query

CREATE PROCEDURE GetStudent

@ID INT

AS

BEGIN

SELECT * FROM Student

WHERE StudentID = @ID;

END;

Output

Procedure created successfully.

Execute the Procedure

EXEC GetStudent 101;

Output

StudentID

StudentName

Department

Marks

101

Ananya

CSE

85

Explanation:
The procedure displays the details of the student whose StudentID is
101
.

4. Delete the Stored Procedure

Query

DROP PROCEDURE GetStudent;

Output

Procedure dropped successfully.

SQL Command

Purpose

CREATE PROCEDURE

Creates a stored procedure

EXEC

Executes a stored procedure

DROP PROCEDURE

Deletes a stored procedure

11.Write SQL queries to implement cursors.

Aim

To create and use
SQL Cursors
to retrieve and process records one row at a time from a
database table.

Theory

A
Cursor
is a database object used to fetch and process rows
one at a time
from the
result set of a query. Cursors are useful when row-by-row processing is required,
although set-based SQL operations are generally more efficient.

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

CREATE TABLE Student (

StudentID INT PRIMARY KEY,

StudentName VARCHAR(30),

Department VARCHAR(20),

Marks INT

);

Output

Table Student created successfully.

  • 2.
    Insert Records

INSERT INTO Student VALUES

(101,'Ananya','CSE',85),

(102,'Rahul','ISE',72),

(103,'Priya','ECE',90);

Output

3 rows inserted successfully.

Student Table

StudentID

StudentName

Department

Marks

101

Ananya

CSE

85

102

Rahul

ISE

72

103

Priya

ECE

90

3. Declare and Use a Cursor

The following cursor fetches each student's name and marks one record at a time.

Query

DECLARE @Name VARCHAR(30), @Marks INT;

DECLARE StudentCursor CURSOR FOR

SELECT StudentName, Marks

FROM Student;

OPEN StudentCursor;

FETCH NEXT FROM StudentCursor

INTO @Name, @Marks;

WHILE @@FETCH_STATUS = 0

BEGIN

PRINT @Name + ' - Marks: ' + CAST(@Marks AS VARCHAR);

FETCH NEXT FROM StudentCursor

INTO @Name, @Marks;

END;

CLOSE StudentCursor;

DEALLOCATE StudentCursor;

Output

Ananya - Marks: 85

Rahul - Marks: 72

Priya - Marks: 90

Explanation:

  • 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

12.Write SQL queries to create and use triggers.

Aim

To create and use
SQL Triggers
to automatically perform actions when
INSERT
,
UPDATE
, or
DELETE
operations are executed on a table.

Theory

A
Trigger
is a special type of stored program that is automatically executed (or "fired")
when a specified event occurs on a table.

Triggers are commonly used for:

  • 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

CREATE TABLE Student (

StudentID INT PRIMARY KEY,

StudentName VARCHAR(30),

Department VARCHAR(20),

Marks INT

);

Output

Table Student created successfully.

2. Create Audit Table

This table stores information about inserted records.

CREATE TABLE Student_Audit (

StudentID INT,

ActionPerformed VARCHAR(20)

);

Output

Table Student_Audit created successfully.

3. Create an INSERT Trigger

The trigger automatically stores the student ID in the audit table whenever a new student
is inserted.

Query

CREATE TRIGGER trg_InsertStudent

ON Student

AFTER INSERT

AS

BEGIN

INSERT INTO Student_Audit(StudentID, ActionPerformed)

SELECT StudentID, 'INSERT'

FROM inserted;

END;

Output

Trigger created successfully.

4. Test the Trigger

Insert a new record into the
Student
table.

INSERT INTO Student

VALUES

(101,'Ananya','CSE',85);

Output

1 row inserted successfully.

View Student Table

SELECT * FROM Student;

Output

StudentID

StudentName

Department

Marks

101

Ananya

CSE

85

View Audit Table

SELECT * FROM Student_Audit;

Output

StudentID

ActionPerformed

101

INSERT

Explanation:
When a record is inserted into the
Student
table, the trigger automatically inserts the
StudentID and the action (INSERT) into the
Student_Audit
table.

5. Delete the Trigger

Query

DROP TRIGGER trg_InsertStudent;

Output

Trigger dropped successfully.

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.

13. Design an ER model and convert it into a relational
schema.

Aim

To design an
Entity-Relationship (ER) Model
for a
Student Course Registration
System
and convert it into a
Relational Schema
.

Theory

An
Entity-Relationship (ER) Model
is a graphical representation of entities, their
attributes, and the relationships among them. It helps in designing a database before
implementation.

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

Design an ER model for a
Student Course Registration System
.

Entities

Student

  • StudentID
  • StudentName
  • Department

Course

  • CourseID
  • CourseName
  • Credits

Enrollment

Represents the relationship between Student and Course.

ChatGPT Image Jul 30, 2026, 12_57_15 PM
  • EnrollmentID
  • StudentID
  • CourseID
  • Semester

ER Diagram

ER Model Explanation

  • A
    Student
    can enroll in
    many Courses
    .
  • A
    Course
    can have
    many Students
    .
  • The
    Enrollment
    entity resolves the many-to-many relationship between Student
    and Course.

Relational Schema

The ER model is converted into the following relational schema.

Student

Student(

StudentID (PK),

StudentName,

Department

)

Course

Course(

CourseID (PK),

CourseName,

Credits

)

Enrollment

Enrollment(

EnrollmentID (PK),

StudentID (FK),

CourseID (FK),

Semester

)

SQL Implementation

1. Create Student Table

CREATE TABLE Student(

StudentID INT PRIMARY KEY,

StudentName VARCHAR(50),

Department VARCHAR(30)

);

  • 2.
    Create Course Table

CREATE TABLE Course(

CourseID INT PRIMARY KEY,

CourseName VARCHAR(50),

Credits INT

);

  • 3.
    Create Enrollment Table

CREATE TABLE Enrollment(

EnrollmentID INT PRIMARY KEY,

StudentID INT,

CourseID INT,

Semester VARCHAR(20),

FOREIGN KEY(StudentID)

REFERENCES Student(StudentID),

FOREIGN KEY(CourseID)

REFERENCES Course(CourseID)

);

Output

Tables created successfully.

Sample Data

Student

StudentID

StudentName

Department

101

Ananya

CSE

102

Rahul

ISE

Course

CourseID

CourseName

Credits

C101

DBMS

4

C102

Operating Systems

3

Enrollmen
t

EnrollmentID

StudentID

CourseID

Semester

1

101

C101

4th

2

101

C102

4th

3

102

C101

4th

Relationship Summary

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.

14.Perform normalization (1NF, 2NF, 3NF, BCNF) on a given
dataset.

Aim

To perform
Normalization (1NF, 2NF, 3NF, and BCNF)
on a given dataset to reduce
redundancy and improve data integrity.

Theory

Normalization
is the process of organizing data in a database to eliminate redundancy
and maintain consistency.

Types of Normalization

  • First Normal Form (1NF):
    Removes repeating groups and ensures atomic
    values.
  • Second Normal Form (2NF):
    Removes partial dependency.
  • Third Normal Form (3NF):
    Removes transitive dependency.
  • Boyce-Codd Normal Form (BCNF):
    Ensures every determinant is a candidate
    key.

Example Dataset (Unnormalized Form - UNF)

Suppose a college maintains the following student-course details.

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.

The above table already satisfies
1NF
because every field contains a single (atomic)
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 in
    1NF
    .
  • Remove
    partial dependency
    .

Here,

  • StudentName
    depends only on
    StudentID
    .
  • CourseName
    and
    Faculty
    depend only on
    CourseID
    .

So, split the table into three tables.

Student Table

StudentID

StudentName

StudentID

StudentName

101

Ananya

102

Rahul

103

Priya

Course Table

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 in
    2NF
    .
  • Remove
    transitive dependency
    .

In this example,

  • No non-key attribute depends on another non-key attribute.
  • Therefore, the tables are already in
    3NF
    .

3NF Tables

Student(StudentID, StudentName)

Course(CourseID, CourseName, Faculty)

Enrollment(StudentID, CourseID)

Step 4: Boyce-Codd Normal Form (BCNF)

Rule

For every functional dependency,

Determinant → Dependent

the determinant must be a
candidate key
.

In the above tables,

  • StudentID uniquely identifies StudentName.
  • CourseID uniquely identifies CourseName and Faculty.
  • (StudentID, CourseID) uniquely identifies Enrollment.

Hence, all relations satisfy
BCNF
.

Final Relational Schema

Student(

StudentID (PK),

StudentName

)

Course(

CourseID (PK),

CourseName,

Faculty

)

Enrollment(

StudentID (FK),

CourseID (FK)

)

Summary

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

Thus, the given dataset was successfully normalized from
UNF to 1NF, 2NF, 3NF, and
BCNF
. The final normalized schema eliminates redundancy, minimizes update
anomalies, and improves data consistency.

15.Demonstrate transaction processing and ACID properties
using SQL commands.

Aim

To demonstrate
Transaction Processing
and the
ACID (Atomicity, Consistency,
Isolation, Durability)
properties using SQL commands.

Theory

A
Transaction
is a sequence of one or more SQL operations executed as a single unit of
work.

A transaction is completed using:

  • COMMIT
    –
    Saves all changes permanently.
  • ROLLBACK
    –
    Undoes all changes made during the transaction.

A transaction follows the
ACID
properties.

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

CREATE TABLE BankAccount (

AccountNo INT PRIMARY KEY,

CustomerName VARCHAR(30),

Balance DECIMAL(10,2)

);

Output

Table BankAccount created successfully.

Insert Records

INSERT INTO BankAccount VALUES

(101,'Ananya',5000),

(102,'Rahul',3000);

Output

2 rows inserted successfully.

BankAccount Table

AccountNo

CustomerName

Balance

101

Ananya

5000

102

Rahul

3000

Transaction Example using COMMIT

Transfer
₹
1000
from Ananya's account to Rahul's account.

Step 1: Start Transaction

START TRANSACTION;

Step 2: Deduct Amount

UPDATE BankAccount

SET Balance = Balance - 1000

WHERE AccountNo = 101;

Step 3: Add Amount

UPDATE BankAccount

SET Balance = Balance + 1000

WHERE AccountNo = 102;

Step 4: Save Changes

COMMIT;

Output

Transaction committed successfully.

BankAccount Table after COMMIT

AccountNo

CustomerName

Balance

101

Ananya

4000

102

Rahul

4000

Transaction Example using ROLLBACK

Suppose a wrong amount is deducted from Ananya's account.

Step 1: Start Transaction

START TRANSACTION;

Step 2: Update Balance

UPDATE BankAccount

SET Balance = Balance - 500

WHERE AccountNo = 101;

Step 3: Cancel the Transaction

ROLLBACK;

Output

Transaction rolled back successfully.

BankAccount Table after ROLLBACK

AccountNo

CustomerName

Balance

101

Ananya

4000

102

Rahul

4000

Explanation:
Since the transaction was rolled back, the deduction of ₹500 was cancelled,
and the account balances remained unchanged.

Demonstration of ACID Properties

1. Atomicity

Both debit and credit operations are completed together. If one operation fails, the entire
transaction is rolled back.

2. Consistency

The total balance remains consistent before and after the transaction.

Before Transfer:

5000 + 3000 = 8000

After Transfer:

4000 + 4000 = 8000

3. Isolation

Multiple transactions can occur simultaneously without affecting each other.

4. Durability

Once the COMMIT command is executed, the updated balances are permanently stored
in the database.

Summary of Transaction Commands

SQL Command

Purpose

START TRANSACTION

Begins a transaction

COMMIT

Saves changes permanently

ROLLBACK

Cancels all changes made during the transaction

Result

Thus, transaction processing was successfully demonstrated using
START
TRANSACTION
,
COMMIT
, and
ROLLBACK
commands. The experiment also
verified the
ACID properties
—
Atomicity, Consistency, Isolation, and Durability
—
ensuring reliable and consistent database operations.