tags:

views:

297

answers:

7

Hi all

just wondering does anyone in here have good idea about generating nice order id?

for example

832-28-394, which show a quite nice and formal order id (rather than just use an database auto increment number like ID=35).

the order id need to look random so it can not be able to guess by user.

e.g. 832-28-395 (shoudnt exist) so there will always some gap between each id.

just like the account number for your bank card?

Cheers

+1  A: 

If you are using .NET you can use System.Guid.NewGuid()

tster
GUIDs have their uses, however beware that they require a lot of storage space (16 bytes, specifically), and that this amount is not typically needed for most applications. And at display-time they are not what I would call "pretty looking" or debug friendly...
mjv
+1  A: 

The auto-incremented IDs are stored as integer or long integer data. One of the reasons for this is that this format is compact, saving space, including in indexes which are typically inclusive a primary key for use with joins and such.

If you wish to create a nice looking id following a particular format syntax, you'll need to manage the generation of the IDs yourself, and store these in a "regular" column not one that is auto-incremented.

I suggest you keep using "ugly looking" ids, be they auto-incremented or not, and format these value for display purposes only, using whatever format you may desire, including some format that use the values from several columns. Depending on the database system you are using you may be able to declare custom functions, at the level of the database itself, allowing you to obtain the readily formatted value with a simple query (as in

SELECT MakeAFancyId(id_field), some_other_columns, .. FROM ...

If you cannot use some built-in or custom function at the level of SQL, you'll need to format the value supplied by SQL (an integer of sorts), into the desired format, on the client-side, using the language associated with your UI / presentation framework.

mjv
+1  A: 

I'd create something where the first eight numbers are loosely in a pattern, and a third quartet looks random but is really a sort of checksum. So, for example, the first eight digits increment based on the current seconds on the server clock. The last four could be something like the sum of the first four, plus twice the sum of the second four, which will give either a two or three digit number. The final digit is calculated so that the sum of all 11 digits plus this last one is a multiple of 9. This is slightly akin to how barcode numbers are verified. You can format the resulting 12 digits any way you want, although it is the first eight that are unique here.

CodeByMoonlight
A: 
  1. Hash the clock time.

  2. Mod by 100,000 or something.

  3. Format with hyphens.

  4. Check for duplicates. If found, restart.

recursive
A: 

Hi,

I would suggest using a autoincrement ID in the database to link tables and as a primary key. Integer fields are always faster than string fields for indexing and well as searching.

You can have the order number field (which is for display) as a different field in the order table which will be used to display. And whenever you are planning to send a URl to a user or display a URL to the user which has order ID (which is a autoincremented number) you can encrypt it with some algorithm.

Both your purpose will be solved.

But I suggest not to make string as primary key. Though you can have a unique constraint on the order number which is going to be displayed.

Hope this helps.

Kalpak Luniya

Kalpak
A: 

I would suggest internally you keep the database derived primary key, which is auto-incremented.

For the visible order number, you will probably need a longer length than 8 characters, if you are using this for security.

If you are using Ruby, look at SecureRandom, which will generate sufficiently random strings to accomodate this. For example, you can use SecureRandom.hex(16), and it will give you a 16 digit hex number. I believe it can also give you base 64 strings, which will look weirder but be shorter.

Make sure this is not your only security on an order, as it may not be that hard to find a valid order number within your 8 digit code, especially if some are some sort of checksum.

ndp
A: 

For security reasons i suggest that you should use Criptographicaly secure random number generator. Think about idea on icreasing User Id length -if you have 1 million users then the probability to gues User ID in first try is 0.01 and 67 tries to increase probability over 0.5

ralu