创建非聚簇PK语法是否不一致?

Gen*_*нин 1 t-sql sql-server primary-key sql-server-2008-r2 non-clustered-index

SQL Server 2008 R2

为什么

create table A
(
   id int,
   primary key nonclustered (id)
)
Run Code Online (Sandbox Code Playgroud)

是正确的,执行没有错误?

create table A
(
   id int,
   primary key nonclustered id
)
Run Code Online (Sandbox Code Playgroud)

是错误的?给

')'附近的语法不正确.

附带问题:
为什么

create table c(id int primary key clustered)  
Run Code Online (Sandbox Code Playgroud)

被执行
但是

create table c(id int primary key nonclustered)
Run Code Online (Sandbox Code Playgroud)

是错误的?对不起,两个都有效.

是否建议纠正语法不一致?

gbn*_*gbn 5

(id)应该在括号中.像CHECK条件和FOREIGN KEY列一样.在一行:

create table A (id int, primary key nonclustered (id)) --ok
create table A (id int, primary key nonclustered id) --not ok
Run Code Online (Sandbox Code Playgroud)

注意:逗号表示"表级约束".没有逗号的其他语法意味着"列级约束".当然PK是每个表,你可以有复合键,不能在列级别定义:

create table A (
   id int,
   something datetime,

   primary key clustered (id,something)
)
Run Code Online (Sandbox Code Playgroud)

注意:对于名称,我总是明确我的约束.否则,SQL Server会生成十六进制查找.以及明确定义可空性和索引样式.所以我写这个:

create table A (
   id int NOT NULL,
   something datetime NOT NULL,

   CONSTRAINT PK_A PRIMARY KEY CLUSTERED (id,something)
)
Run Code Online (Sandbox Code Playgroud)

最后,根据CREATE TABLE,这是正常的,适用于我的SQL Server 2008实例:

 create table c(id int primary key nonclustered)
Run Code Online (Sandbox Code Playgroud)