Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views12 pages

Dbms PT Answers

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

DBMS Answers:

1 I)

DECLARE

V_deptid department.deptid%TYPE;

V_deptname department.deptname%TYPE;

V_head_of_dept department.head_of_dept%TYPE;

BEGIN

 Assign values to the variables

V_deptid := 1;

V_deptname := ‘Computer Science’;

V_head_of_dept := ‘John Doe’;

 Display the values of the variables

DBMS_OUTPUT.PUT_LINE(‘Department ID: ‘ || v_deptid);

DBMS_OUTPUT.PUT_LINE(‘Department Name: ‘ || v_deptname);

DBMS_OUTPUT.PUT_LINE(‘Head of Department: ‘ || v_head_of_dept);

END;

ii)

DECLARE

V_is_true BOOLEAN := TRUE;

BEGIN

IF v_is_true THEN

DBMS_OUTPUT.PUT_LINE(‘The boolean value is TRUE’);

ELSE

DBMS_OUTPUT.PUT_LINE(‘The boolean value is FALSE’);

END IF;

END;

2)
CREATE OR REPLACE PROCEDURE update_employee_salaries AS

BEGIN

UPDATE employees

SET salary = salary * 1.1; -- Increase salary by 10%

DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ‘ records updated.’); -- Display number of records


affected

COMMIT; -- Commit the changes

END;

DECLARE

CURSOR employee_cursor IS

SELECT employee_id, first_name, last_name, job_id, hire_date, salary

FROM employees;

 Declare variables to hold column values

V_employee_id employees.employee_id%TYPE;

V_first_name employees.first_name%TYPE;

V_last_name employees.last_name%TYPE;

V_job_id employees.job_id%TYPE;

V_hire_date employees.hire_date%TYPE;

V_salary employees.salary%TYPE;

BEGIN

OPEN employee_cursor;

LOOP

FETCH employee_cursor INTO v_employee_id, v_first_name, v_last_name, v_job_id, v_hire_date,


v_salary;

 Exit loop when there are no more rows to fetch

EXIT WHEN employee_cursor%NOTFOUND;

 Display the details of each employee


DBMS_OUTPUT.PUT_LINE(‘Employee ID: ‘ || v_employee_id);

DBMS_OUTPUT.PUT_LINE(‘First Name: ‘ || v_first_name);

DBMS_OUTPUT.PUT_LINE(‘Last Name: ‘ || v_last_name);

DBMS_OUTPUT.PUT_LINE(‘Job ID: ‘ || v_job_id);

DBMS_OUTPUT.PUT_LINE(‘Hire Date: ‘ || v_hire_date);

DBMS_OUTPUT.PUT_LINE(‘Salary: ‘ || v_salary);

DBMS_OUTPUT.PUT_LINE(‘-----------------------‘);

END LOOP;

CLOSE employee_cursor;

END;

3)

i) CREATE OR REPLACE TRIGGER employees_before_each_row_update

BEFORE UPDATE ON employees

FOR EACH ROW

BEGIN

DBMS_OUTPUT.PUT_LINE(‘Before update on each row fired for employee ID ‘ || :OLD.employee_id);

 Additional code here

END;

CREATE OR REPLACE TRIGGER employees_after_each_row_update

AFTER UPDATE ON employees

FOR EACH ROW

BEGIN

DBMS_OUTPUT.PUT_LINE(‘After update on each row fired for employee ID ‘ || :NEW.employee_id);

 Additional code here

END;

/
CREATE OR REPLACE TRIGGER employees_before_statement_update

BEFORE UPDATE ON employees

BEGIN

DBMS_OUTPUT.PUT_LINE(‘Before update on statement fired’);

 Additional code here

END;

CREATE OR REPLACE TRIGGER employees_after_statement_update

AFTER UPDATE ON employees

BEGIN

DBMS_OUTPUT.PUT_LINE(‘After update on statement fired’);

 Additional code here

END;

ii)

CREATE OR REPLACE FUNCTION CalculateFare(

Vehicle_id IN NUMBER,

Num_adults IN NUMBER,

Num_children IN NUMBER

) RETURN NUMBER

IS

Vehicle_fare NUMBER;

Total_fare NUMBER;

BEGIN

 Look up the fare for the given vehicle ID

SELECT fare INTO vehicle_fare

FROM vehicles

WHERE vehicle_id = vehicle_id;


 Calculate the total fare

Total_fare := vehicle_fare * num_adults + (vehicle_fare / 2) * num_children;

 Return the result

RETURN total_fare;

END;

DECLARE

Fare NUMBER;

BEGIN

Fare := CalculateFare(123, 2, 3);

DBMS_OUTPUT.PUT_LINE(‘Total fare is ‘ || fare);

END;

4)

i)

CREATE OR REPLACE FUNCTION sf_get_emp_id_by_deptid(

P_dept_id IN NUMBER

) RETURN NUMBER

IS

V_emp_id NUMBER;

BEGIN

 Check if the department ID is valid

SELECT COUNT(*) INTO v_emp_id

FROM departments

WHERE department_id = p_dept_id;


IF v_emp_id = 0 THEN

 Invalid department ID

RETURN -1;

END IF;

 Check if more than one employee is associated with the department

SELECT COUNT(*) INTO v_emp_id

FROM employees

WHERE department_id = p_dept_id;

IF v_emp_id <> 1 THEN

 More than one employee or none is associated with the department

RETURN -2;

END IF;

 Fetch the employee ID for the given department ID

BEGIN

SELECT employee_id INTO v_emp_id

FROM employees

WHERE department_id = p_dept_id;

EXCEPTION

WHEN TOO_MANY_ROWS THEN

 More than one employee is associated with the department

RETURN -2;

WHEN OTHERS THEN

 Other errors

RETURN -3;

END;
 Return the employee ID

RETURN v_emp_id;

END;

DECLARE

V_emp_id NUMBER;

BEGIN

V_emp_id := sf_get_emp_id_by_deptid(10);

DBMS_OUTPUT.PUT_LINE(‘Employee ID for department ID 10 is ‘ || v_emp_id);

V_emp_id := sf_get_emp_id_by_deptid(20);

DBMS_OUTPUT.PUT_LINE(‘Employee ID for department ID 20 is ‘ || v_emp_id);

V_emp_id := sf_get_emp_id_by_deptid(25);

DBMS_OUTPUT.PUT_LINE(‘Employee ID for department ID 25 is ‘ || v_emp_id);

END;

ii)

DECLARE

V_manager_id employees.employee_id%TYPE := 103;

BEGIN

FOR emp_rec IN (

SELECT e.employee_id, e.first_name, j.job_title, e.department_id, e.hire_date

FROM employees e

JOIN jobs j ON e.job_id = j.job_id

WHERE e.manager_id = v_manager_id

LOOP
DBMS_OUTPUT.PUT_LINE(‘Employee ID: ‘ || emp_rec.employee_id);

DBMS_OUTPUT.PUT_LINE(‘Name: ‘ || emp_rec.first_name);

DBMS_OUTPUT.PUT_LINE(‘Job Title: ‘ || emp_rec.job_title);

DBMS_OUTPUT.PUT_LINE(‘Department: ‘ || emp_rec.department_id);

DBMS_OUTPUT.PUT_LINE(‘Joining date: ‘ || TO_CHAR(emp_rec.hire_date, ‘DD-MON-YYYY’));

DBMS_OUTPUT.NEW_LINE;

END LOOP;

END;

5)

i)

DECLARE

V_commission_pct employees.commission_pct%TYPE;

V_salary employees.salary%TYPE;

V_total_salary employees.salary%TYPE;

BEGIN

 Get the commission percentage and salary of Pat

SELECT commission_pct, salary

INTO v_commission_pct, v_salary

FROM employees

WHERE employee_id = 202;

 If Pat is a marketing rep, update his commission percentage to 0.1

IF v_commission_pct IS NOT NULL AND EXISTS (

SELECT 1

FROM employees

WHERE employee_id = 202 AND job_id = ‘MK_REP’

) THEN
UPDATE employees

SET commission_pct = 0.1

WHERE employee_id = 202;

END IF;

 Calculate Pat’s total salary

V_total_salary := v_salary * (1 + v_commission_pct);

 Display Pat’s total salary

DBMS_OUTPUT.PUT_LINE(‘Pat’’s total salary: ‘ || v_total_salary);

END;

ii)

DECLARE

V_department_id departments.department_id%TYPE := 50; -- Set the department ID here

CURSOR c_emp IS

SELECT employee_id, salary, hire_date

FROM employees

WHERE department_id = v_department_id;

BEGIN

 Loop through the employees in the department and display their details

FOR emp IN c_emp LOOP

DBMS_OUTPUT.PUT_LINE(‘Employee ID: ‘ || emp.employee_id ||

‘, Salary: ‘ || emp.salary ||

‘, Hire Date: ‘ || emp.hire_date);

END LOOP;

END;

/
6)

i)

DECLARE

V_country_code VARCHAR2(2);

BEGIN

 Check if Singapore already exists in the countries table

SELECT country_id INTO v_country_code FROM countries WHERE country_id = ‘SG’;

IF v_country_code IS NULL THEN

 If Singapore does not exist, add it to the countries table

INSERT INTO countries (country_id, country_name, region_id) VALUES (‘SG’, ‘Singapore’, 3);

DBMS_OUTPUT.PUT_LINE(‘Singapore added to the database.’);

ELSE

 If Singapore already exists, display a message

DBMS_OUTPUT.PUT_LINE(‘Singapore already exists in the database.’);

END IF;

 Check if Sri Lanka already exists in the countries table

SELECT country_id INTO v_country_code FROM countries WHERE country_id = ‘LK’;

IF v_country_code IS NULL THEN

 If Sri Lanka does not exist, add it to the countries table

INSERT INTO countries (country_id, country_name, region_id) VALUES (‘LK’, ‘Sri Lanka’, 3);

DBMS_OUTPUT.PUT_LINE(‘Sri Lanka added to the database.’);

ELSE

 If Sri Lanka already exists, display a message

DBMS_OUTPUT.PUT_LINE(‘Sri Lanka already exists in the database.’);

END IF;

END;

/
ii)

CREATE SEQUENCE seq_employee_id

START WITH 301

INCREMENT BY 1

NOCACHE

NOCYCLE;

SELECT seq_employee_id.NEXTVAL FROM dual;

7)

i)

DECLARE

V_job_id VARCHAR2(35) := ‘Programmer’;

V_count NUMBER;

BEGIN

 Check if the given job is valid

SELECT COUNT(*) INTO v_count FROM jobs WHERE job_title = v_job_id;

IF v_count = 0 THEN

DBMS_OUTPUT.PUT_LINE(‘Invalid job: ‘ || v_job_id);

ELSE

 If the job is valid, generate the report

SELECT COUNT(*) INTO v_count FROM employees WHERE job_id = v_job_id;

DBMS_OUTPUT.PUT_LINE(‘Number of employees working as ‘ || v_job_id || ‘: ‘ || v_count);

END IF;

END;

ii)

CREATE OR REPLACE TRIGGER trg_check_salary


BEFORE INSERT ON employees

FOR EACH ROW

DECLARE

V_min_salary jobs.min_salary%TYPE;

BEGIN

SELECT min_salary INTO v_min_salary FROM jobs WHERE job_id = :new.job_id;

IF :new.salary < v_min_salary THEN

:new.salary := v_min_salary;

END IF;

END;

You might also like