Munish Goyal
Munish Goyal

Reputation: 1409

Unique ID for database tables in .NET

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 ?

Upvotes: 0

Views: 436

Answers (2)

Mitch Wheat
Mitch Wheat

Reputation: 300797

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.

Upvotes: 1

Ghyath Serhal
Ghyath Serhal

Reputation: 7632

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.

Upvotes: 1

Related Questions