0% found this document useful (0 votes)
27 views31 pages

DBMS Practical File

DBMS Practical File for RGPV

Uploaded by

ankitas5912
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views31 pages

DBMS Practical File

DBMS Practical File for RGPV

Uploaded by

ankitas5912
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment No: 01

Problem Statement: Delete duplicate row from the table.


Solution:
1. Create the Employees table:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT
);
2. Insert data:
INSERT INTO Employees (ID, Name, Department, Salary) VALUES
(1, 'Ekam', 'HR', 5000),
(2, 'Lalita', 'IT', 7000),
(3, 'Ekam', 'HR', 5000),
(4, 'Ankita', 'IT', 8000);
you have a table named Employees with the following data:

Name Departmen Salary


t
1 Ekam HR 5000
2 Lalita IT 7000
3 Ekam HR 5000
4 Ankita IT 8000

3. Execute the delete queries to remove duplicates.


Query : Using GROUP BY and MIN()
DELETE FROM Employees
WHERE ID NOT IN (
SELECT MIN(ID)
FROM Employees
GROUP BY Name, Department, Salary
);

Explanation:
1. GROUP BY groups duplicates based on the relevant columns.
2. MIN(ID) keeps the first occurrence of each group.
3. All other rows (not in the MIN(ID)) are deleted.

After Execution:
The table will look like this:

I Name Departmen Salary


D t
1 Ekam HR 5000
2 Lalita IT 7000
4 Ankita IT 8000
Experiment No: 02
Problem Statement: Display the alternate row from table.
Solution:
[Link] the table:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT
);
2. Insert data:
INSERT INTO Employees (ID, Name, Department, Salary) VALUES
(1, 'Ekam ', 'HR', 5000),
(2, 'Ankita', 'IT', 7000),
(3, 'Lalita', 'IT', 8000),
(4, 'Arghya', 'HR', 6000),
(5, 'Honey', 'Finance', 9000);
you have a table named Employees with the following data:

ID Name Department Salary


1 Ekam HR 5000
2 Ankita IT 7000
3 Lalita IT 8000
4 Arghy HR 6000
a
5 Honey Finance 9000

[Link] queries to retriAnuj alternate rows.


Query : Using LIMIT and OFFSET
SELECT ID, Name, Department, Salary
FROM Employees
WHERE ID IN (
SELECT ID
FROM Employees
WHERE MOD(ID, 2) = 1
);

After Execution:
The result for odd rows:

I Name Departmen Salary


D t
1 Ekam HR 5000
3 Lalita IT 8000
5 Honey Finance 9000

For Anujn rows (% 2 = 0), the result will be:

ID Name Department Salary


2 Ankita IT 7000
4 Arghy HR 6000
a
Experiment No: 03
Problem Statement: Delete alternate row from table.
Solution:
[Link] the table:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT
);
2. Insert data:
INSERT INTO Employees (ID, Name, Department, Salary) VALUES
(1, 'Ekam ', 'HR', 5000),
(2, 'Ankit', 'IT', 7000),
(3, 'Lalita', 'IT', 8000),
(4, 'Arghya', 'HR', 6000),
(5, 'Honey', 'Finance', 9000);
you have a table named Employees with the following data:

ID Name Department Salary


1 Ekam HR 5000
2 Ankit IT 7000
3 Lalita IT 8000
4 Arghy HR 6000
a
5 Honey Finance 9000

[Link] the queries to retriAnuj alternate rows.


Query: Using MOD() or %
If the table has a sequential primary key like ID, you can use the MOD() or % operator:
DELETE FROM Employees
WHERE ID % 2 = 0; -- Deletes rows with Anujn IDs

Explanation:
 The MOD() or % operator checks if the ID is Anujn or odd.
 Rows with Anujn ID are deleted.

After Execution:
After deleting alternate rows (e.g., Anujn rows):

I Name Departmen Salary


D t
1 Ekam HR 5000
3 Lalita IT 8000
5 Honey Finance 9000
Experiment No: 04
Problem Statement: Update multiple rows in using single update statement.
Solution:
1. Create the table:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT
);
2. Insert data:
INSERT INTO Employees (ID, Name, Department, Salary) VALUES
(1, 'Ekam ', 'HR', 5000),
(2, 'Ankit', 'IT', 7000),
(3, 'Lalita', 'IT', 8000),
(4, 'Arghya', 'HR', 6000),
(5, 'Honey', 'Finance', 9000);
Assume you have a table named Employees with the following data:

ID Name Department Salary


1 Ekam HR 5000
2 Ankit IT 7000
3 Lalita IT 8000
4 Arghy HR 6000
a
5 Honey Finance 9000

[Link] the UPDATE queries to modify multiple rows in one statement.


Query: Using Static Conditions
If you want to apply the same update to multiple rows, you can use a simple condition:
UPDATE Employees
SET Department = 'General'
WHERE Department = 'HR';

Explanation:
 All employees in the HR department are moved to the General department.

After Execution:
After executing the static condition query, the table looks like this (if the condition matches):

ID Name Department Salary


1 Ekam General 5500
2 Ankit IT 7500
3 Lalita IT 8000
4 Arghy General 6500
a
5 Honey Finance 9000
Experiment No: 05
Problem Statement: Find the third highest paid and third lowest paid salary.
Solution:
[Link] the table:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT
);

2. Insert data:
INSERT INTO Employees (ID, Name, Department, Salary) VALUES
(1, 'Ekam ', 'HR', 5000),
(2, 'Ankit', 'IT', 7000),
(3, 'Lalita', 'IT', 8000),
(4, 'Arghya', 'HR', 6000),
(5, 'Honey', 'Finance', 9000),
(6, 'Anuj', 'HR', 4000);
Assume you have a table named Employees with the following data:

ID Name Department Salary


1 Ekam HR 5000
2 Ankit IT 7000
3 Lalita IT 8000
4 Arghy HR 6000
a
5 Honey Finance 9000
6 Anuj HR 4000

[Link] the queries to find the third-highest and third-lowest salaries.


Query: Without Advanced Functions
Third-highest salary:
SELECT MAX(Salary)
FROM Employees
WHERE Salary < (
SELECT MAX(Salary)
FROM Employees
WHERE Salary < (
SELECT MAX(Salary)
FROM Employees
)
);
Third-lowest salary:
SELECT MIN(Salary)
FROM Employees
WHERE Salary > (
SELECT MIN(Salary)
FROM Employees
WHERE Salary > (
SELECT MIN(Salary)
FROM Employees
)
);

Explanation:
 The inner queries progressively eliminate the highest or lowest salaries to find the third.
After Execution:
Using the table:
 Third-highest salary: 7000 (Ekam > Lalita > Ankit)
 Third-lowest salary: 6000 (Anuj < Ekam < Arghya)

Experiment No: 06
Problem Statement: Display the 3rd, 4th, 9th rows from table.
Solution:
[Link] the table:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT
);
[Link] data:
INSERT INTO Employees (ID, Name, Department, Salary) VALUES
(1, 'Ekam ', 'HR', 5000),
(2, 'Ankita', 'IT', 7000),
(3, 'Lalita', 'IT', 8000),
(4, 'Arghya', 'HR', 6000),
(5, 'Honey', 'Finance', 9000),
(6, 'Anuj', 'HR', 4000),
(7, 'Lucky', 'IT', 4500),
(8, 'Samay', 'HR', 7500),
(9, 'Henry', 'Finance', 5000);
you have a table named Employees with the following data:

ID Name Department Salary


1 Ekam HR 5000
2 Ankita IT 7000
3 Lalita IT 8000
4 Arghy HR 6000
a
5 Honey Finance 9000
6 Anuj HR 4000
7 Lucky IT 4500
8 Samay HR 7500
9 Henry Finance 5000

[Link] one of the queries above to retriAnuj the 3rd, 4th, and 9th rows.
The table has a primary key.
Query:
SELECT * FROM Employees
WHERE ID IN (3, 4, 9);

Explanation:
 This assumes that the ID column corresponds to the row positions.
 The query retriAnujs rows with ID values 3, 4, and 9.

After Execution:
For the table, the output would be:

ID Name Department Salary


3 Lalita IT 8000
4 Arghy HR 6000
a
9 Henry Finance 5000
Experiment No: 07
Problem Statement: Display the Ename, which starts with J, K, L or M.
Solution:
[Link] the table:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Ename VARCHAR(50),
Department VARCHAR(50),
Salary INT
);
2. Insert data:
INSERT INTO Employees (ID, Ename, Department, Salary) VALUES
(1, 'Ekam ', 'HR', 5000),
(2, 'Ankit', 'IT', 7000),
(3, 'Nancy', 'IT', 8000),
(4, 'Arghya', 'HR', 6000),
(5, 'Honey', 'Finance', 9000),
(6, 'Neha', 'HR', 4000),
(7, 'Lucky', 'IT', 4500),
(8, 'Samay', 'HR', 7500),
(9, 'Happy', 'Finance', 5000);
you have a table named Employees with the following data:
I Ename Departmen Salary
D t
1 Ekam HR 5000
2 Ankit IT 7000
3 Nancy IT 8000
4 Arghya HR 6000
5 Honey Finance 9000
6 Neha HR 4000
7 Lucky IT 4500
8 Samay HR 7500
9 Happy Finance 5000

[Link] the query to retriAnuj names starting with J, K, L, or M.


Query 1: Using LIKE and OR
SELECT Ename
FROM Employees
WHERE Ename LIKE 'J%'
OR Ename LIKE 'K%'
OR Ename LIKE 'L%'
OR Ename LIKE 'M%';

Explanation:
 LIKE 'J%' matches names starting with J.
 The OR operator combines conditions for K%, L%, and M%.

After Execution:
For the table, the result would be:

Ename
Ekam
Nancy
Neha
Happy
Experiment No: 08
Problem Statement: Show all employees who were hired the first half of the
month.
Solution:
[Link] the table:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
HireDate DATE
);
2. Insert data:
INSERT INTO Employees (ID, Name, Department, HireDate) VALUES
(1, 'Ekam ', 'HR', '2023-01-03'),
(2, 'Ankit', 'IT', '2023-01-18'),
(3, 'Lalita', 'IT', '2023-02-12'),
(4, 'Arghya', 'HR', '2023-02-25'),
(5, 'Honey', 'Finance', '2023-03-15'),
(6, 'Anuj', 'HR', '2023-03-20');
you have a table named Employees with the following data:

I Name Department HireDate


D
1 Ekam HR 2023-01-03
2 Ankit IT 2023-01-18
3 Lalita IT 2023-02-12
4 Arghya HR 2023-02-25
5 Honey Finance 2023-03-15
6 Anuj HR 2023-03-20

[Link] the query to filter employees hired in the first half of the month.
Query 1: Using DAY()
SELECT *
FROM Employees
WHERE DAY(HireDate) BETWEEN 1 AND 15;

Explanation:
 The DAY(HireDate) function extracts the day portion of the HireDate.
 The BETWEEN 1 AND 15 condition filters rows where the day is in the first half of the
month.

After Execution:
For the table, the result would be:

I Name Department HireDate


D
1 Ekam HR 2023-01-03
3 Lalita IT 2023-02-12
5 Honey Finance 2023-03-15
Experiment No:9
Problem Statement: Display the three record in the first row and two records in the
second row and one record in the third row in a single sql statements.
Solution:
[Link] the table:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT
);
[Link] data:
INSERT INTO Employees (ID, Name, Department, Salary) VALUES
(1, 'Ekam ', 'HR', 5000),
(2, 'Ankit', 'IT', 7000),
(3, 'Lalita', 'IT', 8000),
(4, 'Arghya', 'HR', 6000),
(5, 'Honey', 'Finance', 9000),
(6, 'Anuj', 'HR', 4000);
you have a table named Employees with the following data:

ID Name Department Salary


1 Ekam HR 5000
2 Ankit IT 7000
3 Lalita IT 8000
4 Arghy HR 6000
a
5 Honey Finance 9000
6 Anuj HR 4000

[Link] the query to display rows in the specified format.


SQL Query:
WITH RankedEmployees AS (
SELECT
*,
ROW_NUMBER() OVER (ORDER BY ID) AS row_num
FROM Employees
),
GroupedEmployees AS (
SELECT
ID,
Name,
Department,
Salary,
CASE
WHEN row_num <= 3 THEN 1 -- First three rows in Group 1
WHEN row_num <= 5 THEN 2 -- Next two rows in Group 2
WHEN row_num = 6 THEN 3 -- Last row in Group 3
END AS group_num
FROM RankedEmployees
)
SELECT group_num, STRING_AGG(Name, ', ') AS Employees
FROM GroupedEmployees
GROUP BY group_num
ORDER BY group_num;

Explanation:
1. ROW_NUMBER():
o Assigns a sequential number (row_num) to each row, ordered by ID.

2. CASE in GroupedEmployees:
o Groups rows based on their position:

 First 3 rows → Group 1.


 Next 2 rows → Group 2.
 Last row → Group 3.
3. STRING_AGG:
o Combines the names of employees in each group into a single row.

4. GROUP BY:
o Groups the data based on group_num for output.

Perform this lab Practical in the DBMS Write a sql statements for rollback commit and save
points

After Execution:
Group_Num Employees
1 Ekam , Ankit, Lalita
2 Arghya, Honey
3 Anuj
Experiment No: 10
Problem Statement: Write a sql statements for rollback commit and save points.
Solution:
[Link] the table:
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50),
Salary DECIMAL(10, 2)
);
[Link] data:
INSERT INTO Employees (EmpID, EmpName, Salary)
VALUES
(1, 'Ankit', 50000),
(2, 'Lalita', 55000),
(3, 'Charlie', 60000);
3: Start a transaction
START TRANSACTION;
4: Perform operations with savepoints
-- Insert a new record
INSERT INTO Students (StudentID, Name, Age) VALUES (4, 'Harshita', 21);

-- Set a savepoint
SAVEPOINT Savepoint1;

-- Insert another record


INSERT INTO Students (StudentID, Name, Age) VALUES (5, 'Anuj', 23);

-- Set another savepoint


SAVEPOINT Savepoint2;

-- Update an existing record


UPDATE Students SET Age = 24 WHERE StudentID = 3;
5: Rollback to a savepoint
-- Undo changes after Savepoint1
ROLLBACK TO Savepoint1;
At this point:
 The record for 'Harshita' (StudentID 4) remains.
 The record for 'Anuj' (StudentID 5) and the update to 'Charlie' (StudentID 3) are undone.
6: Commit the transaction
COMMIT;
7: Verify the changes
SELECT * FROM Students;

Explanation:
1. START TRANSACTION: Begins a transaction, allowing changes to be grouped and
committed or rolled back as a unit.
2. SAVEPOINT: Creates a marker in the transaction, allowing partial rollback.
3. ROLLBACK TO Savepoint: Undoes changes made after the savepoint, without affecting
earlier changes.
4. COMMIT: Saves all changes made in the transaction permanently to the database.

After Execution:
After the rollback and commit, the table will contain:

StudentID Name Age


1 Ankit 20
2 Lalita 22
3 Charlie 19
4 Harshita 21

Experiment No: 11
Problem Statement: Write a pl/sql for select, insert, update and delete statements.
Solution:
[Link] the table:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
);
[Link] the PL/SQL Code
DECLARE
-- Declare variables to hold data
v_StudentID [Link]%TYPE;
v_Name [Link]%TYPE;
v_Age [Link]%TYPE;
BEGIN
-- 1. INSERT Operation
INSERT INTO Students (StudentID, Name, Age)
VALUES (101, 'Ekam Doe', 20);

DBMS_OUTPUT.PUT_LINE('Insert Operation Completed: StudentID = 101, Name = Ekam


Doe, Age = 20');

-- 2. SELECT Operation
SELECT StudentID, Name, Age
INTO v_StudentID, v_Name, v_Age
FROM Students
WHERE StudentID = 101;

DBMS_OUTPUT.PUT_LINE('Select Operation Result:');


DBMS_OUTPUT.PUT_LINE('StudentID = ' || v_StudentID || ', Name = ' || v_Name || ', Age = '
|| v_Age);

-- 3. UPDATE Operation
UPDATE Students
SET Age = 21
WHERE StudentID = 101;

DBMS_OUTPUT.PUT_LINE('Update Operation Completed: Updated Age = 21 for StudentID =


101');

-- 4. DELETE Operation
DELETE FROM Students
WHERE StudentID = 101;

DBMS_OUTPUT.PUT_LINE('Delete Operation Completed: Removed StudentID = 101');


EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
[Link] DBMS_OUTPUT: To see the results of DBMS_OUTPUT.PUT_LINE, ensure that the
output is enabled:
SET SERVEROUTPUT ON;
[Link] the Operations:
 After the INSERT and UPDATE, check the Students table using:
SELECT * FROM Students;
 After the DELETE, the record with StudentID = 101 should no longer exist.

Experiment No:12
Problem Statement: Write a pl/sql block to delete a record. If delete operation is successful
return 1 else return 0.
Solution:
[Link] the Students Table
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
);
2. Insert Sample Data:
INSERT INTO Students (StudentID, Name, Age) VALUES (101, 'Ekam Doe', 20);
[Link] DBMS_OUTPUT:
SET SERVEROUTPUT ON;
[Link] the PL/SQL Code:
DECLARE
v_StudentID [Link]%TYPE := 101; -- Change StudentID as needed
v_Result NUMBER; -- Variable to hold the result (1 for success, 0 for failure)
BEGIN
-- Attempt to delete the record
DELETE FROM Students
WHERE StudentID = v_StudentID;

-- Check if any rows were deleted


IF SQL%ROWCOUNT > 0 THEN
v_Result := 1; -- Success
DBMS_OUTPUT.PUT_LINE('Record deleted successfully. Result = ' || v_Result);
ELSE
v_Result := 0; -- Failure
DBMS_OUTPUT.PUT_LINE('No record found to delete. Result = ' || v_Result);
END IF;
EXCEPTION
WHEN OTHERS THEN
v_Result := 0; -- On error, set result to failure
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM || '. Result = ' || v_Result);
END;
5. Verify the Deletion:
o If the record is successfully deleted, the output will display Result = 1.

o If the record does not exist, the output will display Result = 0.
Experiment No: 13
Problem Statement: Display name, hire date of all employees using cursors.
Solution:
[Link] the Employees Table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
HireDate DATE
);
[Link] Data:
INSERT INTO Employees (EmployeeID, Name, HireDate) VALUES
(1, 'Ankit', TO_DATE('2023-01-10', 'YYYY-MM-DD')),
(2, 'Lalita', TO_DATE('2022-06-15', 'YYYY-MM-DD')),
(3, 'Charlie', TO_DATE('2021-03-20', 'YYYY-MM-DD'));
[Link] DBMS_OUTPUT:
SET SERVEROUTPUT ON;
[Link] the PL/SQL Code:
DECLARE
-- Declare a cursor to fetch employee name and hire date
CURSOR emp_cursor IS
SELECT Name, HireDate
FROM Employees;

-- Declare variables to hold the cursor data


v_Name [Link]%TYPE;
v_HireDate [Link]%TYPE;
BEGIN
-- Open the cursor and iterate through each record
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_Name, v_HireDate;

-- Exit the loop when no more rows are found


EXIT WHEN emp_cursor%NOTFOUND;

-- Display the name and hire date


DBMS_OUTPUT.PUT_LINE('Name: ' || v_Name || ', Hire Date: ' || TO_CHAR(v_HireDate,
'DD-MON-YYYY'));
END LOOP;

-- Close the cursor


CLOSE emp_cursor;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
5. Expected Output:
Name: Ankit, Hire Date: 10-JAN-2023
Name: Lalita, Hire Date: 15-JUN-2022
Name: Charlie, Hire Date: 20-MAR-2021
Experiment No: 14
Problem Statement: Display details of first 5 highly paid employees using cursors.
Solution:
1. Create the Employees Table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
Salary NUMBER(10, 2),
HireDate DATE
);
2. Insert Data:
INSERT INTO Employees (EmployeeID, Name, Salary, HireDate) VALUES
(1, 'Ankit', 75000, TO_DATE('2023-01-10', 'YYYY-MM-DD')),
(2, 'Lalita', 90000, TO_DATE('2022-06-15', 'YYYY-MM-DD')),
(3, 'Charlie', 85000, TO_DATE('2021-03-20', 'YYYY-MM-DD')),
(4, 'Harshita', 95000, TO_DATE('2020-08-05', 'YYYY-MM-DD')),
(5, 'Anuj', 87000, TO_DATE('2023-07-01', 'YYYY-MM-DD')),
(6, 'Lucky', 92000, TO_DATE('2019-11-25', 'YYYY-MM-DD');
3. Enable DBMS_OUTPUT:
SET SERVEROUTPUT ON;
4. Run the PL/SQL Code:
DECLARE
-- Declare a cursor to fetch details of top 5 highest-paid employees
CURSOR high_salary_cursor IS
SELECT EmployeeID, Name, Salary, HireDate
FROM Employees
ORDER BY Salary DESC
FETCH FIRST 5 ROWS ONLY;

-- Variables to hold the cursor data


v_EmployeeID [Link]%TYPE;
v_Name [Link]%TYPE;
v_Salary [Link]%TYPE;
v_HireDate [Link]%TYPE;
BEGIN
-- Open the cursor
OPEN high_salary_cursor;

-- Fetch each record from the cursor


LOOP
FETCH high_salary_cursor INTO v_EmployeeID, v_Name, v_Salary, v_HireDate;

-- Exit the loop if no more rows are found


EXIT WHEN high_salary_cursor%NOTFOUND;

-- Display the details of the employee


DBMS_OUTPUT.PUT_LINE('EmployeeID: ' || v_EmployeeID ||
', Name: ' || v_Name ||
', Salary: ' || v_Salary ||
', Hire Date: ' || TO_CHAR(v_HireDate, 'DD-MON-YYYY'));
END LOOP;

-- Close the cursor


CLOSE high_salary_cursor;

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
[Link] Output:
EmployeeID: 4, Name: Harshita, Salary: 95000, Hire Date: 05-AUG-2020
EmployeeID: 6, Name: Lucky, Salary: 92000, Hire Date: 25-NOV-2019
EmployeeID: 2, Name: Lalita, Salary: 90000, Hire Date: 15-JUN-2022
EmployeeID: 5, Name: Anuj, Salary: 87000, Hire Date: 01-JUL-2023
EmployeeID: 3, Name: Charlie, Salary: 85000, Hire Date: 20-MAR-2021
Experiment No: 15
Problem Statement: Write a database trigger which fires if you try to insert,
update, or delete after 7’o’ clock.
Solution:
1. Create the Employees Table:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
Salary NUMBER(10, 2),
HireDate DATE
);
2. Create the Trigger:
CREATE OR REPLACE TRIGGER trg_restrict_after_hours
BEFORE INSERT OR UPDATE OR DELETE ON Employees
FOR EACH ROW
BEGIN
-- Check if the current time is after 7 PM
IF TO_CHAR(SYSDATE, 'HH24') >= 19 THEN
RAISE_APPLICATION_ERROR(-20001, 'Operations are not allowed after 7:00
PM.');
END IF;
END;
/
3. Test the Trigger:
 Perform an INSERT, UPDATE, or DELETE during and after 7 PM.

Before 7 PM:
INSERT INTO Employees (EmployeeID, Name, Salary, HireDate)

VALUES (1, 'Ekam Doe', 50000, SYSDATE);

 The operation will succeed.

After 7 PM:

INSERT INTO Employees (EmployeeID, Name, Salary, HireDate)

VALUES (2, 'Jane Doe', 60000, SYSDATE);

 The operation will fail with the error:

ORA-20001: Operations are not allowed after 7:00 PM.

4. Check Error Handling: You can capture the error using an application or script that
interacts with the database.

You might also like