简体   繁体   English

在SQL Server 2005中添加主键无法正常工作

[英]Adding a primary key in SQL Server 2005 not working

I am pretty new to database stuff in general and I can't seem to get any sample code for creating a primary key to work. 一般而言,我对数据库工作还很陌生,而且似乎无法获得用于创建工作主键的任何示例代码。 I am using Microsoft SQL Server and the server type is SQL Server 2005 (90). 我正在使用Microsoft SQL Server,服务器类型为SQL Server 2005(90)。 The code I am currently trying to use is: 我当前尝试使用的代码是:

ALTER TABLE dbo.CustomerVisit
ALTER COLUMN CustomerID int NOT NULL;
ADD CONSTRAINT PK_CustomerVisit PRIMARY KEY CLUSTERED (CustomerID)
GO

But I am getting an error: 但我收到一个错误:

Incorrect Syntax near the keyword 'CONSTRAINT' 关键字“ CONSTRAINT”附近的语法不正确

I just created this table and it has no constraints or anything. 我刚刚创建了该表,它没有任何约束。 Just 3 columns. 仅3列。 I've also tried 我也尝试过

ADD PRIMARY KEY CustomerID;

but that results in 但这导致

Incorrect Syntax new the keyword 'PRIMARY' 语法不正确,新关键字“ PRIMARY”

add constraint comes after alter table . add constraint出现在alter table The ; ; ends the previous alter table , so you have to start the new statement with alter table again: 结束先前的alter table ,因此您必须再次使用alter table开始新语句:

ALTER TABLE dbo.CustomerVisit ADD CONSTRAINT PK_CustomerVisit 
    PRIMARY KEY CLUSTERED (CustomerID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM