← Back to MySQL Course | Chapter 16: Triggers | Lesson 1 of 3

CREATE TRIGGER

एक trigger एक motion sensor light जैसा है: जब किसी table के साथ कुछ होता है, जैसे एक नई row, यह automatically on हो जाता है और अपना छोटा काम चलाता है।
Syntax
sql
CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE}
ON table_name
FOR EACH ROW
BEGIN
  -- statements using NEW.column / OLD.column
END

एक Trigger क्या है?

एक trigger database में stored code का एक block है जो एक specific table event के response में automatically fire होता है, एक INSERT, UPDATE, या DELETE के reaction में चलते हुए बिना application के कभी इसे directly call किए।

उदाहरण: What is a Trigger?

sql
DELIMITER //
CREATE TRIGGER log_new_user AFTER INSERT ON users
FOR EACH ROW
BEGIN
  INSERT INTO user_audit (user_id, action) VALUES (NEW.id, 'created');
END //
DELIMITER ;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

एक INSERT Trigger बनाना

एक INSERT trigger automatically चलता है जिस table से यह attached है उसमें एक नया row add होते ही, या तो row लिखे जाने से ठीक पहले या ठीक बाद, यह इस पर depend करता है कि यह कैसे defined है।

उदाहरण: Creating an INSERT Trigger

sql
DELIMITER //
CREATE TRIGGER before_user_insert BEFORE INSERT ON users
FOR EACH ROW
BEGIN
  SET NEW.created_at = NOW();
END //
DELIMITER ;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

NEW Values Access करना

एक INSERT या UPDATE trigger के अंदर, NEW keyword आपको लिखी जा रही row की actual values तक access देता है, trigger को save होने से पहले उन values को inspect या adjust करने देते हुए।

उदाहरण: Accessing NEW Values

sql
DELIMITER //
CREATE TRIGGER lowercase_email BEFORE INSERT ON users
FOR EACH ROW
BEGIN
  SET NEW.email = LOWER(NEW.email);
END //
DELIMITER ;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Trigger Execution Test करना

Triggers जब चलते हैं तो खुद announce नहीं करते — insert या update के दौरान खुद कोई visible confirmation नहीं होता, इसलिए यह verify करने का मतलब है बाद में जिस भी table को trigger affect करने वाला था उसे query करना।

उदाहरण: Testing Trigger Execution

sql
CREATE TABLE users (id INT, email TEXT);
CREATE TABLE user_audit (user_id INT, action TEXT);
INSERT INTO users VALUES (1, '[email protected]');
-- No confirmation is printed -- check the audit table instead
SELECT * FROM user_audit;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Trigger Restrictions

Triggers original statement चलाने वाले किसी को सीधे एक result set वापस नहीं दे सकते, और MySQL एक trigger को उस same table modify करने से भी block करता है जिसने इसे fire करवाया, infinite recursive triggering से बचने के लिए।

उदाहरण: Trigger Restrictions

sql
-- A trigger on users cannot modify users itself, only other tables,
-- to avoid infinite recursive triggering:
DELIMITER //
CREATE TRIGGER log_new_user AFTER INSERT ON users
FOR EACH ROW
BEGIN
  INSERT INTO user_audit (user_id, action) VALUES (NEW.id, 'created');
END //
DELIMITER ;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक AFTER trigger में SET NEW.col से एक row बदलने की कोशिश करना, जिसकी अनुमति सिर्फ BEFORE triggers में है।
  2. FOR EACH ROW भूल जाना, जो MySQL में required है।
  3. एक INSERT trigger में OLD या एक DELETE trigger में NEW इस्तेमाल करना, जहाँ वे rows exist ही नहीं करतीं।
🔒

Chapter Quiz — Complete all 3 topics to unlock

0/3 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.