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 850045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:12:02+00:00 2026-05-15T07:12:02+00:00

I have a situation in which I don’t want inserts to take place (the

  • 0

I have a situation in which I don’t want inserts to take place (the transaction should rollback) if a certain condition is met. I could write this logic in the application code, but say for some reason, it has to be written in MySQL itself (say clients written in different languages will be inserting into this MySQL InnoDB table) [that’s a separate discussion].

Table definition:

CREATE TABLE table1(x int NOT NULL);

The trigger looks something like this:

CREATE TRIGGER t1 BEFORE INSERT ON table1
FOR EACH ROW
  IF (condition) THEN
    NEW.x = NULL;
  END IF;
END;

I am guessing it could also be written as(untested):

CREATE TRIGGER t1 BEFORE INSERT ON table1
FOR EACH ROW
  IF (condition) THEN
    ROLLBACK;
  END IF;
END;

But, this doesn’t work:

CREATE TRIGGER t1 BEFORE INSERT ON table1 ROLLBACK;

You are guaranteed that:

  1. Your DB will always be MySQL
  2. Table type will always be InnoDB
  3. That NOT NULL column will always stay the way it is

Question: Do you see anything objectionable in the 1st method?

  • 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-05-15T07:12:02+00:00Added an answer on May 15, 2026 at 7:12 am

    From the trigger documentation:

    The trigger cannot use statements that explicitly or implicitly begin or end a transaction such as START TRANSACTION, COMMIT, or ROLLBACK.

    Your second option couldn’t be created. However:

    Failure of a trigger causes the statement to fail, so trigger failure also causes rollback.

    So Eric’s suggestion to use a query that is guaranteed to result in an error is the next option. However, MySQL doesn’t have the ability to raise custom errors — you’ll have false positives to deal with. Encapsulating inside a stored procedure won’t be any better, due to the lack of custom error handling…

    If we knew more detail about what your condition is, it’s possible it could be dealt with via a constraint.

    Update


    I’ve confirmed that though MySQL has CHECK constraint syntax, it’s not enforced by any engine. If you lock down access to a table, you could handle limitation logic in a stored procedure. The following trigger won’t work, because it is referencing the table being inserted to:

    CREATE TRIGGER t1 BEFORE INSERT ON table1
    FOR EACH ROW
    
      DECLARE num INT;
      SET num = (SELECT COUNT(t.col)
                  FROM your_table t
                 WHERE t.col = NEW.col);
    
      IF (num > 100) THEN
        SET NEW.col = 1/0;
      END IF;
    
    END;
    

    ..results in MySQL error 1235.

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

Sidebar

Related Questions

I have a situation where I definitely don't want to get the whole domain
I have a SQL situation for which I don't have a solution. Combinations of
My situation is I have a legacy app which I don't have the code
I have a situation where I have an xml file that I don't want
I have situation in which I read a record from a database. And if
I have a situation in which a managed DLL calls some unmanaged DLL. I
I have a situation in which I need to convert a text data into
I have a situation in which user can single tap a control, which show
I have a situation in which the ideal relationship, I believe, would involve Value
I have a situation in which I am putting the UITableView in editing mode,

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.