How do I add auto_increment to a column in SQL Server 2008

柔情痞子 提交于 2019-12-01 05:12:26

To illustrate Martin's point:

And PS: - as Mikael Eriksson rightfully mentions (and documents nicely), this Identity Specification remains grayed out as long as that column you're working on has a default constraint.

Martin Smith

You need to expand the "Identity Specification" node to change it via the (Is Identity) property.

This will rebuild the table so you might also need to go into Tools -> Options -> Designers -> Prevent saving changes that require table re-creation.

This can be an extremely time consuming operation on large tables as well as entailing a lot of logging and locking. To perform this operation on a large table see my answer here.

Remove the default constraint of column Trans_ID first. Then you can set Is Identity to Yes in the designer.

This is properties for column Trans_ID in your table AR_Transactions. (Is Identity) is disabled:

Remove the default constraint and (Is Identity) is no longer disabled:

Set to yes and save. Default Value or Binding is disabled instead:

You can't use ALTER TABLE ... ALTER COLUMN to modify a column to have an identity property. You'll need to

  • drop the primary key constraint and any foreign key constraints referencing the column in question in your table.
  • add a new column with the identity property. It should have the same type (int, I presume) as the existing column.
  • update the table to seed the new column with the values of the existing column.
  • alter the new column to make it non-nullable.
  • drop the old/existing column.
  • rename the new column so that its name is the same as that of the old column.
  • Recreate the primary key and foreign key references you dropped in the 1st step.

Simple! Or something.

CREATE TABLE [dbo].[AR_Transactions](
       [Trans_ID] [bigint] IDENTITY(1,1) NOT NULL,
       [DateTime] [datetime] NOT NULL,
       [Cashier_ID] [nvarchar](50) NULL,
       [CustNum] [nvarchar](12) NOT NULL,
       [Trans_Type] [nvarchar](2) NOT NULL,
       [Prev_Cust_Balance] [money] NULL,
       [Prev_Inv_Balance] [money] NULL,
       [Trans_Amount] [money] NOT NULL,
       [Payment_Method] [nvarchar](4) NULL,
       [Payment_Info] [nvarchar](20) NULL,
       [Description] [nvarchar](38) NULL,
       [Invoice_Number] [bigint] NOT NULL,
       [Store_ID] [nvarchar](10) NOT NULL,
       [Dirty] [bit] NOT NULL,
       [Station_ID] [nvarchar](5) NULL,
       [Payment_Type] [smallint] NULL,

CONSTRAINT [pkAR_Transactions] 
       PRIMARY KEY CLUSTERED([Store_ID] ASC, [Trans_ID] ASC)
           WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
                 ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[AR_Transactions] 
    ADD CONSTRAINT [DF_AR_Transactions_Trans_ID_AR_Transactions] 
    DEFAULT ((0)) FOR [Trans_ID]

ALTER TABLE [dbo].[AR_Transactions] 
    ADD CONSTRAINT [DF_AR_Transactions_Invoice_Number_AR_Transactions] 
    DEFAULT ((0)) FOR [Invoice_Number]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!