Skip to content
Advertisement

Generate unique Identifier for table

This is a screanshot for the database

I’m trying to create a table in SQL Server. I want to generate a unique identifier 32 bit and be the primary key.

I define it as primary key and set the type – how I can turn on the generation? How I can set it to be 32 bit?

Advertisement

Answer

If you want a 32-bit unique identifier that is unique on each row . . . well, you have just defined the identity column:

create table . . . (
    id int identity primary key,
    . . .
);

This will, of course, be a sequential number, but that meets your conditions.

EDIT:

If you want newid() then use a default value:

create table . . . (
    id uniqueidentifier primary key default newid(),
    . . .
);

Of course, newsequentialid() is a better choice than newid(), but your sample code uses newid().

A uniqueidentifer is 16 bytes (128 bits), so it doesn’t meet your needs. I don’t think there is any other built-in mechanism for a 32-bit unique number that is guaranteed to be unique.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement