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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:19:28+00:00 2026-06-16T09:19:28+00:00

I have an InooDB table: id – primary key, auto increment short_code – varchar(6),

  • 0

I have an InooDB table:

  • id – primary key, auto increment
  • short_code – varchar(6), unique
  • some other columns

The short_code is always generated in php based on id.
The problem is that I can’t generate the short_code before inserting a record, because I don’t know the id.

I tried the following:

  1. insert a record with empty short_code
  2. get the id of the inserted row with mysqli_insert_id, or pdo, whatever
  3. generate the short code based on the id, and update the record with the new short_code

This works, but this is really slow. I have to do an insert and also an update, which is time consuming.

A better idea is to “guess” the id the record I want to insert will have, with
select max(id)+1 from mytable; (or something similar), generate the short_code, and insert the record. Here I have a SELECT and one INSERT which is actually much better.

My question is:

  1. Is there a smarter solution for this problem?
  2. If no, and I go with solution 2 (guessing the next id), how can I make sure that in the time I do the select max(id)+1 from mytable;, generate short code, and insert a record, nobody will insert another record, so max(id)=1 doesn’t change in meantime?

NOTE!

  • I can not generate the short code in a mysql procedure.
  • Short code must be generated based on the id.
  • 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-16T09:19:29+00:00Added an answer on June 16, 2026 at 9:19 am

    Auto-inc value is generated when insert occurs, with a locking technique (to avoid generation on two same values) that depends on configuration and you cannot guess it. In a transactional environment many transaction can require a new id, but they may or may not commit, so at the time of the query, the value is reserved, but still not used.

    If you want to “guess” the correct value, you have to lock the entire table for write, to avoid another thread to insert a record and to break your guess. This will work if you don’t have a lot of writes on the table, e.g. for few dozen inserts per second.

    If there are a lot of updates on this table and write lock is not an option, you can use another table to generate the values (it will have only the id field), and instead of insert/update you will have insert/insert, assuming that the 2 operations will be as fast as they can.

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

Sidebar

Related Questions

I have (all InnoDB) a parent table tbl1 with primary key some_id and child
I have a table which has several ID columns to other tables. I want
I have a MySQL InnoDB table with 238 columns. 56 of them are TEXT
I have the following MySQL/InnoDB table. I added a compound index as the primary
I have a fairly static (InnoDB) table T with four columns: A , B
We have a MySQL InnoDB table holding ~10 columns of small base64 encoded javascript
I have a myism table 'test' which holds some out-dated data, now I want
I've got an index on columns a VARCHAR(255), b INT in an InnoDB table.
I have a InnoDB table that has about 17 normalized columns with ~6 million
I have an InnoDB table with a unique index on performance_id , ticket_rank_id and

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.