0

I have a table structured as such:

PrimaryKey int set as PK SomeForeignKey int set as FK... linked to some other table's PK SomeVarChar varchar SomeDate datetime 

When I right-click on Properties of my PK, I noticed that Identity is set to False, Identity Seed is set to 0 and Identity Icrement is set to 0.

However, I am unable to modify these properties. How can I change these values?

Here is a screen shot.

enter image description here

2 Answers 2

3

You change the table, not the constraint. Management Studio will take care of the other stuff for you (note that it has to drop the table and re-create it, so if the table is large, be prepared to grab a coffee and hurry up and wait).

  • Right-click the table in Object Explorer and choose Design

enter image description here

  • Right-click the column you want to be the identity and choose Properties

enter image description here

In the properties pane, under "Identity Column", choose the correct column:

enter image description here

Click the Save icon (or Ctrl+S):

enter image description here

Note that you may also have to un-check the option Tools > Designers > Table and Database Designers > Prevent saving changes that require table re-creation.

enter image description here

Normally I would suggest making changes to schema using DDL instead of the hokey and bug-ridden GUI, but this is one rare case where the GUI actually requires less work than typing the requisite commands would. Changing the IDENTITY property is one thing DDL just hasn't caught up with, and the nonsense it has to do to work around it is ridiculous (in this case I added a new column, and scripted out the change required to make that the identity column instead - and you can't even see all the nonsense here, because of the non-resizable dialog):

enter image description here

Here is the actual script (not something you probably want to run very often, never mind come up with on your own):

/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/ BEGIN TRANSACTION SET QUOTED_IDENTIFIER ON SET ARITHABORT ON SET NUMERIC_ROUNDABORT OFF SET CONCAT_NULL_YIELDS_NULL ON SET ANSI_NULLS ON SET ANSI_PADDING ON SET ANSI_WARNINGS ON COMMIT BEGIN TRANSACTION GO CREATE TABLE dbo.Tmp_foo ( ID int NOT NULL, [Foo INT] int NOT NULL IDENTITY (1, 1) ) ON [PRIMARY] GO ALTER TABLE dbo.Tmp_foo SET (LOCK_ESCALATION = TABLE) GO SET IDENTITY_INSERT dbo.Tmp_foo OFF GO IF EXISTS(SELECT * FROM dbo.foo) EXEC('INSERT INTO dbo.Tmp_foo (ID) SELECT ID FROM dbo.foo WITH (HOLDLOCK TABLOCKX)') GO DROP TABLE dbo.foo GO EXECUTE sp_rename N'dbo.Tmp_foo', N'foo', 'OBJECT' GO ALTER TABLE dbo.foo ADD CONSTRAINT PK__foo__3214EC274CF5691D PRIMARY KEY CLUSTERED (ID) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO COMMIT 
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to change this property in Management Studio, you right click on the table, select Design, and then select the primary key column, and see its properties in the bottom, where you can open the "Identity Specification" section. There, you can edit the setting "IsIdentity". Then save the changes.

8 Comments

Thanks that worked! really hate management studio, not sure why the field isn't editable where I was looking...
@Rhs because the IDENTITY column is a property of the table, not the constraint.
@Rhs Actually, according to msdn.microsoft.com/en-us/library/ms187742.aspx, section 'IDENTITY', "You cannot modify an existing table column to add the IDENTITY property." This means that Management Studio actually does in the background if you change this setting for an existing table is: renaming the table, create a new one with the changed identity setting, copy all data from the renamed original to the new table, and then drop the renamed table. This may be an expensive action for large tables, hence I assume it is not enabled in the properties display that you refer to.
@FrankPI that is not why the identity setting is read-only in the properties for the primary key (after all, the UI lets you change the setting in other places). It's the same reason you don't change a database setting by right-clicking a stored procedure - the IDENTITY property is a property of the table, not a property of the primary key constraint.
@AaronBertrand That is an interesting point, even if I would see that as a physical detail, as from a user's point of view, it is a property of the column. And this is also reflected in the UI in Management Studio: you have to select the column in Table Designer and its Column Properties to change the setting. I assume that it is a table property is also the reason why you can only have one identity column per table.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.