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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:40:36+00:00 2026-05-12T14:40:36+00:00

I would like to have a trigger to perform following operation for inserted records:

  • 0

I would like to have a trigger to perform following operation for inserted records:

 # pseudocode
 if new.group_id is null
    set new.group_id = new.id
 else
    # don't touch it
 end

More clearly: say I have one table with three columns: id primary key, group_id int, value varchar.

When I insert with group_id like that:

INSERT INTO table(value, group_id) VALUES ('a', 10)

I’d like to have:

id | group_id | value
---+----------+------
 1 |       10 | a

but when I omit group_id:

INSERT INTO table(value) VALUES ('b')

it should be automatically set to the id of this record:

id | group_id | value
---+----------+------
 2 |        2 | b

Is it possible with a trigger? (I know I can update the record after inserting but having the trigger would be nicer.)

  • 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-12T14:40:36+00:00Added an answer on May 12, 2026 at 2:40 pm

    I don’t know of any way to do this in one statement, even using a trigger.

    The trigger solution that @Lucky suggested would look like this in MySQL:

    CREATE TRIGGER MyTrigger BEFORE INSERT ON MyTable
    FOR EACH ROW BEGIN
      SET NEW.group_id = COALESCE(NEW.group_id, NEW.id);
    END
    

    However, there’s a problem. In the BEFORE INSERT phase, the auto-generated id value hasn’t been generated yet. So if group_id is null, it defaults to NEW.id which is always 0.

    But if you change this trigger to fire during the AFTER INSERT phase, so you have access to the generated value of NEW.id, you can’t modify column values.

    MySQL doesn’t support expressions for the DEFAULT of a column, so you can’t declare this behavior in the table definition either. *Update: MySQL 8.0.13 supports DEFAULT (<expression>) but the expression still can’t depend on an auto-increment value (this is documented).

    The only solution is to do the INSERT, and then immediately do an UPDATE to change the group_id if it’s not set.

    INSERT INTO MyTable (group_id, value) VALUES (NULL, 'a');
    UPDATE MyTable SET group_id = COALESCE(group_id, id) WHERE id = LAST_INSERT_ID();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of logic I would like to implement as a trigger,
I have a problem that I would like have solved via a SQL query.
I would like to have a reference for the pros and cons of using
I would like to have an iframe take as much vertical space as it
I would like to have a VM to look at how applications appear and
We would like to have user defined formulas in our c++ program. e.g. The
I would like to have a nice template for doing this in development. How
I would like to have all developers on my team to use the same
I would like to have information about the icons which are displayed alongside the
I would like to have the same editor available on all of the platforms

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.