Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

How do I create a revolving serialized numberfor a column in MySQL?


  • Please log in to reply
2 replies to this topic

#1 thejestre

thejestre

  • Members
  • 25 posts
  • OFFLINE
  •  
  • Local time:09:45 AM

Posted 24 September 2010 - 06:36 PM

Ok here is the deal:
On INSERT into the table I want to automatically create a serialized number and store it with the record. I want the range of the serial number to be between 1 and 150 and after it hits 150 it should start back at 1. Is there a way to do this in MySQL?

CREATE SEQUENCE would be perfect but apparently it is not supported in MySQL.

Thanks in advance,

_theJestre

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:10:45 AM

Posted 24 September 2010 - 07:48 PM

Not exactly sure why you would want to do that. Regardless, you have a couple of options. You could use whatever language you are using to insert the record to first check the previous number, then insert the record based on that number. The other option is ti use a stored procedure to do it automagically for you.

#3 thejestre

thejestre
  • Topic Starter

  • Members
  • 25 posts
  • OFFLINE
  •  
  • Local time:09:45 AM

Posted 24 September 2010 - 08:22 PM

Not exactly sure why you would want to do that.
...
The other option is ti use a stored procedure to do it automatically for you.


Ya, ya, that's what everyone says. "Why would you want to do that?"
This is a give-away system that creates a print out with a non unique number associated with it. That number is used to schedule the give-away in a paper log. Although the serial number is non unique, the give-aways are infrequent enough that no two live give-aways will have the same number.

What it really boils down to is the end users are use to using non unique serial numbers between 1 and 150.

This reminds me of something I heard in a programming class once:
It is easier to change the specification to fit the program than vice versa ;).

Thanks for the info on stored procedures, I was unaware of them. I'm pretty new to SQL so that looks like a great place to start learning.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users