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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:27:16+00:00 2026-06-05T07:27:16+00:00

I have a database table which has a compound primary key (id_a, id_b) .

  • 0

I have a database table which has a compound primary key (id_a, id_b). That’s not what they’re actually called but I am simplifying for this question. id_a is a foreign key and id_b is just an integer, “sequence number” so to speak. I cannot just make id_b auto_incrementing because it is not allowed for the InnoDB storage engine (as it is not the first field in the compound key).

I would like to make it so that these objects can be “grouped” with id_a, so for example:

id_a    id_b
----------------
1       1
1       2
2       1
2       2
2       3

and the result of inserting a row with (id_a=1) should automatically set id_b=3. I would like to avoid using database locks, especially since there is no need to lock the entire table when concurrent inserts with different id_a should totally be allowed.

I am accessing this database through Python via SQLAlchemy (using the ORM), although I suspect this question can be answered agnostically with regards to the client side (pure MySQL).

  • 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-05T07:27:17+00:00Added an answer on June 5, 2026 at 7:27 am

    SELECT coalesce(max(ID_B),0)+1
    FROM yourTable
    WHERE ID_A = '1'

    However, without the locking, you run the risk of getting duplicate ID_B values, so you’d have to handle duplicate key errors on the insert statements. Either “Retry” within code automatically for a set number of times or error and force user to try again.

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

Sidebar

Related Questions

I have a database table A which stores records, A has a primary key
I have a table which has no primary key and I can't add one
Have a linking (or ref) table which has a dual primary key. Need to
I have got a table which has an id (primary key with auto increment),
I have a Rails model called Person which has database table columns for first_name
I have a database which has a table that stores medical conditions and another
We have a Oracle 9i database and OrderDetails table which has a column to
I have a table which has essentially boolean values in a legacy database. The
I have a table (session) in a database which has almost 72,000 rows. I
I have a table in SQLite database of Android which has a column RANK

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.