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

  • Home
  • SEARCH
  • 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 3623284
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:21:52+00:00 2026-05-18T23:21:52+00:00

Ok, let me explain the scenario. I have a orders table with a auto

  • 0

Ok, let me explain the scenario. I have a “orders” table with a auto incr key “orderno” in it. The table has a field “orderdate” also. What I want is a formatted order no. (orderno_formatted) in following style YYYYNUMBER i.e. date of order plus a unique no. and here is the twist. If no order is present for year 2010, “orderno_formatted” should be 20101 i.e. year of order and 1 and then 20102, 20103 etc.

next year in 2011, the (orderno_formatted) should be like 20111, 20112

so what is the best way to achieve this. I know, I can search for max no. in a column but it may be possible, that many users are accessing the site so If a get a max no and save it, meanwhile another user may get the same number.

clear?

any help will be appreciated.

Regards,

  • 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-18T23:21:53+00:00Added an answer on May 18, 2026 at 11:21 pm

    I suggest you create a separate sequence table with fields year and sequence. You then do:

    SELECT sequence FROM year_sequence WHERE year = 2010 FOR UPDATE

    This locks the table so that any consecutive selects on the same row will wait for the current transaction to commit (remember to begin a transaction before running SELECT … FOR UPDATE if your autocommit is enabled).

    After this you increment the sequence number you read by one, and push it back with:

    UPDATE year_sequence SET sequence = sequence + 1 WHERE year = 2010

    Alternatively if the first query did not read any rows, you’ll insert a new row with sequence of 1. Then you commit the changes with COMMIT.

    Now you got a sequence number you can use to insert to the another table, that is guaranteed to be unique for that year and there will be no conflicts as long as you remember to use this method anywhere you need to get the next sequence number.

    Another way would be to start a transaction, and first run an UPDATE that increments the sequence value by 1, and in the same transaction after the update read the new value, and only after that commit the transaction. That also ensures that the value you read will always be unique, as other transactions will wait until after you read the new value.

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

Sidebar

Related Questions

I have this alogirthm problem, in terms of creating mysql tables, let me explain
Let me explain the situation I have two tables: -One is to store the
7 tables. In each table there is a common entity called fisherid. I have
We have trouble with keeping apart our different configurations. Allow me to explain with
I have created a View Model in Silverlight. This View Model has an event
I am trying to migrate my project code from OSCache to EhCache. We have
This is a best practice question, and I expect the answer to be it
i am making a tree with the help javascript and jquery . I the
So this one is a bit odd to describe, please bear with me :)

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.