Anda di halaman 1dari 7

Assighment

Advanced DataBase
01

Name:

Qasim Ali

Enrollment # 01-234122-019
Class:

BSCS-6B

Date:

23-02-2015

Submitted To:

Sayed Khushal Shah

Bahria University Islamabad Campus

Q. What are Triggers and Usage of Triggers and list


10 examples of Triggers?
Ans. Triggers are stored programs, which are automatically executed or fired
when some events occur. Triggers are, in fact, written to be executed in
response to any of the following events:

A database manipulation (DML) statement (DELETE, INSERT, or


UPDATE).

A database definition (DDL) statement (CREATE, ALTER, or DROP).

A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or


SHUTDOWN).
Triggers could be defined on the table, view, schema, or database with which
the event is associated.

Usage of Triggers
Triggers can be written for the following purposes:

Generating some derived column values automatically

Enforcing referential integrity

Event logging and storing information on table access

Auditing

Synchronous replication of tables

Imposing security authorizations

Preventing invalid transactions

Examples of Triggers
1. Logging deletions
CREATE TRIGGER Books_Delete
AFTER DELETE ON Books
REFERENCING OLD ROW AS Old
FOR EACH ROW
INSERT INTO Books_Deleted_Log
VALUES (Old.title);

2. Inserting default expressions


CREATE TRIGGER Clients_Insert
BEFORE INSERT ON Clients
REFERENCING NEW ROW AS New
FOR EACH ROW
SET New.home_telephone =
COALESCE(New.home_telephone,New.work_telephone);

3. Constraint substitute
CREATE TRIGGER Departments_Update
AFTER UPDATE OF budget ON Departments
WHEN (CURRENT_TIME > '17:00:00')

/* first flaw */
/* second flaw */

SELECT MAX(budget) / 0 FROM Departments;

4. Cascading update
CREATE TRIGGER Prime_Minister_Update
AFTER UPDATE ON Prime_Ministers
REFERENCING OLD ROW AS Old, NEW ROW AS New FOR EACH ROW
WHEN (New.name = 'Bob' AND New.name <> Old.name)
UPDATE Taxpayers SET tax_payable = tax_payable * 0.99;

5. Trigger Uses Conditional Predicates to Detect Triggering


Statement
CREATE OR REPLACE TRIGGER t
BEFORE
INSERT OR
UPDATE OF salary, department_id OR
DELETE
ON employees
BEGIN
CASE
WHEN INSERTING THEN
DBMS_OUTPUT.PUT_LINE('Inserting');
WHEN UPDATING('salary') THEN
DBMS_OUTPUT.PUT_LINE('Updating salary');
WHEN UPDATING('department_id') THEN
DBMS_OUTPUT.PUT_LINE('Updating department ID');
WHEN DELETING THEN
DBMS_OUTPUT.PUT_LINE('Deleting');

END CASE;END;

6. Create trigger that inserts row in log table after


EMPLOYEES.SALARY is updated:
CREATE OR REPLACE TRIGGER log_salary_increase
AFTER UPDATE OF salary ON employees
FOR EACH ROW
BEGIN
INSERT INTO Emp_log (Emp_id, Log_date, New_salary, Action)
VALUES (:NEW.employee_id, SYSDATE, :NEW.salary, 'New Salary');
END;

7. Conditional Trigger Prints Salary Change Information


CREATE OR REPLACE TRIGGER print_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON employees
FOR EACH ROW
WHEN (NEW.job_id <> 'AD_PRES')
President

-- do not print information about

DECLARE
sal_diff NUMBER;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
DBMS_OUTPUT.PUT(:NEW.last_name || ': ');
DBMS_OUTPUT.PUT('Old salary = ' || :OLD.salary || ', ');
DBMS_OUTPUT.PUT('New salary = ' || :NEW.salary || ', ');
DBMS_OUTPUT.PUT_LINE('Difference: ' || sal_diff);
END;

8.
CREATE TRIGGER trig1
AFTER INSERT ON T4
REFERENCING NEW AS newRow
FOR EACH ROW
WHEN (newRow.a <= 10)
BEGIN
INSERT INTO T5 VALUES(:newRow.b, :newRow.a);
END trig1;

9. Aborting Triggers with Error


CREATE TRIGGER PersonCheckAge
AFTER INSERT OR UPDATE OF age ON Person
FOR EACH ROW
BEGIN
IF (:new.age < 0) THEN
RAISE_APPLICATION_ERROR(-20000, 'no negative age allowed');
END IF;
END;

10.
CREATE TRIGGER instrig INSTEAD OF INSERT ON all_customers
FOR EACH ROW
BEGIN
IF (:new.cust.location = 'SAN_JOSE') THEN
INSERT INTO customers_sj

VALUES (:new.cust.cust, :new.cust.address,:new.cust.credit);


ELSE
INSERT INTO customers_pa
VALUES (:new.cust.cust, :new.cust.address, :new.cust.credit);
END IF;
END;

Anda mungkin juga menyukai