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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:56:04+00:00 2026-05-11T13:56:04+00:00

Here’s an excerpt of my current database (changed the table-names for an easier understanding):

  • 0

Here’s an excerpt of my current database (changed the table-names for an easier understanding):

Pet(ownerFK, id, name, age) Owner(id, name) 

Where id is always a surrogate key, created with auto_increment.

I want to have the surrogate key Pet.id to be ‘scoped’ by Pet.ownerFK or in otherwords, have a composite key [ownerFk, id] as my minimum key. I want the table to behave like this:

INSERT Pet(1, ?, 'Garfield', 8); INSERT Pet(1, ?, 'Pluto', 12); INSERT Pet(2, ?, 'Mortimer', 1);  SELECT * FROM Pet;   RESULT:    Pet(1, 1, 'Garfield', 8)    Pet(1, 2, 'Pluto', 12)    Pet(2, 1, 'Mortimer', 1) 

I am currently using this feature of MyISAM where ‘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.’

However, due to various (and maybe obvious) reasons, I want to switch from MyISAM to InnoDB, as I need transactions at some places.

Is there any way how to achieve this effect with InnoDB?

I found some posts on this issue, many of them proposed to write-lock the table before insertion. I am not very familiar with this, but wouldn’t be a table-write-lock a little-bit of an overhaul for this one? I rather thought of having write-safe transactions (which I never did before) if these are possible – having a Owner.current_pet_counter as an helper field.

So another acceptable Solution would be…

Actually I don’t need the ‘scoped’ ID to be part of the actual Key. My actual database design uses a separate ‘permalink’ table which uses this ‘feature’. I currently use it as a workaround for the missing transactions. I thought of the following alternative:

 Pet(id, ownerFK, scopedId, name, age), KEY(id), UNIQUE(ownerFK, scopedId)  Owner(id, name, current_pet_counter)   START TRANSACTION WITH CONSISTENT SNAPSHOT;  SELECT @new=current_pet_counter FROM Owner WHERE id = :owner_id;  INSERT Pet(?, :owner_id, @new, 'Pluto', 21);  UPDATE Owners SET current_pet_counter = @new + 1 WHERE id = :owner_id;  COMMIT; 

I haven’t worked with transactions/transactionvars in MySQL yet, so I don’t know whether there would be serious issues with this one. Note: I do not want to reuse ids that have been given to a pet once. That’s why I don’t use MAX(). Does this solution have any caveats?

  • 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. 2026-05-11T13:56:05+00:00Added an answer on May 11, 2026 at 1:56 pm

    I don’t believe so. If you really had to have that schema, you could use a transaction to SELECT the MAX(id) WHERE ownerFK, then INSERT.

    I’m very sceptical there’s a good reason for that schema, though; the primary key is now also a fact about the key, which might make the database theorists unhappy.

    Normally you’d want ‘id’ to really be a proper primary key on its own, with ownerFK used to group and, if you needed it, a separate ‘rank’ column to put pets in a particular order per owner, and a UNIQUE index over (ownerFK, rank).

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

Sidebar

Related Questions

Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here's what I want to do: Given a table PeopleOutfit (id int primary key,
here is my configuration: http://domain.com (obviously fictitious name...) hosted on a server running Apache
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here is a snippet of my xaml: <ComboBox x:Name=cbo1 Width=100 SelectedValue=200> <ComboBoxItem Name=n1>100</ComboBoxItem> <ComboBoxItem
Here is my project Here I want to insert something into my database. I
Here's my proposed (very simplified to illustrate the problem space) design for a C#
Here the total height of all <div> 's are 900 pixels, but the jQuery
Here is a complete example. I want to forbid using A::set from objects casted
Here's the deal: I'm in the process of planning a mid-sized business application that

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.