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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:21:10+00:00 2026-05-26T04:21:10+00:00

I have got a table which has an id (primary key with auto increment),

  • 0

I have got a table which has an id (primary key with auto increment), uid (key refering to users id for example) and something else which for my question won’t matter.

I want to make, lets call it, different auto-increment keys on id for each uid entry.

So, I will add an entry with uid 10, and the id field for this entry will have a 1 because there were no previous entries with a value of 10 in uid. I will add a new one with uid 4 and its id will be 3 because I there were already two entried with uid 4.

…Very obvious explanation, but I am trying to be as explainative an clear as I can to demonstrate the idea… clearly.

  1. What SQL engine can provide such a functionality natively? (non Microsoft/Oracle based)
  2. If there is none, how could I best replicate it? Triggers perhaps?
  3. Does this functionality have a more suitable name?
  4. In case you know about a non SQL database engine providing such a functioality, name it anyway, I am curious.

Thanks.

  • 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-26T04:21:10+00:00Added an answer on May 26, 2026 at 4:21 am

    MySQL’s MyISAM engine can do this. See their manual, in section Using AUTO_INCREMENT:

    For MyISAM tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups.

    The docs go on after that paragraph, showing an example.

    The InnoDB engine in MySQL does not support this feature, which is unfortunate because it’s better to use InnoDB in almost all cases.

    You can’t emulate this behavior using triggers (or any SQL statements limited to transaction scope) without locking tables on INSERT. Consider this sequence of actions:

    1. Mario starts transaction and inserts a new row for user 4.
    2. Bill starts transaction and inserts a new row for user 4.
    3. Mario’s session fires a trigger to computes MAX(id)+1 for user 4. You get 3.
    4. Bill’s session fires a trigger to compute MAX(id). I get 3.
    5. Bill’s session finishes his INSERT and commits.
    6. Mario’s session tries to finish his INSERT, but the row with (userid=4, id=3) now exists, so Mario gets a primary key conflict.

    In general, you can’t control the order of execution of these steps without some kind of synchronization.

    The solutions to this are either:

    • Get an exclusive table lock. Before trying an INSERT, lock the table. This is necessary to prevent concurrent INSERTs from creating a race condition like in the example above. It’s necessary to lock the whole table, since you’re trying to restrict INSERT there’s no specific row to lock (if you were trying to govern access to a given row with UPDATE, you could lock just the specific row). But locking the table causes access to the table to become serial, which limits your throughput.

    • Do it outside transaction scope. Generate the id number in a way that won’t be hidden from two concurrent transactions. By the way, this is what AUTO_INCREMENT does. Two concurrent sessions will each get a unique id value, regardless of their order of execution or order of commit. But tracking the last generated id per userid requires access to the database, or a duplicate data store. For example, a memcached key per userid, which can be incremented atomically.

    It’s relatively easy to ensure that inserts get unique values. But it’s hard to ensure they will get consecutive ordinal values. Also consider:

    • What happens if you INSERT in a transaction but then roll back? You’ve allocated id value 3 in that transaction, and then I allocated value 4, so if you roll back and I commit, now there’s a gap.
    • What happens if an INSERT fails because of other constraints on the table (e.g. another column is NOT NULL)? You could get gaps this way too.
    • If you ever DELETE a row, do you need to renumber all the following rows for the same userid? What does that do to your memcached entries if you use that solution?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I've got a table called [Items] which has a primary key ItemID
I have two database tables: 'MyTable' which has a standard auto-incrementing primary key integer
I've got a table that people have been inserting into getting the primary key
I.E. if we have got a table with 4 million rows. Which has got
I've got a database which has a table called m_AttentionFlags which references a primary
I have got a simple MySQL table and primary index (id) is not numbered
i have bookings table which has two people- i want to return person_1 as
I've got a table of 5,651,744 rows, with a primary key made of 6
I have created a new table with no primary key. But the table does
I've got a table which has a XML field. The typical XML it contains

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.