Master PL SQL Triggers with Real World Examples Tutorial

In the realm of programming, particularly in the context of PL/SQL (Procedural Language/Structured Query Language), a trigger is a special kind of procedural code that is automatically executed in response to certain events on a particular tables. Triggers are used to maintain the integrity of data in a database. They are a way to automatically prevent illegal values from being inserted or updated, or to automatically generate or manipulate data based on the data being inserted or updated.

What is a SQL Trigger? - Essential SQL
What is a SQL Trigger? - Essential SQL

A trigger is associated with a table and is typically fired (or activated) when a specific event occurs – such as an INSERT, UPDATE, or DELETE operation. When the trigger is fired, it has the ability to execute a collection of SQL statements, including further INSERT, UPDATE, or DELETE operations.

MySQL Triggers: How to Automate Data Integrity
MySQL Triggers: How to Automate Data Integrity

Understanding the Syntax of PL/SQL Triggers

Let's first understand the basic syntax of a PL/SQL trigger. Here's a simple example:

SQL Triggers
SQL Triggers

```sql CREATE OR REPLACE TRIGGER trigger_name AFTER INSERT OR UPDATE OR DELETE ON table_name FOR EACH ROW DECLARE ... BEGIN ... -- PL/SQL statements EXCEPTION ... END; ```

The `AFTER` keyword specifies that the trigger needs to be fired after the event has occurred. `FOR EACH ROW` means that the trigger will fire for each individual row that is affected by the event.

oplas/Trigger Types in PL/SQL

7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio

In PL/SQL, there are four types of triggers - BEFORE, AFTER, INSTEAD OF, and FOR EACH STATEMENT. Each of these triggers fires at different times relative to a DML (Data Manipulation Language) operation.

  • BEFORE: Fires before the triggering event.
  • AFTER: Fires after the triggering event.
  • INSTEAD OF: Prevents the triggering event from happening and instead executes the trigger body.
  • FOR EACH STATEMENT: Executes once per triggering statement rather than once per row.

Common Uses of Triggers in PL/SQL

PL/SQL tutorial 18: DDL Trigger with Schema Auditing Example
PL/SQL tutorial 18: DDL Trigger with Schema Auditing Example

Below are some common use-cases of triggers in PL/SQL:

  1. Enforcing referential integrity: Triggers can be used to keep rows in related tables in sync.
  2. Auditing or logging: Triggers can be used to log changes to the database for auditing purposes.
  3. Calculating values: Triggers can be used to calculate values that are dependent on other values.

Examples of PL/SQL Triggers

SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial

Here is an example of a simple PL/SQL trigger that utilizes the `RAISE_APPLICATION_ERROR` function to prevent the insertion of negative values into a `salary` column of an `employees` table:

```sql CREATE OR REPLACE TRIGGER check_salary_positive BEFORE INSERT OR UPDATE OF salary ON employees FOR EACH ROW DECLARE ... BEGIN IF :OLD.salary < 0 OR :NEW.salary < 0 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary must be positive'); END IF; EXCEPTION ... END; ```

Finally, triggers can be a powerful and useful tool for maintaining data integrity and enforcing business rules. When used wisely, they can make your PL/SQL code more robust and maintainable. However, it's important to use them judiciously as over-use can make your code harder to understand and debug.

SQL Triggers with Examples
SQL Triggers with Examples
SQL Subqueries Explained with Examples | Beginner SQL Tutorial
SQL Subqueries Explained with Examples | Beginner SQL Tutorial
PL/SQL tutorial 15: DML Trigger With Before Insert, Update and Delete DML Examples
PL/SQL tutorial 15: DML Trigger With Before Insert, Update and Delete DML Examples
PL/SQL tutorial 16: Table Auditing using DML Triggers in Oracle PL/SQL
PL/SQL tutorial 16: Table Auditing using DML Triggers in Oracle PL/SQL
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Triggers in SQL | Triggers In Database | SQL Triggers Tutorial For Beginners | Edureka Rewind
Triggers in SQL | Triggers In Database | SQL Triggers Tutorial For Beginners | Edureka Rewind
PL/SQL tutorial 24: How To Create INSTEAD OF DELETE trigger in Oracle Database
PL/SQL tutorial 24: How To Create INSTEAD OF DELETE trigger in Oracle Database
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL IN NUTSHELL
SQL IN NUTSHELL
a cell phone with the text postgresql and other information on it
a cell phone with the text postgresql and other information on it
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis
SQL in 60 days
SQL in 60 days
7 SQL QUERIES EVERY STUDENT SHOULD MASTER
7 SQL QUERIES EVERY STUDENT SHOULD MASTER
PL/SQL tutorial 21: How To Create Startup Trigger In Oracle Database By Manish Sharma
PL/SQL tutorial 21: How To Create Startup Trigger In Oracle Database By Manish Sharma
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
Oracle PLSQL Tutorial 42- Before and After Trigger in PLSQL #PL/SQL #ora... Pl/sql Collection Method, Postgresql Like Operator, Sql Not Operator Syntax, Pl/sql Collection Tutorial, Pl/sql Tutorial 13, Oracle Sql Training, Pl/sql Tutorial 80, Pl/sql Collection, Pl/sql Tutorial 57
Oracle PLSQL Tutorial 42- Before and After Trigger in PLSQL #PL/SQL #ora... Pl/sql Collection Method, Postgresql Like Operator, Sql Not Operator Syntax, Pl/sql Collection Tutorial, Pl/sql Tutorial 13, Oracle Sql Training, Pl/sql Tutorial 80, Pl/sql Collection, Pl/sql Tutorial 57
SQL RIGHT() Explained with Examples | Extract Characters from the Right in SQL Server
SQL RIGHT() Explained with Examples | Extract Characters from the Right in SQL Server
an image of a diagram with words and numbers on it, including the number of columns in
an image of a diagram with words and numbers on it, including the number of columns in
SQL Query Execution Order Explained | Logical Order of SQL Query Processing
SQL Query Execution Order Explained | Logical Order of SQL Query Processing

Related Articles

Create Trigger Syntax Function To Handle Null Values In Sql Apex List Of Values Sql Query Pl Sql Function Return Multiple Values W3schools Update Sql Server Grouping Sets Sql Example Mysql Vs Microsoft Sql Server Management Studio Unique Values Sql Query Insert Multiple Values Sql Query Microsoft Sql Server Management Studio Query Designer