PL/SQL Instead of Triggers
Last Updated :
13 Sep, 2024
PL/SQL INSTEAD OF Triggers are a special type of trigger used primarily with views to allow operations like INSERT, UPDATE, and DELETE.
These triggers provide a way to perform complex operations that might not be possible directly on a view, enabling you to maintain data integrity and enforce business rules. In this article, We will learn about the INSTEAD OF trigger in PL/SQL by understanding various examples.
INSTEAD OF Trigger in PL/SQL
PL/SQL procedures offer control over database operations, allowing tasks to be executed at specific times. This approach integrates directly into application logic, providing more flexibility than automatic triggers.
Syntax of the INSTEAD OF Trigger:
CREATE OR REPLACE TRIGGER trigger_name
INSTEAD OF {INSERT | UPDATE | DELETE}
ON view_name
[FOR EACH ROW]
BEGIN
EXCEPTION
END;
Parameters:
- trigger_name: The name of the trigger being created (e.g., employee_view_trigger).
- {INSERT | UPDATE | DELETE}: Specifies which type of DML operation the trigger will handle (e.g., INSERT, UPDATE, DELETE).
- view_name: The name of the view on which the trigger is defined (e.g., employee_view).
Using the INSTEAD OF Trigger in PL/SQL
The INSTEAD OF trigger can be used in various PL/SQL operations to filter records. Below are examples demonstrating its use in INSERT, UPDATE, and DELETE statements.
Step 1: First, create the employees and departments tables
CREATE TABLE departments (
department_id NUMBER PRIMARY KEY,
department_name VARCHAR2(50)
);
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
name VARCHAR2(50),
department_id NUMBER,
CONSTRAINT fk_department
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);
Step 2: Next, insert some data into these tables
INSERT INTO departments (department_id, department_name) VALUES (1, 'HR');
INSERT INTO departments (department_id, department_name) VALUES (2, 'IT');
INSERT INTO departments (department_id, department_name) VALUES (3, 'Finance');
INSERT INTO employees (employee_id, name, department_id) VALUES (101, 'Alice', 1);
INSERT INTO employees (employee_id, name, department_id) VALUES (102, 'Bob', 2);
INSERT INTO employees (employee_id, name, department_id) VALUES (103, 'Charlie', 3);
Step 3: Now, create a view that joins the employees and departments tables
CREATE VIEW employee_details AS
SELECT e.employee_id, e.name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
Step 4: To allow INSERT operations on the employee_details view, we create an INSTEAD OF trigger
CREATE OR REPLACE TRIGGER emp_details_insert_trigger
INSTEAD OF INSERT ON employee_details
FOR EACH ROW
BEGIN
INSERT INTO employees (employee_id, name, department_id)
VALUES (:NEW.employee_id, :NEW.name,
(SELECT department_id FROM departments WHERE department_name = :NEW.department_name));
END;
Example 1: Inserting a New Employee into the HR Department
INSERT INTO employee_details (employee_id, name, department_name)
VALUES (104, 'David', 'HR');
Output:
Employee ID | Name | Department ID | Department Name |
---|
104 | David | 1 | HR |
Explanation: The trigger successfully maps 'HR' to department_id 1 and inserts the new employee, David, into the employees table.
Example 2: Inserting a New Employee into the IT Department
INSERT INTO employee_details (employee_id, name, department_name)
VALUES (105, 'Eva', 'IT');
Output:
Employee ID | Name | Department ID | Department Name |
---|
105 | Eva | 2 | IT |
Explanation: Eva is added to the employees table with the correct department_id for the IT department.
Example 3: Inserting a New Employee into the Finance Department
INSERT INTO employee_details (employee_id, name, department_name)
VALUES (106, 'Frank', 'Finance');
Output:
Employee ID | Name | Department ID | Department Name |
---|
106 | Frank | 3 | Finance |
Explanation: Frank is inserted into the employees table with the corresponding department_id for Finance.
Conclusion
PL/SQL INSTEAD OF triggers are a way to manage changes in database views that normally can’t be updated directly. These triggers let you control on data how you added data, changed, or removed, ensuring everything stays accurate and follows your business rules. By using INSTEAD OF triggers, you can make your database more flexible and work smoothly, even with complex views. This makes it easier to keep your data organized and reliable.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read