0% found this document useful (0 votes)
7 views6 pages

Triggers - Examples - Lab - 6 - 1 1

The document outlines the creation of various database tables and triggers related to member management and employee actions. It includes examples of logging changes to employee records, enforcing salary limits, and maintaining a history log of actions performed on the 'employees' table. Additionally, it demonstrates how to implement constraints on salary changes based on specific conditions.

Uploaded by

dias.ashimov.04
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)
7 views6 pages

Triggers - Examples - Lab - 6 - 1 1

The document outlines the creation of various database tables and triggers related to member management and employee actions. It includes examples of logging changes to employee records, enforcing salary limits, and maintaining a history log of actions performed on the 'employees' table. Additionally, it demonstrates how to implement constraints on salary changes based on specific conditions.

Uploaded by

dias.ashimov.04
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
You are on page 1/ 6

Main table which will be used in the following examples:

CREATE TABLE Members (


member_id NUMBER(10) PRIMARY KEY,
first_name VARCHAR2(50) NOT NULL,
last_name VARCHAR2(50) NOT NULL,
membership_date DATE NOT NULL,
membership_type VARCHAR2(20) CHECK (membership_type IN ('Basic', 'Premium', 'VIP')),
loyalty_points NUMBER DEFAULT 0
);
EXAMPLE 1
CREATE TABLE history_log (
id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
user_name VARCHAR2(50),
table_name VARCHAR2(50),
action_type VARCHAR2(10),
action_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modification_date TIMESTAMP,
action_details VARCHAR2(200)
);

CREATE OR REPLACE TRIGGER before_table_change


BEFORE INSERT OR UPDATE OR DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO history_log (
user_name, table_name, action_type, modification_date, action_details
)
VALUES (
USER,
'employees',
CASE
WHEN INSERTING THEN 'INSERT'
WHEN UPDATING THEN 'UPDATE'
WHEN DELETING THEN 'DELETE'
END,
SYSTIMESTAMP,
'BEFORE action executed'
);
END;
/

CREATE OR REPLACE TRIGGER after_table_change


AFTER INSERT OR UPDATE OR DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO history_log (
user_name, table_name, action_type, modification_date, action_details
)
VALUES (
USER,
'employees',
CASE
WHEN INSERTING THEN 'INSERT'
WHEN UPDATING THEN 'UPDATE'
WHEN DELETING THEN 'DELETE'
END,
SYSTIMESTAMP,
'AFTER action executed'
);
END;
/
EXAMPLE 2
CREATE TABLE attribute_change_log (
id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
attribute_name VARCHAR2(50),
old_value VARCHAR2(100),
new_value VARCHAR2(100),
change_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE OR REPLACE TRIGGER log_attribute_changes


AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
INSERT INTO attribute_change_log (attribute_name, old_value, new_value)
VALUES ('salary', TO_CHAR(:OLD.salary), TO_CHAR(:NEW.salary));

INSERT INTO attribute_change_log (attribute_name, old_value, new_value)


VALUES ('job_id', :OLD.job_id, :NEW.job_id);

INSERT INTO attribute_change_log (attribute_name, old_value, new_value)


VALUES ('manager_id', TO_CHAR(:OLD.manager_id), TO_CHAR(:NEW.manager_id));
END;
/
EXAMPLE 3
CREATE OR REPLACE TRIGGER prevent_large_changes
BEFORE UPDATE OF salary ON employees
FOR EACH ROW
BEGIN
IF :NEW.salary - :OLD.salary > 5000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary increase exceeds the limit of 5000.');
ELSIF :NEW.salary - :OLD.salary < -3000 THEN
RAISE_APPLICATION_ERROR(-20002, 'Salary decrease exceeds the limit of 3000.');
END IF;
END;
/
EXAMPLE 4
CREATE OR REPLACE PROCEDURE enforce_salary_range (
position VARCHAR2,
new_salary NUMBER
) IS
BEGIN
IF position = 'Manager' AND (new_salary < 5000 OR new_salary > 15000) THEN
RAISE_APPLICATION_ERROR(-20003, 'Salary for Manager must be between 5000 and
15000.');
ELSIF position = 'Clerk' AND (new_salary < 2000 OR new_salary > 8000) THEN
RAISE_APPLICATION_ERROR(-20004, 'Salary for Clerk must be between 2000 and 8000.');
END IF;
END;
/

CREATE OR REPLACE TRIGGER enforce_salary_limits


BEFORE UPDATE OF salary ON employees
FOR EACH ROW
BEGIN
enforce_salary_range(:NEW.job_id, :NEW.salary);
END;
/

You might also like