Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:54:18+00:00 2026-06-14T21:54:18+00:00

New to MySql triggers, just learning. CREATE TRIGGER MyTrigger AFTER UPDATE ON MyTable FOR

  • 0

New to MySql triggers, just learning.

CREATE TRIGGER MyTrigger
AFTER UPDATE  ON MyTable
FOR EACH ROW
BEGIN
IF (new.field1 < 0 or new.field1 > 5) THEN
    UPDATE new SET new.field1 = old.field1;
END IF;
END;

The goal is to keep the value of field1 the same, if the update puts it outside the range.
However, instead it sets it to 0. What am I doing wrong? How should this code look?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:54:20+00:00Added an answer on June 14, 2026 at 9:54 pm

    Here is an example that should hopefully get you started:

    DELIMITER ~
    CREATE TRIGGER `so_13547992_trigger`
    BEFORE UPDATE ON `so_13547992`
    FOR EACH ROW BEGIN
        IF ( NEW.`field1` < 0 OR NEW.`field1` > 5 ) THEN
            SET NEW.`field1` = OLD.`field1`;
        END IF;
    END;
    ~
    

    Why would it work better? Well first of all your example trigger is recursive, you can’t update the same table in a trigger that was triggered by an update.

    Second, the new in your UPDATE statement is not a table name, you need to specify one explicitly.

    It doesn’t appear to be a legit trigger at all, doesn’t your server complain when you try to create it? Can you perhaps show actually SHOW CREATE TRIGGER `your_trigger`; to make sure that it’s really created and looks like you pasted it above?

    Even if your example would would work, you’re trying to do an unconstrained update on all rows of your table, not on the ones you’re trying to update, you should have a WHERE clause; again, given that issue one and two are taken care of.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we create a new MySQL database( not a table, I want a new
I've just created a new MySQL user with a password and granted them all
I just want an update trigger like this postgresql version... It seems to me
I've just learned the MySQL triggers and how they work. I decided to apply
I'm very new to MySQL triggers and procedure code in general, so this is
[New to DB triggers/transactions] In my app, my users are allowed to create, edit
I'm new to mysql triggers and I'm trying to figure it out how should
I'm working in phpMyAdmin and I'm new to creating MySQL 5.0.45 triggers. I'm trying
I made new trigger, which has to update the salary of employee. It takes
I'm relatively new to triggers in MYSQL, so sorry if this is a pretty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.