HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

SQL Server - storing ulong as primary key

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
primaryulongsqlserverstoringkey

Problem

I need to store ulong values as a primary key in SQL Server. Since SQL Server doesn't have a ulong datatype (or for that matter anything unsigned), the only way to go is to have column as varbinary(8).

Now, having a varbinary as a PK doesn't sound like a good idea. So what are the alternatives to store ulong values?

Solution

I'm assuming by ulong that you mean a 64-bit unsigned integer.

Your best option if you truly need to span the entire range of a ulong is to use NUMERIC(20,0) or larger. If you need to enforce the range of values to those valid for your data type, use a check constraint to restrict the values to between 0 and 18,446,744,073,709,551,615. Alternately, you could use VARBINARY(8), but your application would have to be base-aware.

Context

StackExchange Database Administrators Q#34033, answer score: 4

Revisions (0)

No revisions yet.