0% found this document useful (0 votes)
13 views

DBMS All PR

The document contains a series of practical exercises related to Database Management Systems (DBMS), covering various SQL commands such as creating databases, tables, and inserting, updating, and deleting records. It also includes examples of using aggregate functions, constraints, joins, subqueries, and user privileges. Each practical focuses on different aspects of SQL and database operations, providing a comprehensive overview of DBMS functionalities.

Uploaded by

komalpatil23.cs
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)
13 views

DBMS All PR

The document contains a series of practical exercises related to Database Management Systems (DBMS), covering various SQL commands such as creating databases, tables, and inserting, updating, and deleting records. It also includes examples of using aggregate functions, constraints, joins, subqueries, and user privileges. Each practical focuses on different aspects of SQL and database operations, providing a comprehensive overview of DBMS functionalities.

Uploaded by

komalpatil23.cs
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/ 13

DBMS Practical’s All

Practical 01
CREATE DATABSE Aniket;

Use Aniket;

CREATE TABLE Employees (EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50),


LastName VARCHAR(50), Age INT,

Department VARCHAR(50), Salary DECIMAL(10, 2));

insert into Employees values (101,'Aniket', 'Suryawanshi',21,'CSE',600000),


(102,'Adesh','Madhurkar',22,'IT',700000), (103,'Purva','Meherar',23,'AI_ML',800000),
(104,'Siddhi','Nikam',21,'CSE',900000),(105,'Yojana','Waghulde',22,'IT',500000);

-- Alter Table Add Column

ALTER TABLE Employees ADD Email VARCHAR(100);

-- Drop Column

ALTER TABLE Employees DROP COLUMN Age ;

-- Modify Column

ALTER TABLE Employees MODIFY COLUMN Salary DECIMAL(12, 2);

-- Drop Table

DROP TABLE Employees;


Practical 02

CREATE TABLE Employes (

EmployeeID INT PRIMARY KEY,

FirstName VARCHAR(50),

LastName VARCHAR(50),Age INT, Department VARCHAR(50), Salary DECIMAL(10, 2));

-- Insert values

INSERT INTO Employes (EmployeeID, FirstName, LastName, Age, Department, Salary)

VALUES (2, 'suraj', 'Mane', 28, 'IT', 60000.00), (3, 'Adesh', 'Madhurkar', 35, 'Marketing',
55000.00), (4, 'Yojana', 'Waghulde', 40, 'Finance', 70000.00);

UPDATE Employees SET Salary = 65000, Department = 'Management' WHERE


EmployeeID = 2;

-- Deleting a specific record

DELETE FROM Employees WHERE EmployeeID = 3;

-- Deleting all records (be cautious with this)

DELETE FROM Employees;


Practical 03
CREATE TABLE Employees0 (EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50),
LastName VARCHAR(50), Age INT, Department VARCHAR(50), Salary DECIMAL(10, 2));

INSERT INTO Employees0 (EmployeeID, FirstName, LastName, Age, Department,


Salary) VALUES (2, 'Adesh', 'Madhurkar', 28, 'IT', 60000.00), (3, 'Suraj', 'Mane', 35,
'Marketing', 55000.00), (4, 'Anna', 'Davis', 40, 'Finance', 70000.00);

SELECT ABS(-15); -- Result: 15 -- ABS() returns the absolute value

SELECT ROUND(123.4567, 2); -- ROUND() rounds the number to 2 decimal places

SELECT CEIL(123.45); -- CEIL() returns the smallest int >= to the number

SELECT FLOOR(123.45); -- FLOOR() returns the largest integer <= to the number

-- Aggregate Functions

SELECT COUNT(*) FROM Employees; -- Returns the total number of employees

SELECT SUM(Salary) FROM Employees; -- Returns the sum of all employee salaries

SELECT AVG(Salary) FROM Employees; -- Returns the average salary

SELECT MAX(Salary) FROM Employees; -- Returns the highest salary

SELECT MIN(Salary) FROM Employees; -- Returns the lowest salary

-- Character Functions

SELECT CONCAT(FirstName, ' ', LastName) AS FullName FROM Employees0;

SELECT LENGTH(FirstName) FROM Employees0; -- Returns the length of the first name

SELECT UPPER(FirstName) FROM Employees0; -- Converts FirstName to uppercase

SELECT LOWER(LastName) FROM Employees0; -- Converts LastName to lowercase

SELECT SUBSTRING(FirstName, 1, 3) FROM Employees0; -- Extracts first 3 characters


of FirstName

-- Conversion Functions

-- CAST() converts a data type to another type


-- Date Functions

-- CURRENT_DATE() returns the current date

SELECT CURRENT_DATE(); -- Result: 2025-04-09 (example)

-- NOW() returns the current date and time

SELECT NOW(); -- Result: 2025-04-09 14:30:00 (example)

-- DATEADD() adds an interval (e.g., 30 days) to a date

-- SELECT DATEADD(DAY, 30, HireDate) FROM Employee; -- Adds 30 days to each


employee's HireDate

-- DATEDIFF() calculates the di erence between two dates

SELECT DATEDIFF(CURRENT_DATE, HireDate) FROM Employee; -- Returns the number


of days since the employee's hire date

-- YEAR(), MONTH(), and DAY() extract parts of a date

SELECT YEAR(HireDate) FROM Employee; -- Extracts the year from the HireDate

SELECT MONTH(HireDate) FROM Employee; -- Extracts the month from the HireDate

SELECT DAY(HireDate) FROM Employee; -- Extracts the day from the HireDate
Practical 04
CREATE TABLE Employeee12 (EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50),
LastName VARCHAR(50), Age INT, Department VARCHAR(50), Salary DECIMAL(10, 2));

INSERT INTO Employeee12 (EmployeeID, FirstName, LastName, Age, Department,


Salary)

VALUES (2, 'Adesh', 'Madhurkar', 28, 'IT', 60000.00), (3, 'Suraj', 'Mane', 35, 'HR', 55000.00),
(4, 'Anna', 'Davis', 40, 'Finance', 70000.00);

-- Arithmatic Operators

SELECT 10 + 5; -- Result: 15

SELECT 10 - 5; -- Result: 5

SELECT 10 * 5; -- Result: 50

SELECT 10 / 5; -- Result: 2

SELECT 10 % 3; -- Result: 1(Remainder)

-- Logical Operators

-- AND operator: Both conditions must be true

SELECT * FROM Employeee12 WHERE Age > 30 AND Department = 'HR';

-- OR operator: Either condition must be true

SELECT * FROM Employeee12 WHERE Department = 'HR' OR Department = 'Finance';

-- NOT operator: Reverses the condition

SELECT * FROM Employeee12 WHERE NOT Department = 'HR'; -- Returns all


employees except those in HR

-- Comparision operators

-- Equal to (=)

SELECT * FROM Employeee12 WHERE Department = 'HR';

-- Not equal to (<> or !=)

SELECT * FROM Employeee12 WHERE Department <> 'HR';

-- Greater than (>)

SELECT * FROM Employeee12 WHERE Salary > 50000;


-- Less than (<)

SELECT * FROM Employeee12 WHERE Age < 30;

-- Greater than or equal to (>=)

SELECT * FROM Employeee12 WHERE Salary >= 60000;

-- Less than or equal to (<=)

SELECT * FROM Employeee12 WHERE Age <= 40;

-- BETWEEN: Selects values within a range

SELECT * FROM Employeee12 WHERE Salary BETWEEN 40000 AND 60000;

-- LIKE: Used for pattern matching

SELECT * FROM Employeee12 WHERE FirstName LIKE 'J%'; -- names starting with 'J'

-- IN: Matches any value in a list

SELECT * FROM Employeee12

WHERE Department IN ('HR', 'IT', 'Finance');

-- Set Operations

-- UNION: Combines two queries and removes duplicates

SELECT FirstName FROM Employeee12 WHERE Department = 'HR'

UNION

SELECT FirstName FROM Employeee12 WHERE Department = 'Finance';

-- UNION ALL: Combines two queries and keeps duplicates

SELECT FirstName FROM Employeee12 WHERE Department = 'HR'

UNION ALL

SELECT FirstName FROM Employeee12 WHERE Department = 'Finance';


Practical 05
CREATE TABLE Employ1 (EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50),

LastName VARCHAR(50), Age INT, DepartmentID int, Department VARCHAR(50), Salary


DECIMAL(10, 2));

CREATE TABLE Departmentss1(DepartmentName varchar(20),DepartmentID int);

insert into Departmentss1 values("IT",101), ("HR",102), ("Finance",103);

INSERT INTO Employ1 (EmployeeID, FirstName, LastName, Age, Department, Salary)

VALUES (2, 'Adesh', 'Madhurkar', 28, 'IT', 60000.00), (3, 'Suraj', 'Mane', 35, 'HR', 55000.00),
(4, 'Anna', 'Davis', 40, 'Finance', 70000.00);

-- INNER JOIN

SELECT Employ1.FirstName, Employ1.LastName, Departmentss1.DepartmentName

FROM Employ1 INNER JOIN Departmentss1

ON Employ1.DepartmentID = Departmentss1.DepartmentID;

-- outer join

SELECT Employ1.FirstName, Employ1.LastName, Departmentss1.DepartmentName

FROM Employ1 LEFT JOIN Departmentss1

ON Employ1.DepartmentID = Departmentss1.DepartmentID;

-- Natiural join

SELECT Employ1.FirstName, Employ1.LastName, Departmentss1.DepartmentName

FROM Employ1 NATURAL JOIN Departmentss1;

-- CROSS JOIN

SELECT Employ1.FirstName, Departmentss1.DepartmentName

FROM Employ1 CROSS JOIN Departmentss1;


Practical 06
create table sales(OrderID int, ProductID int,Quantity int,SalesDate int);

insert into sales values (1,101,3,2025-4-1), (2,102,4,2025-4-1), (3,103,5,2025-4-2),


(4,104,6,2025-4-3), (5,105,7,2025-4-2);

-- Group By & Having clause

SELECT ProductID, SUM(Quantity) AS TotalQuantity FROM Sales GROUP BY ProductID


HAVING SUM(Quantity) > 4;

-- Order By Clause

SELECT ProductID, Quantity, SalesDate FROM Sales ORDER BY ProductID ASC,


Quantity DESC;

-- Indexing

CREATE TABLE Employy (EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50),

LastName VARCHAR(50), Age INT, Department VARCHAR(50), Salary DECIMAL(10, 2));

INSERT INTO Employy (EmployeeID, FirstName, LastName, Age, Department, Salary)

VALUES (2, 'Jane', 'Smith', 28, 'IT', 60000.00), (3, 'Sam', 'Brown', 35, 'Marketing',
55000.00), (4, 'Anna', 'Davis', 40, 'Finance', 70000.00);

CREATE INDEX idx_lastname ON Employy (LastName);

SELECT * FROM Employy WHERE LastName = 'Doe';

-- Composite Index

CREATE INDEX idx_dept_lastname ON Employy (Department, LastName);

SELECT * FROM Employy WHERE Department = 'IT' AND LastName = 'Smith';


Practical 07
create table Employeese11 (EmployeeID int,Name varchar(20),Department
varchar(20),salary decimal(10,2));

insert into Employeese11 values(1,"john","HR",50000), (2,"jane","IT",60000),


(3,"Mane","Finance",70000), (4,"Suraj","It",75000);

-- Single Row Subquery

SELECT Name, Salary FROM Employeese11 WHERE Salary > (SELECT Salary FROM
Employeese11 WHERE Name = 'Mane');

-- Multiple-Row Subquery

SELECT Name, Department FROM Employeese11 WHERE Department IN (SELECT


Department FROM Employeese11 GROUP BY Department HAVING AVG(Salary) >
60000);

-- 3: Correlated Subquery

SELECT Name, Department, Salary FROM Employeese11 e1 WHERE Salary > (SELECT
AVG(Salary) FROM Employeese11 e2 WHERE e1.Department = e2.Department);

-- Views

CREATE VIEW EmployeeInfo1 AS SELECT Name, Department FROM Employeese11;

SELECT * FROM EmployeeInfo1;

-- 2: Creating a View with Aggregate Functions

CREATE VIEW Department Salaries1 ASSELECT Department, SUM(Salary) AS


TotalSalary FROM Employeese11 GROUP BY Department;

SELECT * FROM DepartmentSalaries1;

-- 3: Using Views for Security

CREATE VIEW EmployeeWithoutSalary1 AS SELECT Name, Department FROM


Employeese11;

SELECT * FROM EmployeeWithoutSalary1;


Practical 08
-- 1. NOT NULL Constraint

CREATE TABLE Employees1 (EmployeeID INT NOT NULL, Name VARCHAR(100) NOT
NULL, Department VARCHAR(50));

-- 2. UNIQUE Constraint

CREATE TABLE Employees2(EmployeeID INT UNIQUE, Name VARCHAR(100), Email


VARCHAR(100) UNIQUE);

-- PRIMARY KEY CONSTRAINTS

CREATE TABLE Employees3 (EmployeeID INT PRIMARY KEY, Name VARCHAR(100),


Department VARCHAR(50));

-- 4. FOREIGN KEY Constraint

CREATE TABLE Departments1 (DepartmentID INT PRIMARY KEY, DepartmentName


VARCHAR(100));

CREATE TABLE Employees4 (EmployeeID INT PRIMARY KEY, Name VARCHAR(100),


DepartmentID INT, FOREIGN KEY (DepartmentID) REFERENCES
Departments1(DepartmentID));

-- Check CONSTRAINTS

CREATE TABLE Employees5 (EmployeeID INT PRIMARY KEY, Name VARCHAR(100),


Salary DECIMAL(10, 2) CHECK (Salary > 0));

-- Index Constraints

CREATE INDEX idx_name ON Employees1 (Name);

-- Miultiple Constraints

CREATE TABLE Employees7 (EmployeeID INT PRIMARY KEY, Name VARCHAR(100) NOT
NULL, Email VARCHAR(100) UNIQUE, DepartmentID INT,

Salary DECIMAL(10, 2) CHECK (Salary > 0), DATE DEFAULT CURRENT_DATE, FOREIGN
KEY (DepartmentID) REFERENCES Departments1(DepartmentID));
Practical 09
USE practicals;

-- Step 2: Drop Table if Already Exists

DROP TABLE IF EXISTS students;

-- Step 3: Create the Students Table

CREATE TABLE students1 (id INT PRIMARY KEY, name VARCHAR(50), age INT);

-- Step 4: Start of First Transaction (Inserting Records and Committing)

BEGIN;

-- Inserting Student Records

INSERT INTO students1 (id, name, age) VALUES (1, 'Adesh', 18);

INSERT INTO students1 (id, name, age) VALUES (2, 'Suraj', 21);

INSERT INTO students1 (id, name, age) VALUES (3, 'Aditya', 22);

-- Committing the First Transaction

COMMIT;

-- Step 5: Displaying Records After First Transaction

SELECT * FROM students1;

-- Step 6: Start of Second Transaction (Using Savepoint and Rollback)

BEGIN;

-- Inserting Additional Records

INSERT INTO students1 (id, name, age) VALUES (4, 'Yojana', 20);

INSERT INTO students1 (id, name, age) VALUES (5, 'Sakshi', 19);

-- Creating Savepoint sp1

SAVEPOINT sp1;

-- Updating a Record

UPDATE students1 SET age = 23 WHERE name = 'Vishal';


-- Creating Another Savepoint sp2

SAVEPOINT sp2;

-- Deleting a Record

DELETE FROM students WHERE name = 'Sakshi';

-- Rolling Back to Savepoint sp2 (Restores Deleted Record)

ROLLBACK TO sp2;

-- Rolling Back to Savepoint sp1 (Reverts Age Update)

ROLLBACK TO sp1;

-- Committing the Final State of Second Transaction

COMMIT;

-- Step 7: Displaying Final Records

SELECT * FROM students1;


Practical 10
use practicals;

-- Create a new user 'app_user' that can connect from localhost

CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'app_password';

-- Create another user 'report_user' that can connect from any host (%)

CREATE USER 'report_user'@'%' IDENTIFIED BY 'report123';

-- (Later, if needed) Delete a user

DROP USER 'old_user'@'localhost';

-- Grant the 'CREATE TABLE' privilege to 'app_user' on the 'my_india_app_db'


database

GRANT CREATE tablespace ON practicals.* TO 'app_user'@'localhost';

-- Grant SELECT privilege on all tables in 'my_india_app_db' to 'report_user'

GRANT SELECT ON practicals.* TO 'report_user'@'%';

-- Create a role 'data_modifier' with INSERT, UPDATE, and DELETE privileges

CREATE ROLE 'data_modifier';

GRANT INSERT, UPDATE, DELETE ON practicals.* TO 'data_modifier';

-- Grant the 'data_modifier' role to 'app_user'

GRANT 'data_modifier' TO 'app_user'@'localhost';

-- (Later, if needed) Revoke the 'CREATE TABLE' privilege from 'app_user'

REVOKE CREATE TABLE ON my_india_app_db.* FROM 'app_user'@'localhost';

-- (Later, if needed) Revoke the 'data_modifier' role from 'app_user'

REVOKE 'data_modifier' FROM 'app_user'@'localhost';

-- (Later, if needed) Revoke the SELECT privilege from 'report_user'

REVOKE SELECT ON my_india_app_db.* FROM 'report_user'@'%';

You might also like