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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:00:42+00:00 2026-06-17T15:00:42+00:00

I have an invoices table (innoDB) in which i need to set manually the

  • 0

I have an invoices table (innoDB) in which i need to set manually the progressive number for the next invoice. My code now is

SELECT MAX(invoice_n) FROM invoices WHERE invoice_y = 2013

and then regulary save the record putting the new invoice_n = max + 1. I have an index UNIQUE on invoice_n-invoice_y and I’m logging db errors, so I see that sometimes I have duplicate key entry errors, beacuse I have hundreds of different users connected. I put the code in a loop that continues until the invoice is generated, but i think there can be a more elegant solution, especially using transactions. I read a bit but I can’t understand how can I achive my result with transactions.

Any help?

  • 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-17T15:00:43+00:00Added an answer on June 17, 2026 at 3:00 pm

    You could use “AUTO_INCREMENT” in your column definition. You will see some gaps between numbers if the insertion fails.

    Another alternative is creating a table with the last index per year (see comments) and follow the next steps:

    1. Begin a transaction
    2. Select for update last index per row and increment in one
    3. Insert the new invoice
    4. Commit your transaction

    Some links:

    • See MySQL (InnoDB) transaction model
    • Locking example

    Another one, you could use an “optimistic approach” and repeat the select and the insert if it fails because a duplicate key.

    I hope this helps you, any comment is welcome!

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

Sidebar

Related Questions

I have an invoices table which stores a single record for each invoice, with
I have an invoices and a payments table. For each invoice record (which contains
I have table invoices with field customer_id and some others fields. I need select
I have a html table like this: Invoice# Due Date Balance Select 12345 12/25/2011
I have a table which has a list of invoices and their details: class
I have a table called invoices with 3 fields (for the purposes of this
I have an customer invoice table in my MySQL database with a DECIMAL(10,2) field
I have two tables invoices and pending_payments both of which have the following rows
If I have an Invoice Line Items table and a Products table, and I
I have a Winform app which lists a batch of invoices in a gridview.

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.