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

UBD11

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

Tema nr.

11
Observație!
Scrieți rezolvarea direct în acest document!

1. A business rule states that each time one or more employees are added to the employees
table, an audit record must also be created. This rule could be enforced using application
code, but we have decided to enforce it using a DML statement trigger.

A. Create an audit table by executing the following SQL statement:

CREATE TABLE audit_table


(action VARCHAR2(20),
user_name VARCHAR2(30) DEFAULT USER,
last_change_date TIMESTAMP DEFAULT SYSTIMESTAMP);

B. Create a statement level trigger that inserts a row into the audit table immediately after
one or more rows are added to the employees_dup table that you created in a previous
lesson. The audit table row should contain value “Inserting” in the action column. The
other two columns should have their default values. Save your trigger code for later.

CREATE OR REPLACE TRIGGER record_insert

AFTER INSERT ON employees_dup

BEGIN

INSERT INTO audit_table(action,user_name,last_change_date)

VALUES('Inserting', NULL, NULL);

END;
C. Test your trigger by inserting a row into employees, then querying the audit table to
see that it contains a row.

INSERT INTO employees_dup(last_name,email,hire_date,job_id)

VALUES('Smith', 'smith@yahoo.com', '12-DEC-2001', 'PROG');

SELECT * FROM audit_table

D. Make sure the trigger does not fire with a DELETE by deleting the employee you just
entered. Recheck the audit_table table to make sure that there is not another new row.

DELETE FROM employees_dup

WHERE last_name='Smith';

SELECT * FROM audit_table


2. Modify Triggers:

a. Modify your audit table trigger from question 1B so that it inserts a row into the
audit table immediately before one or more employee salaries are updated. The
audit table row should contain value “Updating” in the action column.

CREATE OR REPLACE TRIGGER record_insert

BEFORE UPDATE OF salary

ON employees_dup

BEGIN

INSERT INTO audit_table(action)

VALUES('Updating');

END;

b. Test your trigger by updating the salary of a non-existent employee (employee_id


= 999), then querying the audit table to see that it contains a new row.

UPDATE employees_dup

SET salary=15000

WHERE employee_id=999;
SELECT * FROM audit_table

c. Modify your trigger so that it prevents employees’ salaries being updated outside
working hours. The trigger should allow updates at other times (and still insert a
row into the audit table), but should raise an application error if an update is
attempted before 8am or after 6pm on any day.

CREATE OR REPLACE TRIGGER check_sal_update

BEFORE UPDATE OF salary

ON employees_dup

BEGIN

IF (TO_CHAR(SYSDATE,'HH24') <8 OR TO_CHAR(SYSDATE,'HH24') <18) THEN

RAISE_APPLICATION_ERROR(-20101,'Nu puteti actualiza salariul!');

END IF;

END;
d. You want to test your modified trigger. However, you need to make sure that right
now the database time is outside working hours. Remember that the database
could be anywhere in the world and therefore the database may not be in your
time zone! Find the current database time by executing:

SELECT TO_CHAR(SYSDATE,'HH24:MI') FROM dual;

e. Now modify your trigger so that it will raise the application error if you try to
update a salary within the next hour. For example, if the database time is 10:30,
modify the trigger code to include: … BETWEEN ’10:30’ AND ’11:30’ …

CREATE OR REPLACE TRIGGER check_sal_update

BEFORE UPDATE OF salary

ON employees_dup

BEGIN

IF (SYSDATE BETWEEN '10:30' AND '11:30') THEN

RAISE_APPLICATION_ERROR(-20101,'Nu puteti actualiza salariul!');

END IF;

END;

f. Test your modified trigger by trying to update the salary of employee_id 100 to a
new value of 25000.

UPDATE employees SET salary = 25000


WHERE employee_id = 100;
3. Triggers:

A. Retrieve the code for the AFTER INSERT trigger you created in the previous practice,
question 1B. If you have lost the code, here it is again:

CREATE OR REPLACE TRIGGER emp_audit_trigg


AFTER INSERT ON employees
BEGIN
INSERT INTO audit_table (action)
VALUES ('Inserting');
END;

B. Modify this trigger so that a DELETE on the employees table will fire the same trigger.
Use the conditional predicates so an insert adds a row to the audit_emp table with
‘Inserted’ for the action column and a delete adds a row with ‘Deleted’ in the action
column. Save the script and test your trigger by inserting an employee row and then
deleting the same row, querying the audit table each time.

CREATE OR REPLACE TRIGGER emp_audit_trigg


AFTER DELETE ON employees
BEGIN
INSERT INTO audit_table (action) VALUES ('Deleted');
END;
C. Add a new column called emp_id to the audit_table table. This column will contain the
employee id of the worker whose record was inserted or deleted. Modify your trigger to
be a row trigger so it will fire once for each row affected. The inserts into the audit_emp
table should now include the employee id of the affected employee.
CREATE OR REPLACE TRIGGER emp_audit_trigg
BEFORE DELETE OR INSERT ON employees_dup
FOR EACH ROW
BEGIN
INSERT INTO audit_table(action, emp_id) VALUES ('Deleted', :old.employee_id);
END;

D. Test your trigger. First, turn off Autocommit in Application Express (you will need to
rollback your changes later). Then, delete the three Sales Representatives (job_id =
‘SA_REP’). Query the audit table; you should see that three audit rows have been inserted.
Finally, rollback your changes.

4. Create a row trigger:

A. That displays the maximum salary in the employees table, and is fired immediately
before an employee’s salary is updated.
CREATE OR REPLACE TRIGGER emp_update_salary
BEFORE UPDATE ON EMPLOYEES_DUP
FOR EACH ROW
DECLARE
v_max_salary NUMBER(8, 2);
BEGIN
IF(:new.salary != :old.salary) THEN
SELECT MAX(salary) INTO v_max_salary FROM EMPLOYEES_DUP;
END IF;
END;

B. Test your trigger by attempting to update the salary of employee_id 100 to a new value of
25000. What happens and why?
UPDATE employees_dup
SET salary=25000
WHERE employee_id=100;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy