SWAMINARAYANA SIDDHANTA INSTITUTE OF TECHNOLOGY,
NAGPUR
COMPUTER ENGINEERING DEPARTMENT
Session: 2024-2025
Semester: 5th Sem B.Tech (Third Year)
Subject: Database System LAB
Student Name :……………………………….
Roll. No. :……………………………….
Enrollment No :……………………………….
Practical Incharge :………………………………
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
SWAMINARAYANA SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
CERTIFICATE
Date: Signature of Teacher
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
INDEX
Page Date of Date of
Sr. Remark Signature
List of Experiments No. Performance Submission
No.
To design and implement a database
schema for a Student Management
1
Application using SQL.
(Defining Schema for Applications).
To understand and perform
2 operations of creating tables,
renaming tables, applying constraints
(Primary Key, Foreign Key, Not
Null), and inserting data into a table
using SQL.
To study and perform grouping of
3 data, aggregate functions, and Oracle
functions (mathematical and character
functions) using SQL.
To understand and implement Sub-
4 queries, Set operations, and Joins
using SQL.
To create a database and write SQL
5 & PL/SQL queries to retrieve
information from the database.
To study and implement Triggers
6
and Cursors in SQL/PLSQL.
10
Date: Signature of Teacher
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
EXPERIMENT 1
AIM:
To design and implement a database schema for a Student Management Application using SQL.
(Defining Schema for Applications).
THEORY:
- A Database Schema defines the structure of a database, including tables, fields, data types, and
relationships.
- Schema helps in organizing application data in a systematic way.
- For a Student Management Application, we need to store information about students, courses, and
enrollments.
- Relationships:
• One Student → Many Enrollments
• One Course → Many Enrollments
PROGRAM:
-- Create Database
CREATE DATABASE StudentApp;
-- Use Database
USE StudentApp;
-- Table: Student
CREATE TABLE Student (
student_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) UNIQUE NOT NULL,
age INT CHECK (age > 15)
);
-- Table: Course
CREATE TABLE Course (
course_id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
credits INT CHECK (credits >= 1 AND credits <= 5)
);
-- Table: Enrollment
CREATE TABLE Enrollment (
enroll_id INT PRIMARY KEY AUTO_INCREMENT,
student_id INT,
course_id INT,
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
grade CHAR(2),
FOREIGN KEY (student_id) REFERENCES Student(student_id),
FOREIGN KEY (course_id) REFERENCES Course(course_id)
);
-- Insert Sample Data
INSERT INTO Student (name, email, age) VALUES
('Rahul Sharma', '
[email protected]', 20),
('Priya Verma', '
[email protected]', 19);
INSERT INTO Course (title, credits) VALUES
('Database Systems', 4),
('Human Computer Interaction', 3);
INSERT INTO Enrollment (student_id, course_id, grade) VALUES
(1, 1, 'A'),
(2, 2, 'B');
-- Display Student with their Courses
SELECT s.name, c.title, e.grade
FROM Enrollment e
JOIN Student s ON e.student_id = s.student_id
JOIN Course c ON e.course_id = c.course_id;
OUTPUT:
Name Course Grade
Rahul Sharma Database Systems A
Priya Verma Human Computer B
Interaction
CONCLUSION:
In this practical, we successfully designed and implemented a database schema for a Student
Management Application.
We created tables for Student, Course, and Enrollment with appropriate constraints and relationships.
By inserting sample data and executing queries, we understood how to:
Define schema for an application.
Date: Signature of Teacher
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
EXPERIMENT 2
AIM:
To understand and perform operations of creating tables, renaming tables, applying constraints (Primary
Key, Foreign Key, Not Null), and inserting data into a table using SQL.
THEORY:
Creating Table: Used to define a new table with fields, datatypes, and constraints.
Renaming Table: SQL command RENAME TABLE old_name TO new_name; is used to change a
table’s name.
Constraints:
PRIMARY KEY → uniquely identifies each row.
FOREIGN KEY → establishes relationship between tables.
NOT NULL → ensures column cannot store NULL values.
Data Insertion: INSERT INTO table_name (columns) VALUES (values); is used to insert records
into a table.
PROGRAM:
-- Create Database
CREATE DATABASE CollegeDB;
-- Use Database
USE CollegeDB;
-- Create Student Table with constraints
CREATE TABLE Student (
student_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) UNIQUE NOT NULL
);
-- Create Department Table
CREATE TABLE Department (
dept_id INT PRIMARY KEY AUTO_INCREMENT,
dept_name VARCHAR(50) NOT NULL
);
-- Create Enrollment Table with Foreign Key
CREATE TABLE Enrollment (
enroll_id INT PRIMARY KEY AUTO_INCREMENT,
student_id INT,
dept_id INT,
FOREIGN KEY (student_id) REFERENCES Student(student_id),
FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
);
-- Rename Table Department to Dept
RENAME TABLE Department TO Dept;
-- Insert Records into Student
INSERT INTO Student (name, email) VALUES
('Rahul Sharma', '
[email protected]'),
('Priya Verma', '
[email protected]');
-- Insert Records into Dept
INSERT INTO Dept (dept_name) VALUES
('Computer Science'),
('Information Technology');
-- Insert Records into Enrollment
INSERT INTO Enrollment (student_id, dept_id) VALUES
(1, 1),
(2, 2);
-- Display all records from Enrollment with Join
SELECT s.name, d.dept_name
FROM Enrollment e
JOIN Student s ON e.student_id = s.student_id
JOIN Dept d ON e.dept_id = d.dept_id;
OUTPUT:
Name Department
Rahul Sharma Computer Science
Priya Verma Information Technology
CONCLUSION:
In this practical, we created multiple tables with constraints like Primary Key, Foreign Key, and Not Null.
We also learned how to rename a table using the RENAME command.
By inserting sample data and retrieving results using joins, we understood how constraints ensure data
integrity and maintain proper relationships between tables.
Date: Signature of Teacher:
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
EXPERIMENT 3
AIM:
To study and perform grouping of data, aggregate functions, and Oracle functions (mathematical and
character functions) using SQL.
THEORY:
Grouping Data:
o The GROUP BY clause is used to arrange identical data into groups.
Aggregate Functions:
o Functions that operate on a group of rows and return a single value.
o Examples: COUNT(), SUM(), AVG(), MIN(), MAX().
Oracle Functions:
o Mathematical Functions: ABS(), ROUND(), CEIL(), FLOOR(), POWER().
o Character Functions: UPPER(), LOWER(), INITCAP(), LENGTH(), SUBSTR().
PROGRAM:
-- Create Employee Table
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10,2)
);
-- Insert Records
INSERT INTO Employee VALUES (1, 'Rahul Sharma', 'IT', 50000);
INSERT INTO Employee VALUES (2, 'Priya Verma', 'HR', 45000);
INSERT INTO Employee VALUES (3, 'Amit Kumar', 'IT', 60000);
INSERT INTO Employee VALUES (4, 'Neha Singh', 'Finance', 55000);
INSERT INTO Employee VALUES (5, 'Karan Patel', 'HR', 40000);
-- 1. Grouping Data: Find average salary department-wise
SELECT department, AVG(salary) AS avg_salary
FROM Employee
GROUP BY department;
-- 2. Aggregate Functions: Find highest and lowest salary
SELECT MAX(salary) AS highest, MIN(salary) AS lowest
FROM Employee;
-- 3. Mathematical Functions
SELECT name, salary,
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
ROUND(salary/3,2) AS rounded_salary,
CEIL(salary/3) AS ceil_value,
FLOOR(salary/3) AS floor_value
FROM Employee;
-- 4. Character Functions
-- Oracle → INITCAP(name) ; MySQL equivalent → CONCAT(UPPER(LEFT(name,1)),
LOWER(SUBSTRING(name,2)))
SELECT name,
UPPER(name) AS upper_name,
LOWER(name) AS lower_name,
CONCAT(UPPER(LEFT(name,1)), LOWER(SUBSTRING(name,2))) AS initcap_name,
LENGTH(name) AS name_length,
SUBSTRING(name,1,5) AS first_five_chars
FROM Employee;
OUTPUT:
1. Grouping Data (Average Salary by Department):
Department Avg_Salary
IT 55000.00
HR 42500.00
Finance 55000.00
2. Aggregate Functions:
Highest Lowest
60000.00 40000.00
3. Mathematical Functions (for 1 employee):
Name Salary Rounded_Salary Ceil_Value Floor_Value
Rahul Sharma 50000.00 16666.67 16667 16666
4. Character Functions (for 1 employee):
Name Upper_Name Lower_Name Initcap_Name Name_Length First_Five_Chars
Rahul Sharma RAHUL SHARMA rahul sharma Rahul sharma 12 Rahul
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
CONCLUSION:
We learned to group data, use aggregate functions, and apply mathematical & character functions in
SQL for effective data analysis and formatting.
Date: Signature of Teacher
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
EXPERIMENT 4
AIM:
To understand and implement Sub-queries, Set operations, and Joins using SQL.
THEORY:
Sub-queries: A query inside another query, used to filter or fetch results.
Example: SELECT name FROM Employee WHERE salary > (SELECT AVG(salary) FROM
Employee);
Set Operations: Combine results of two or more queries.
o UNION → combines unique results.
o UNION ALL → combines all including duplicates.
o INTERSECT → common results.
o MINUS (Oracle) / EXCEPT (MySQL) → results in first query but not in second.
Joins: Used to combine data from multiple tables.
o INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN.
PROGRAM:
-- Drop old tables if they exist (to avoid error on re-run)
DROP TABLE IF EXISTS Employee;
DROP TABLE IF EXISTS Department;
-- Create Department Table
CREATE TABLE Department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);
-- Create Employee Table
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
name VARCHAR(50),
dept_id INT,
salary DECIMAL(10,2),
FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
);
-- Insert Departments
INSERT INTO Department VALUES
(1, 'IT'),
(2, 'HR'),
(3, 'Finance'),
(4, 'Marketing');
-- Insert Employees
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
INSERT INTO Employee VALUES
(1, 'Rahul Sharma', 1, 50000),
(2, 'Priya Verma', 2, 45000),
(3, 'Amit Kumar', 1, 60000),
(4, 'Neha Singh', 3, 55000),
(5, 'Karan Patel', 2, 40000);
---------------------------------------------------
-- 1. Sub-query: Employees earning more than average salary
---------------------------------------------------
SELECT name, salary
FROM Employee
WHERE salary > (SELECT AVG(salary) FROM Employee);
---------------------------------------------------
-- 2. Set Operation: Employees from IT and HR departments
-- (Using UNION in MySQL)
---------------------------------------------------
SELECT name FROM Employee WHERE dept_id = 1
UNION
SELECT name FROM Employee WHERE dept_id = 2;
---------------------------------------------------
-- 3. Inner Join: Employee with Department
---------------------------------------------------
SELECT e.name, e.salary, d.dept_name
FROM Employee e
INNER JOIN Department d ON e.dept_id = d.dept_id;
---------------------------------------------------
-- 4. Left Join: All Departments with Employees (if any)
---------------------------------------------------
SELECT d.dept_name, e.name
FROM Department d
LEFT JOIN Employee e ON d.dept_id = e.dept_id;
OUTPUT:
1. Sub-query (Employees above avg salary):
Name Salary
Amit Kumar 60000.00
Neha Singh 55000.00
2. Set Operation (Employees from IT & HR):
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
Name
Rahul Sharma
Amit Kumar
Priya Verma
Karan Patel
3. Inner Join (Employee with Dept):
Name Salary Dept_Name
Rahul Sharma 50000.00 IT
Priya Verma 45000.00 HR
Amit Kumar 60000.00 IT
Neha Singh 55000.00 Finance
Karan Patel 40000.00 HR
4. Left Join (All Depts with Employees):
Dept_Name Name
IT Rahul Sharma
IT Amit Kumar
HR Priya Verma
HR Karan Patel
Finance Neha Singh
Marketing NULL
CONCLUSION:
We learned how to use sub-queries, set operations, and joins to combine and analyze data effectively in
SQL.
Date: Signature of Teacher
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
EXPERIMENT 5
AIM:
To create a database and write SQL & PL/SQL queries to retrieve information from the database.
THEORY:
Database Creation: A database is created using CREATE DATABASE command in SQL.
SQL Queries: SQL is used to retrieve data using SELECT, filtering using WHERE, and combining
tables using JOIN.
PL/SQL: Procedural Language SQL (in Oracle) allows the use of variables, loops, and conditions
inside SQL. It is used to write blocks for more powerful querying and reporting.
PROGRAM:
-- 1. Database Creation (MySQL)
CREATE DATABASE CollegeDB;
USE CollegeDB;
-- 2. Table Creation
CREATE TABLE Student (
student_id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
dept VARCHAR(50)
);
CREATE TABLE Marks (
mark_id INT PRIMARY KEY,
student_id INT,
subject VARCHAR(50),
marks INT,
FOREIGN KEY (student_id) REFERENCES Student(student_id)
);
-- 3. Insert Records
INSERT INTO Student VALUES
(1, 'Rahul Sharma', 20, 'IT'),
(2, 'Priya Verma', 19, 'HR'),
(3, 'Amit Kumar', 21, 'Finance');
INSERT INTO Marks VALUES
(101, 1, 'DBMS', 85),
(102, 1, 'HCI', 78),
(103, 2, 'DBMS', 88),
(104, 3, 'DBMS', 90);
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
---------------------------------------------------
-- 4. SQL Queries to retrieve information
---------------------------------------------------
-- Show all students
SELECT * FROM Student;
-- Show students with marks above 80
SELECT s.name, m.subject, m.marks
FROM Student s
JOIN Marks m ON s.student_id = m.student_id
WHERE m.marks > 80;
---------------------------------------------------
-- 5. PL/SQL Block (Oracle Only)
---------------------------------------------------
SET SERVEROUTPUT ON;
DECLARE
v_name Student.name%TYPE;
v_marks Marks.marks%TYPE;
BEGIN
SELECT s.name, m.marks
INTO v_name, v_marks
FROM Student s
JOIN Marks m ON s.student_id = m.student_id
WHERE m.marks = (SELECT MAX(marks) FROM Marks);
DBMS_OUTPUT.PUT_LINE('Topper: ' || v_name || ' scored ' || v_marks);
END;
/
OUTPUT:
SQL Queries:
Students with marks above 80
Name Subject Marks
Rahul Sharma DBMS 85
Priya Verma DBMS 88
Amit Kumar DBMS 90
PL/SQL Block:
Topper: Amit Kumar scored 90
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
CONCLUSION:
In this practical, we created a database with tables, inserted sample records, retrieved information using
SQL queries, and wrote a PL/SQL block to display the topper. This demonstrates the integration of SQL and
PL/SQL for efficient data management and retrieval.
Date: Signature of Teacher
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
EXPERIMENT 6
AIM:
To study and implement Triggers and Cursors in SQL/PLSQL.
THEORY:
Trigger:
A trigger is a stored PL/SQL block that automatically executes (fires) when a specified event occurs
on a table (like INSERT, UPDATE, or DELETE).
Example: maintaining log records whenever data is updated.
Cursor:
A cursor is a pointer to the result of a SQL query.
Implicit Cursor: Automatically created by Oracle for single-row queries.
Explicit Cursor: Defined by the programmer for handling multi-row queries.
PROGRAM:
Trigger
👉 Task: Whenever an employee’s salary is updated, store the old and new salary in an Audit table.
-- Employee Table
CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50),
Salary NUMBER(10,2)
);
-- Audit Table
CREATE TABLE Salary_Audit (
AuditID INT PRIMARY KEY,
EmpID INT,
OldSalary NUMBER(10,2),
NewSalary NUMBER(10,2),
ChangeDate DATE
);
-- Sequence for Audit ID
CREATE SEQUENCE audit_seq START WITH 1 INCREMENT BY 1;
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
-- Trigger
CREATE OR REPLACE TRIGGER trg_salary_audit
AFTER UPDATE OF Salary ON Employee
FOR EACH ROW
BEGIN
INSERT INTO Salary_Audit (AuditID, EmpID, OldSalary, NewSalary, ChangeDate)
VALUES (audit_seq.NEXTVAL, :OLD.EmpID, :OLD.Salary, :NEW.Salary, SYSDATE);
END;
/
Execution:
INSERT INTO Employee VALUES (1, 'Rahul', 40000);
INSERT INTO Employee VALUES (2, 'Sneha', 60000);
UPDATE Employee SET Salary = 70000 WHERE EmpID = 2;
👉 Result: One new record will be inserted in Salary_Audit.
Cursor
👉 Task: Display all employees whose salary is greater than 50,000.
SET SERVEROUTPUT ON;
DECLARE
CURSOR emp_cursor IS
SELECT EmpID, EmpName, Salary FROM Employee WHERE Salary > 50000;
v_id Employee.EmpID%TYPE;
v_name Employee.EmpName%TYPE;
v_salary Employee.Salary%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_id, v_name, v_salary;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('ID: ' || v_id || ', Name: ' || v_name || ', Salary: ' || v_salary);
END LOOP;
CLOSE emp_cursor;
END;
/
OUTPUT:
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR
ID: 2, Name: Sneha, Salary: 70000
CONCLUSION:
Trigger is useful for automatic actions like audit logging.
Cursor is useful when we want to process multiple rows one at a time.
Date: Signature of Teacher
SWAMINARAYAN SIDDHANTA INSTITUTE OF TECHNOLOGY, NAGPUR