
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Stored Procedure and Triggers in SQL
Stored Procedures
Stored Procedures is a group of pre-compiled SQL statements that is stored in a database and can be reused anytime. It allows you to do many things in one command. So doing several activities in a database will be easier and quicker than before. A stored procedure can be called from SQL commands as well as applications like Python, Java, or PHP.
Syntax
Following is the syntax to create a stored procedure ?
CREATE PROCEDURE procedure_name AS BEGIN -- SQL statements END;
Example
Let us create a stored procedure where it fetches the data from Employees table. To create table named Employees use the following code -
CREATE TABLE Employees ( Employee_id INT, Name VARCHAR(50), Salary DECIMAL(10, 2) );
To insert records into Employees table we use INSERT command.
INSERT INTO Employees VALUES (1, 'Harry', 50000.00), (2, 'Mohan', 60000.00);
Let's now construct a stored procedure that accesses every record in the Employees table.
DELIMITER # CREATE PROCEDURE ListAllEmployees() BEGIN SELECT * FROM Employees; END # DELIMITER ;Verification
To call the procedure and view the output.
- Use EXEC or EXECUTE in SQL Server, Oracle, or PostgreSQL.
- Use CALL in MySQL.
To retrieve the list of all employees and their details, we will execute the stored procedure ListAllEmployees(). Following is the query -
CALL ListAllEmployees();
Following is the output of the above code ?
Employee_id | Name | Salary |
---|---|---|
1 | Harry | 50000.00 |
2 | Mohan | 60000.00 |
With the help of a stored procedure, We can save time because it reduces repetitive code and makes it simple to retrieve data through a single call.
Triggers
Triggers in SQL enable us to describe automatic operations that would be conducted as a result of specific actions happening within tables, such as INSERT, UPDATE, or DELETE. Triggers are very useful to automate repetitive database operations and maintain data integrity.
Syntax
Following is the syntax to create a trigger ?
DELIMITER # CREATE TRIGGER trigger_name AFTER | BEFORE [INSERT, UPDATE, DELETE] ON table_name FOR EACH ROW BEGIN -- SQL statements END # DELIMITER;
Example
Let's create an AFTER INSERT trigger on the Employees table that logs each new insertion into the Log table. We have already created the Employees table in the above example. Let us now create a log table using the following code:
CREATE TABLE Log ( log_id INT AUTO_INCREMENT PRIMARY KEY, employee_id INT, action VARCHAR(50) );Creating the trigger for log insertion
This trigger automatically logs every insertion into the Employees table. When a new employee record is added, the trigger captures the employee_id and logs the action 'INSERT' into the Log table.
DELIMITER # CREATE TRIGGER after_employee_insert AFTER INSERT ON Employees FOR EACH ROW BEGIN INSERT INTO Log (employee_id, action) VALUES (NEW.employee_id, 'INSERT'); END # DELIMITER ;
To activate the trigger we need to insert new records into the Employees table.
INSERT INTO Employees (Employee_id, Name, Salary) VALUES (1, 'Alex', 33000.00);Verification
After the insert, the log table will contain:
SELECT * FROM Log;
Following is the output of the above code ?
log_id | employee_id | action |
---|---|---|
1 | 1 | INSERT |
This shows that the trigger activated successfully when we added a unique record to the employee table.
Stored Procedure vs Triggers
The table below compares the key differences between stored procedures and triggers, highlighting their behavior, usage, and capabilities in database management.
Triggers | Stored procedures |
---|---|
Trigger is a stored procedure that runs automatically when various events happen (e.g. update, insert, delete). | Stored procedures are pieces of the code written in PL/SQL to perform some specific task. |
It can execute automatically based on the events. | It can be invoked explicitly by the user |
It can not take input as a parameter. | It can take input as a parameter. |
We can't use transaction statements inside a trigger. | We can use transaction statements like begin the transaction, commit the transaction, and roll back inside a stored procedure. |
Triggers can not return values. | Stored procedures can return values. |
As they run automatically without explicit calls, they tend to be much less user-visible, so debugging becomes much harder. | It can be easily tested and debugged because you can run them directly and also have control over how they are executed. |
Used for enforcing rules automatically such as maintaining data integrity. | It is used to reduce the risk of SQL injection attacks. It increases the modularity and maintainability of code. |