question

Heather 1 avatar image
Heather 1 asked

Column properties for Unique Identifier column

I have to create a Primary Key by creating a column with Uniqueidentifier data type. I want the numbers to auto generate. Other than creating the column and setting it as a primary key, what do I need to change under Column Properties?

uniqueguidglobally-unique-identifienewidnewsequentialid
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image
Grant Fritchey answered

You'll need to create a default value for the column as part of the table definition:

CREATE TABLE...
[MyColumn] [uniqueidentifier] NOT NULL CONSTRAINT [MyDefaultValue] DEFAULT NEWID()
...

But, you should read up on the methods for using sequential values for GUID's. It makes them less likely to fragment badly. Then, the same column would be:

...
[MyColumn] [uniqueidentifier] NOT NULL CONSTRAINT [MyDefaultValue] DEFAULT NEWSEQUENTIALID()
...
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Slick84 avatar image
Slick84 answered

You can specify IDENTITY(1,1)

Example:

CREATE TABLE jobs
(
job_id smallint
IDENTITY(1,1) PRIMARY KEY CLUSTERED,
job_desc varchar(50) NOT NULL DEFAULT 'New Position - title not formalized yet',
min_lvl tinyint NOT NULL CHECK (min_lvl >= 10),
max_lvl tinyint NOT NULL CHECK (max_lvl <= 250)
)

Source: http://doc.ddart.net/mssql/sql70/create_7.htm

Thanks,
S

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.