如何使用标识列创建表
我有一张现成的表,我将要消失,因为我没有将ID
列设置为表格的标识列来创建它。
使用SQL Server Management Studio ,我编写了现有表的“Create To ...”,并得到了以下结果:
CREATE TABLE [dbo].[History](
[ID] [int] NOT NULL,
[RequestID] [int] NOT NULL,
[EmployeeID] [varchar](50) NOT NULL,
[DateStamp] [datetime] NOT NULL,
CONSTRAINT [PK_History] PRIMARY KEY CLUSTERED
(
[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]
我的问题是,我将如何修改此SQL
以便我的结果表将ID
列设置为Identity ?
CREATE TABLE [dbo].[History](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RequestID] [int] NOT NULL,
[EmployeeID] [varchar](50) NOT NULL,
[DateStamp] [datetime] NOT NULL,
CONSTRAINT [PK_History] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
) ON [PRIMARY]
这已经得到了回答,但我认为最简单的语法是:
CREATE TABLE History (
ID int primary key IDENTITY(1,1) NOT NULL,
. . .
当您真正想要更改选项时,更复杂的约束指数非常有用。
顺便说一句,我更喜欢命名这样的一列HistoryId,所以它匹配外键关系中的列的名称。
[id] [int] IDENTITY(1,1) NOT NULL,
当然,因为您在SQL Server Management Studio中创建表,您可以使用表设计器来设置标识规范。
链接地址: http://www.djcxy.com/p/94659.html