Dbms Lab Assignment 3new
Dbms Lab Assignment 3new
INFORMATION OF
TECHNOLOGY
Submitted By: Nilesh kumar kamal
Roll no-2021UG1060
Branch: CSE
Group: A
Semester: V
Course: DBMS
Course Code: CS 3001
Submitted To: Dr. Nidhi Kushwaha
• Write a code in PL/SQL to create a trigger that checks for different values in a
specific column and raises an exception if found.
CREATE OR REPLACE TRIGGER check_last_name
BEFORE INSERT OR UPDATE ON Employee
FOR EACH ROW
BEGIN
-- Check for specific values in the 'last_name' column
IF :NEW.last_name= 'INVALID' THEN
-- Raise an exception if 'INVALID' values are found
RAISE_APPLICATION_ERROR(-20001, 'Invalid value in the last_name column: '
|| :NEW.last_name);
END IF;
END;
• Write a code in PL/SQL to create a trigger that checks for duplicate values in a
specific column and raises an exception if found.
CREATE OR REPLACE TRIGGER prevent_duplicates
BEFORE INSERT ON Employee
FOR EACH ROW
DECLARE
v_count NUMBER;
BEGIN
-- Check if the new first_name already exists
SELECT COUNT(*) INTO v_count FROM Employee WHERE first_name
= :NEW.first_name;
-- If duplicate value found, raise an error
IF v_count> 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'first name already exists.');
END IF;
END;
/
• Create a trigger that checks if "value" is greater than 1000 for any new row inserted
to "Test" table, then insert a row to Audit table?
CREATE TABLE Test
(
test_id INTEGER,
value NUMBER,
test_date DATE
);
• Write a PL/SQL function to return the maximum bit length of the Last_Names in
the Employees table.
DECLARE
v_bit_length NUMBER;
v_max_bit_length NUMBER := 0;
BEGIN
FOR emp IN (SELECT Last_Name FROM Employee) LOOP
v_bit_length := LENGTHB(trim(emp.Last_Name)) * 8;
IF v_bit_length > v_max_bit_length THEN
v_max_bit_length := v_bit_length;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Maximum Bit Length: ' || v_max_bit_length);
END;
/
• Write a PL/SQL block to calculate the bit length of the employee's first name in the
employees table for all records.
DECLARE
v_bit_length NUMBER;
BEGIN
FOR emp IN (SELECT Last_Name FROM Employee) LOOP
v_bit_length := LENGTHB(trim(emp.Last_Name)) * 8;
DBMS_OUTPUT.PUT_LINE('Bit length of ' || emp.Last_Name || ': ' || v_bit_length);
END LOOP;
END;
/
• Write a PL/SQL block to display the character representation of each ASCII value
in the range of 65 to 90.
DECLARE
v_ascii_value NUMBER;
v_character CHAR(1);
BEGIN
FOR v_ascii_value IN 65..90 LOOP
-- Convert ASCII value to character
v_character := CHR(v_ascii_value);
• Write a PL/SQL block to convert the last name of each employee in the employees
table to lowercase and display the result.
DECLARE
v_lower_last_name VARCHAR2(50);
BEGIN
FOR emp IN (SELECT Last_Name FROM Employee) LOOP
-- Convert the last name to lowercase
v_lower_last_name := LOWER(emp.Last_Name);
• Write a PL/SQL block that replaces all occurrences of the substring 'IT_PROG'
with ‘IT PROGRAMMER' in the job titles of employees in the employees table.
Display the updated job titles.
DECLARE
v_job_title Employee.Job_Id%TYPE;
BEGIN
FOR emp IN (SELECT Job_Id FROM Employee) LOOP
-- Check if the current job_title is 'IT_PROG'
IF emp.Job_Id = 'IT_PROG' THEN
-- Update job_title to 'IT_PROGRAMMER'
v_job_title := 'IT_PROGRAMMER';
ELSE
-- Keep the existing job_title if not 'IT_PROG'
v_job_title := emp.Job_Id;
END IF;
• Write a program in PL/SQL to find the number of rows effected by the use of SQL
%ROWCOUNT attributes of an implicit cursor.
CREATE TABLE emp_temp AS
SELECT Employee_Id, First_Name, Last_Name,Email
FROM Employee;
BEGIN
UPDATE emp_temp
SET email = 'not available'
WHERE first_name LIKE 'B%';
-- Call the procedure to compute the square (passing and returning the same parameter)
compute_square(v_number);
• Write a program to update the CUSTOMER table and increase the the salary of
each customer by 1000. Here, SQL%ROWCOUNT attribute is used to determine
the number of rows affected.
DECLARE
v_updated_rows NUMBER;
BEGIN
-- Update the salary for each customer
UPDATE Customer
SET SALARY = SALARY + 1000;
• Using explicit cursor write a program to retrieve the customer name and address
from CUSTOMER table.
DECLARE
-- Declare variables to store customer data
v_customer_name Customer.NAME%TYPE;
v_customer_address Customer.ADDRESS%TYPE;
BEGIN
-- Open the cursor
OPEN customer_cursor;