Skip to main content

SQL Stored Procedures, Functions, Triggers and Transactions Explained

SQL Stored Procedures, Functions, Triggers and Transactions

SQL Stored Procedures, Functions, Triggers and Transactions Explained

As you advance in SQL, understanding procedures, functions, triggers, and transactions becomes essential for database automation and integrity. This article breaks down these concepts step-by-step.


Part 1: SQL Stored Procedures

Stored procedures are precompiled SQL statements saved in the database that can be executed repeatedly.

Syntax

CREATE PROCEDURE procedure_name()
BEGIN
  -- SQL statements
END;

Example

DELIMITER //
CREATE PROCEDURE GetAllCustomers()
BEGIN
  SELECT * FROM Customers;
END //
DELIMITER ;

Call it using:

CALL GetAllCustomers();

Part 2: SQL Functions

Functions are similar to procedures but return a single value and can be used in SQL expressions.

Syntax

CREATE FUNCTION function_name (param1 datatype)
RETURNS return_datatype
BEGIN
  -- SQL logic
  RETURN value;
END;

Example

CREATE FUNCTION GetTotalPrice(quantity INT, price DECIMAL(10,2))
RETURNS DECIMAL(10,2)
BEGIN
  RETURN quantity * price;
END;

Use like this:

SELECT GetTotalPrice(10, 5.50);

Part 3: SQL Triggers

Triggers are automatic actions fired by events such as INSERT, UPDATE, or DELETE.

Syntax

CREATE TRIGGER trigger_name
AFTER INSERT ON table_name
FOR EACH ROW
BEGIN
  -- SQL logic
END;

Example

CREATE TRIGGER after_insert_customer
AFTER INSERT ON Customers
FOR EACH ROW
BEGIN
  INSERT INTO LogTable(action, log_time)
  VALUES('Customer Added', NOW());
END;

This automatically logs whenever a new customer is added.

Part 4: SQL Transactions

SQL transactions group multiple SQL operations into a single unit that can be committed or rolled back together.

Keywords

  • BEGIN – starts a transaction
  • COMMIT – saves all changes
  • ROLLBACK – undoes all changes

Example

START TRANSACTION;
UPDATE Accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE Accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;

If any step fails, use:

ROLLBACK;

Best Practices

  • Use transactions for any operation that changes multiple rows/tables.
  • Validate inputs before using procedures or triggers.
  • Keep triggers minimal to avoid performance overhead.

Also Read:

FAQs

  • Q: What’s the difference between a procedure and a function?
    A: Functions return a value and can be used in queries. Procedures perform actions and cannot return values directly.
  • Q: When should I use transactions?
    A: Whenever multiple queries need to succeed or fail together.
  • Q: Do all databases support triggers?
    A: Most major databases do, including MySQL, PostgreSQL, and SQL Server.

Explore more SQL tutorials on Sabbir93s Blog.

Comments

Popular posts from this blog

Beatriz Taufenbach vs Natalie Burn: Real Truth Behind the Viral Toxic Trailer Scene (2026) — Explained

Beatriz Taufenbach vs Natalie Burn: Real Truth Behind the Viral Toxic Trailer Scene (2026) — Explained The teaser for “ Toxic: A Fairy Tale for Grown-Ups ” starring Yash exploded across the internet after its January 2026 release, not just for its gritty action but for one sensuous, viral moment in a cemetery sequence. Almost immediately, fans and content sites were searching terms like “Toxic trailer mystery actress,” “Beatriz Taufenbach identity,” and “Natalie Burn Toxic scene” to understand who the foreign woman in that intimate car sequence really is. What followed was a mix of misinformation, viral curiosity, and eventual clarification straight from the director’s official account. Here’s the complete, true story behind the confusion — what’s factual, what’s rumor, and who the real actress is. The Viral Moment That Sparked the Searches When the Toxic teaser dropped on January 8, 2026 — intentionally released on Yash’s birthday — millions of viewers were captivated by a bold and in...

How to Earn from Clipster.gg in 2025

How to Earn from Clipster.gg (2025 Guide) 💸 How to Earn from Clipster.gg in 2025 Clipster.gg is a platform where you can earn money from viral videos using three simple methods: logo promotion , UGC content , and content clipping . Let’s break them down: 🔥 1. Logo Promotion Pick any brand’s logo from the campaign section and simply overlay it on top of your short-form videos (Reels, Shorts, TikTok). Once your videos get views, you get paid based on performance. No need for fancy editing. 🎥 2. UGC Content Creation Create your own reaction, meme, skit or short-form video based on the campaign theme. If your video performs well (goes viral), you earn money based on views. This is perfect for creators who want to make engaging content. 📁 3. Content Clipping (Copy & Paste Method) Clipster provides ready-made clips in Google Drive or folders. You simply download the videos and repost them as Sho...

Headai – AI Marketing Platform to Automate Your Brand

Headai – AI Marketing Platform to Automate Your Brand Headai – AI Marketing Platform to Automate Your Brand Exclusive Access: Click to Sign Up 🚀 What is Headai? Headai is an AI-powered marketing platform that allows you to automate your entire brand strategy. From generating ads to writing content and analyzing performance, Headai does it all using artificial intelligence. No more manual ad creation or guessing what content works— Headai does it smartly for you! ✨ Key Features 🎯 AI-powered Ad Generator (Google, Facebook, Instagram) 📝 Smart Content Creation for Blogs & Social Media 📊 Performance Insights & Auto Optimization 📆 Social Media Scheduler with AI 🧠 Full AI Brand Manager 🔗 Internal Resource Also Read: SQL Joins and Subqueries Explained Step-by-Step — One of our mos...