April 1, 2015 at 12:56 pm
Hello All,
I am trying to set "Is Identity property" To Yes on a column that has uniqueidentifier as a type and has a primary key constraint on it. Is this possible? If yes, could anyone please help with pointers.
Thank you,
April 1, 2015 at 1:18 pm
No, it's not possible.
The IDENTITY property can be assigned to tinyint, smallint, int, bigint, decimal(p,0), or numeric(p,0) columns. Only one identity column can be created per table.
April 1, 2015 at 4:16 pm
You have to use NEWID() to generate new values for a UNIQUEIDENTIFIER column. You might also want to look at NEWSEQUENTIALID() (in the same documentation).
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
April 1, 2015 at 7:49 pm
If you wanted to get a column to act like an identity column but to be a uniqueidentifier instead of an int you could do this:
CREATE TABLE dbo.yourtable
(
pk_col AS newid(),
xxx varchar(10) not null
);
GO
INSERT dbo.yourtable (xxx) VALUES ('www'),('rrr');
SELECT *
FROM dbo.yourtable;
The problem here is that you can't use the column as a Primary key or a Unique key or as part of a clustered index. 🙁
-- Itzik Ben-Gan 2001
April 1, 2015 at 9:24 pm
tomy200501 (4/1/2015)
Hello All,I am trying to set "Is Identity property" To Yes on a column that has uniqueidentifier as a type and has a primary key constraint on it. Is this possible? If yes, could anyone please help with pointers.
Thank you,
To be totally honest, using a UNIQUEIDENTIFIER as a Primary Key opens a world of hurt on you, especially if the PK is also the clustered index. I'd avoid it, if possible.
Perhaps we can help. Why do you think you need GUIDs (which are no longer guaranteed to be unique across multiple machines, btw).
--Jeff Moden
Change is inevitable... Change for the better is not.
April 4, 2015 at 4:17 am
Jeff Moden (4/1/2015)
tomy200501 (4/1/2015)
Hello All,I am trying to set "Is Identity property" To Yes on a column that has uniqueidentifier as a type and has a primary key constraint on it. Is this possible? If yes, could anyone please help with pointers.
Thank you,
To be totally honest, using a UNIQUEIDENTIFIER as a Primary Key opens a world of hurt on you, especially if the PK is also the clustered index. I'd avoid it, if possible.
Perhaps we can help. Why do you think you need GUIDs (which are no longer guaranteed to be unique across multiple machines, btw).
Further on Jeff's post, 99% fragmentation is not uncommon when using GUID as a clustered primary key, worse yet is that reorganizing efforts tend to be futile. As a result the server has to revert to random access reads which are very expensive.
😎
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply