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?
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?
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()
...
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
No one has followed this question yet.