0% found this document useful (0 votes)
11 views7 pages

Triggers in PLSQL Detailed Darsh Shetty

A PL/SQL trigger is an automatic stored procedure executed in response to specific events like INSERT, UPDATE, or DELETE on a table. The document outlines the general syntax, types of triggers, and provides examples of creating various triggers, including handling exceptions and logging changes. It also discusses limitations, differences between row-level and statement-level triggers, and the impact of triggers on database performance.

Uploaded by

kingnigg8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Triggers in PLSQL Detailed Darsh Shetty

A PL/SQL trigger is an automatic stored procedure executed in response to specific events like INSERT, UPDATE, or DELETE on a table. The document outlines the general syntax, types of triggers, and provides examples of creating various triggers, including handling exceptions and logging changes. It also discusses limitations, differences between row-level and statement-level triggers, and the impact of triggers on database performance.

Uploaded by

kingnigg8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

TRIGGERS IN PLSQL

Darsh Shetty - scs2425012

1. What is a PL/SQL Trigger? Describe its General Syntax and Types.

Definition:

A PL/SQL trigger is a stored procedure in Oracle that is automatically executed (or fired) when a specific event occurs.

These events include INSERT, UPDATE, or DELETE operations on a table or view.

General Syntax:

CREATE [OR REPLACE] TRIGGER trigger_name

{BEFORE | AFTER | INSTEAD OF} [INSERT | UPDATE | DELETE]

ON table_name

[FOR EACH ROW]

[WHEN (condition)]

BEGIN

-- Trigger body (logic to be executed)

END;

Types of Triggers:

1. BEFORE Triggers

2. AFTER Triggers

3. INSTEAD OF Triggers

4. Row-Level Triggers

5. Statement-Level Triggers
---

2. Syntax for Creating a Simple AFTER INSERT Trigger

CREATE OR REPLACE TRIGGER after_insert_trigger

AFTER INSERT

ON table_name

FOR EACH ROW

BEGIN

-- Trigger body (logic for post-insert operation)

END;

---

3. Difference Between BEFORE and AFTER Triggers

BEFORE Trigger: Executes before the DML operation is processed.

AFTER Trigger: Executes after the DML operation is completed.

---

4. How Do You Handle Exceptions in a PL/SQL Trigger? Provide an Example.

CREATE OR REPLACE TRIGGER handle_exception_trigger

AFTER INSERT ON employees

FOR EACH ROW


BEGIN

BEGIN

INSERT INTO employee_log (employee_id, action, log_date)

VALUES (:NEW.employee_id, 'INSERTED', SYSDATE);

EXCEPTION

WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);

END;

END;

---

5. Write a PL/SQL Trigger to Log Changes in the Salary Column of the employees Table.

CREATE OR REPLACE TRIGGER log_salary_changes

AFTER UPDATE OF salary

ON employees

FOR EACH ROW

BEGIN

INSERT INTO salary_log (employee_id, old_salary, new_salary, change_date)

VALUES (:OLD.employee_id, :OLD.salary, :NEW.salary, SYSDATE);

END;

---

6. What is the Use of an INSTEAD OF Trigger? Provide an Example.


CREATE OR REPLACE TRIGGER update_view_instead

INSTEAD OF UPDATE

ON department_view

BEGIN

UPDATE departments

SET department_name = :NEW.department_name

WHERE department_id = :OLD.department_id;

END;

---

7. What are the Limitations or Restrictions When Using Triggers in PL/SQL?

1. Performance Impact

2. Debugging Complexity

3. Recursive Triggers

4. No Direct Invocation

5. Restrictions on DDL Operations

6. Mutating Table Error

---

8. Explain the Difference Between Row-Level and Statement-Level Triggers.

Row-Level Trigger: Fires for each affected row in a DML operation.


Statement-Level Trigger: Fires once for the entire DML statement.

---

9. How Do You Disable or Enable a Trigger in PL/SQL?

Disable: ALTER TRIGGER trigger_name DISABLE;

Enable: ALTER TRIGGER trigger_name ENABLE;

---

10. What is the Purpose of the FOR EACH ROW Clause in a PL/SQL Trigger?

Makes a trigger a row-level trigger, executing once for every affected row.

---

11. Write a PL/SQL Trigger That Updates an audit_log Table After a Row is Deleted from the employees Table.

CREATE OR REPLACE TRIGGER audit_log_trigger

AFTER DELETE ON employees

FOR EACH ROW

BEGIN

INSERT INTO audit_log (employee_id, deleted_by, deletion_date)

VALUES (:OLD.employee_id, USER, SYSDATE);

END;
---

12. How Would You Control the Execution Order of Multiple Triggers on the Same Table?

Oracle does not allow explicit control over trigger execution order. Combine logic into a single trigger.

---

13. Can You Create a Trigger on a View? Explain with an Example.

Yes, INSTEAD OF triggers can be created on views to handle DML operations.

CREATE OR REPLACE TRIGGER view_trigger

INSTEAD OF INSERT ON some_view

BEGIN

INSERT INTO base_table (column1, column2)

VALUES (:NEW.column1, :NEW.column2);

END;

---

14. Explain How Triggers Can Impact the Performance of a Database.

Impact: Additional overhead, complexity, and contention.

Best Practices: Use triggers only for critical tasks like auditing or validation.
---

15. Write a PL/SQL Trigger to Audit Insert Operations on a products Table.

CREATE OR REPLACE TRIGGER audit_insert_products

AFTER INSERT ON products

FOR EACH ROW

BEGIN

INSERT INTO products_audit (product_id, action, log_date)

VALUES (:NEW.product_id, 'INSERTED', SYSDATE);

END;

You might also like