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

  • Home
  • SEARCH
  • 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 8320895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:49:30+00:00 2026-06-08T22:49:30+00:00

I have a trigger that I want to run before insert a row into

  • 0

I have a trigger that I want to run before insert a row into a table:

CREATE TABLE IF NOT EXISTS `test`.`t1`(
  `idt1` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `myType` ENUM('A','B','C') NOT NULL DEFAULT 'A',
  PRIMARY KEY (`idt1`) )
ENGINE = InnoDB;

delimiter $$
CREATE TRIGGER mtCheck BEFORE INSERT ON t1
FOR EACH ROW
BEGIN
    set @action = NEW.myType;
IF (NEW.myType NOT LIKE 'B') THEN
            SET New.myType = 'C';
    END IF;
    set @action2 = NEW.myType;
END $$
delimiter ;

If I run the code below, since ‘A’ is a valid value for the EMUN, the @action is ‘A’ and the @action2 is ‘C’:

set @action = 'no action';
set @action2 = 'no action';
select @action, @action2;
start transaction;
insert into t1(idt1, myType) values (1, 'A');
select @action, @action2;
select * from t1;
rollback;
select * from t1;
select @action, @action2;

If I change the insert statement for one of the following examples, I receive the following errors:

insert into t1(idt1, myType) values (1, 'F');

-> ERROR 1265 (01000): Data truncated for column ‘myType’ at row 1

insert into t1(idt1, myType) values (1, null);

-> ERROR 1048 (23000): Column ‘myType’ cannot be null

Shouldn’t the trigger force the new value into the NEW.myType field and the insert statement continue with the right values? In this case both examples should enter value ‘C’ at the myType field in table t1.

Does it have anything to do with my sql_mode?

SELECT @@GLOBAL.sql_mode;

-> STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

SELECT @@SESSION.sql_mode;

-> STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Should I change it to STRICT_ALL_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION?

  • 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-08T22:49:32+00:00Added an answer on June 8, 2026 at 10:49 pm

    I believe that i found out my mistake.

    The temporary table created to insert/update the original table rows within a transaction, has the same restrictions as the original table.

    If this analysis is accurate, that means that before i can insert/update a table row, the database management system will validate the values that are being inserted/updated and will return the errors above, because i’m not providing valid data.

    I will have to leave the validation to the application, before establishing the connection to the database.

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

Sidebar

Related Questions

I have a trigger that check data before insert to another table IF NOT
is is possible in postgres to have a trigger on CREATE TABLE that will
I have an click function that I want to trigger outside of the function
I have a trigger that looks something like this: create or replace TRIGGER cluster_check
I have a trigger that stores changes made in a separate table when a
We have created a table with a trigger that updates a ModifiedDate field in
I have a trigger for insert/update/delete. That is working fine. Also, I need the
I have a client application that scrapes data into a raw table. This client
I have the following function that I want to run on page load and
I want the trigger to run after each and every record that gets inserted.

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.