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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:50:28+00:00 2026-06-11T05:50:28+00:00

I am working on a project where I need my ID column to be

  • 0

I am working on a project where I need my ID column to be a power of 2 (1,2,4,8,16..). I know that we cannot offset the auto_increment but for simple addition/subtraction in my.cnf.

Example:

id
----
1
2
4
8
16
32
64
128
etc

One of the ideas I had was to use the auto increment functionality as the base, and then create a trigger to apply the power of 2 and update the new ID, but unfortunately, it is not working:

DELIMITER $$
CREATE TRIGGER testbitcompatid BEFORE INSERT ON Table
    FOR EACH ROW 
    BEGIN
        SET NEW.id = pow(NEW.id, 2)
    END;
$$
DELIMITER ;

Because the BEFORE INSERT has not yet generated the AUTO_INCREMENT id, the AUTO_INCREMENT will always return 0, essentially causing no change on the columns.

I also tried AFTER INSERT:

DELIMITER $$
CREATE TRIGGER testbitcompatid AFTER INSERT ON Table
    FOR EACH ROW 
    BEGIN
        SET Table.id = pow(NEW.id, 2) WHERE id = NEW.id;
    END;
$$
DELIMITER ;

But this failed because you cannot change values of the table which the trigger is applied to during an AFTER INSERT.

Scratching my head, but I am sure someone else has a great way of accomplishing this.

  • 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-11T05:50:29+00:00Added an answer on June 11, 2026 at 5:50 am

    To work around all the issues above, I was able to construct the following which works great!

    DELIMITER $$
    CREATE TRIGGER testbitcompatid BEFORE INSERT ON Table
        FOR EACH ROW 
        BEGIN
            SET @LAST_ROW = (SELECT MAX(id) FROM Table);
            SET NEW.id = CASE WHEN @LAST_ROW IS NULL THEN 1 ELSE @LAST_ROW * 2 END;
    
        END;
    $$
    DELIMITER ;
    

    Pretty much, we take the highest id, grab the log(2) of it which gives us the corresponding AUTO_INCREMENT id. We then add 1, and power that up to 2.

    I hope this helps prevent some headache down the road for others.

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

Sidebar

Related Questions

I'm working in a project that need to get SVD (Single Values Decomposition) for
I'm working on a project where I need to create a link that when
I'm working on a project that I need to eliminate all references to the
I was working on a project and I need to read a row in
I am working on a project where I need to automate some workflows in
I am working on a project and I need to save user configuration. The
I am working on this project where I need to read in a lot
I'm working on a project where I need to get the total row count
I am working on a project where I need to recursively iterate through the
I am currently working on a project where I need to implement a parallel

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.