0

Can someone list widely used methods for generating unique IDs to be used in DB tables (PK or otherwise)

Should the table field be string always (for flexibility) or GUID (tight coupling) or simply use IDENTITY always ?

If it is string then, of course i have option to change generator logic anytime.

With IDENTITY i feel constrained because i have to depend on database. But is it good option for fields to publish outside like an Invoice #.

For small size of data say 10000 records, what option i have to get smallest length unique but random alpha-numeric ?

I face many situations where i need such unique number for various purposes, if i am unable to think through a good option, i have to fallback to good old and quick .NET Guid.newGuid.ToString().

But i need to hear from experienced folks, What are the general guidelines and things to watch out for ?

Munish Goyal
  • 1,379
  • 4
  • 27
  • 49

2 Answers2

1

You can simply use the below in order to create a unique id

Guid.NewGuid();

personally i prefer to set the table field to unique identifier.

Ghyath Serhal
  • 7,466
  • 6
  • 44
  • 60
1

Always use the smallest ID possible.

If you do not need IDs generated in the business tier (e.g. many clients saving on round trip), use an IDENTITY column in your table, else use a GUID, but do not place your clustered index on the GUID column unless it's also a COMB GUID.

Community
  • 1
  • 1
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • but with identity you depend on database , additionally some places i need non-guessable id. Well, what is the generic method to achieve alpha-numeric random strings of given length. [The possibility of collision should decrease directly with increasing length.] – Munish Goyal Jan 07 '11 at 10:05