0% found this document useful (0 votes)
80 views8 pages

24vmt0l201 Advanced Database Systems Ca2 Lab Solution

Uploaded by

Jp Jp
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)
80 views8 pages

24vmt0l201 Advanced Database Systems Ca2 Lab Solution

Uploaded by

Jp Jp
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/ 8

CONTINUOUS ASSESSMENT-2 - SEM II

24VMT0L201 ADVANCED DATABASE SYSTEMS LAB


1.Write a PL/pgSQL stored procedure that uses a static employee_id to calculate the number of years the employee has worked in the company based on
their hire_date. Insert the result into a visible table named employee_output.

-- INSTRUCTIONS:
-- 1. Create the employees and employee_output tables
-- 2. Write a stored procedure that uses a static employee_id (e.g. 101)
-- 3. Insert employee name and calculated service years into employee_output
-- 4. View the result using SELECT * FROM employee_output

DROP TABLE IF EXISTS employees;


DROP TABLE IF EXISTS employee_output;

CREATE TABLE employees (


employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
designation VARCHAR(50),
hire_date DATE,
department VARCHAR(50),
salary DECIMAL(10,2)
);

CREATE TABLE employee_output (


employee_id INT,
employee_name TEXT,
service_years NUMERIC(5,2)
);

INSERT INTO employees VALUES


(101, 'Rajesh Kumar', 'Software Engineer', '2020-03-15', 'IT', 75000),
(102, 'Priya Sharma', 'Senior Developer', '2018-07-22', 'IT', 95000),
(103, 'Suresh Reddy', 'Team Lead', '2015-01-10', 'IT', 110000);

-- Write your stored procedure below:


DROP PROCEDURE IF EXISTS insert_employee_service_years;

CREATE OR REPLACE PROCEDURE insert_employee_service_years()


LANGUAGE plpgsql
AS $$
DECLARE
emp_id INT := 101;
emp_name TEXT;
hire_dt DATE;
years_of_service NUMERIC(5,2);
BEGIN
-- Get employee data
SELECT employee_name, hire_date
INTO emp_name, hire_dt
FROM employees
WHERE employee_id = emp_id;

-- Correct calculation: use days difference


years_of_service := ROUND((CURRENT_DATE - hire_dt) / 365.0, 2);

-- Insert result
INSERT INTO employee_output (employee_id, employee_name, service_years)
VALUES (emp_id, emp_name, years_of_service);
END;
$$;
CALL insert_employee_service_years();
SELECT * FROM employee_output;

Expected output:
employee_id employee_name service_years

101 Rajesh Kumar 5.39


2.Write a PL/pgSQL trigger named log_salary_update that automatically inserts a record into the salaries_audit table whenever a n employee’s salary is
updated in the employees table. Capture the employee ID, old salary, new salary, and the timestamp.

-- INSTRUCTIONS:
-- 1. Create tables: employees and salaries_audit
-- 2. Create a trigger function that captures salary updates
-- 3. Write a trigger that invokes the function on salary update
-- 4. Use AFTER UPDATE and FOR EACH ROW
-- 5. Capture employee_id, old salary, new salary, and timestamp

-- Step 1: Create tables


DROP TABLE IF EXISTS employees;
DROP TABLE IF EXISTS salaries_audit;

CREATE TABLE employees (


employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
salary DECIMAL(10,2)
);

CREATE TABLE salaries_audit (


audit_id SERIAL PRIMARY KEY,
employee_id INT,
old_salary DECIMAL(10,2),
new_salary DECIMAL(10,2),
change_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Step 2: Insert sample data


INSERT INTO employees VALUES
(101, 'Rajesh Kumar', 75000),
(102, 'Priya Sharma', 95000);

-- Write your trigger function and trigger below:


-- Step 3: Create trigger function
CREATE OR REPLACE FUNCTION log_salary_update()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO salaries_audit (employee_id, old_salary, new_salary)
VALUES (
OLD.employee_id,
OLD.salary,
NEW.salary
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Step 4: Create trigger on salary update


CREATE TRIGGER salary_update_audit
AFTER UPDATE OF salary ON employees
FOR EACH ROW
EXECUTE FUNCTION log_salary_update();

-- Update a salary to trigger the audit


UPDATE employees
SET salary = 98000
WHERE employee_id = 102;

-- View all employees with current salaries


SELECT * FROM employees;
-- View all recorded salary updates
SELECT * FROM salaries_audit;

Expected output:
audit_id employee_id old_salary new_salary change_time

1 102 95000.00 98000.00 2025-08-02T15:57:04.276102


3.Create a PL/pgSQL procedure named issue_book that accepts a book_id and a member_id. The procedure should (1) check if the book is available
(available_copies > 0), (2) insert an issue transaction with today's issue date and a due date of 7 days later, and (3) update the books table to reduce the
available copies by 1.

-- INSTRUCTIONS:
-- 1. Create books and transactions tables
-- 2. Write a PL/pgSQL procedure named issue_book(book_id, member_id)
-- 3. Procedure must:
-- - Check if book is available
-- - Insert transaction with issue and due dates
-- - Decrease available_copies by 1

-- Sample table setup


DROP TABLE IF EXISTS books;
DROP TABLE IF EXISTS transactions;

CREATE TABLE books (


book_id INT PRIMARY KEY,
title VARCHAR(100),
author VARCHAR(100),
available_copies INT
);

CREATE TABLE transactions (


transaction_id SERIAL PRIMARY KEY,
book_id INT,
member_id INT,
issue_date DATE,
due_date DATE
);

INSERT INTO books VALUES


(1, 'Clean Code', 'Robert C. Martin', 3),
(2, 'The Pragmatic Programmer', 'Andrew Hunt', 2),
(3, 'Intro to Algorithms', 'Cormen', 0);

-- Write your procedure below


CREATE OR REPLACE PROCEDURE issue_book(IN p_book_id INT, IN p_member_id INT)
LANGUAGE plpgsql
AS $$
BEGIN
-- Check availability
IF EXISTS (
SELECT 1 FROM books WHERE book_id = p_book_id AND available_copies > 0
) THEN

-- Insert issue transaction


INSERT INTO transactions (book_id, member_id, issue_date, due_date)
VALUES (
p_book_id,
p_member_id,
CURRENT_DATE,
CURRENT_DATE + INTERVAL '7 days'
);

-- Decrease available copies


UPDATE books
SET available_copies = available_copies - 1
WHERE book_id = p_book_id;

ELSE
RAISE NOTICE 'Book ID % is currently unavailable.', p_book_id;
END IF;
END;
$$;

--update operation
CALL issue_book(1, 101);

--View all books and transactions


SELECT * FROM books;
SELECT * FROM transactions;

Expected output:
transaction_id book_id member_id issue_date due_date

1 1 101 2025-08-02 2025-08-09


4.Write a PL/pgSQL stored procedure that uses a cursor to fetch all employee records and inserts employee_id, employee_name, and salary into the
employee_output table. This allows viewing the result in the lab.

-- INSTRUCTIONS:
-- 1. Create employees and employee_output tables
-- 2. Write a stored procedure using a cursor to fetch all employee details
-- 3. Insert employee_id, employee_name, and salary into employee_output
-- 4. View the result using SELECT * FROM employee_output

DROP TABLE IF EXISTS employees;


DROP TABLE IF EXISTS employee_output;

CREATE TABLE employees (


employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
designation VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10,2)
);

CREATE TABLE employee_output (


employee_id INT,
employee_name TEXT,
salary DECIMAL(10,2)
);

INSERT INTO employees VALUES


(101, 'Rajesh Kumar', 'Software Engineer', 'IT', 75000),
(102, 'Priya Sharma', 'Senior Developer', 'IT', 95000),
(103, 'Suresh Reddy', 'Team Lead', 'IT', 110000),
(104, 'Kavitha Nair', 'HR Manager', 'HR', 85000);

-- Write your stored procedure below:


-- Drop procedure if it exists
DROP PROCEDURE IF EXISTS fetch_employees();

-- Create procedure
CREATE OR REPLACE PROCEDURE fetch_employees()
LANGUAGE plpgsql
AS $$
DECLARE
emp_rec RECORD;
emp_cursor CURSOR FOR SELECT employee_id, employee_name, salary FROM employees;
BEGIN
-- Open the cursor
OPEN emp_cursor;

-- Loop through each row returned by the cursor


LOOP
FETCH emp_cursor INTO emp_rec;
EXIT WHEN NOT FOUND;

-- Insert into employee_output


INSERT INTO employee_output (employee_id, employee_name, salary)
VALUES (emp_rec.employee_id, emp_rec.employee_name, emp_rec.salary);
END LOOP;

-- Close the cursor


CLOSE emp_cursor;
END;
$$;
-- Call the procedure
CALL fetch_employees();

-- View the inserted data


SELECT * FROM employee_output;

Expected Output:
employee_id employee_name salary

101 Rajesh Kumar 75000.00

102 Priya Sharma 95000.00

103 Suresh Reddy 110000.00

104 Kavitha Nair 85000.00

You might also like