1

SQL Server 2008 R2的創建非集羣PK語法是否不一致?

爲什麼

create table A 
(
    id int, 
    primary key nonclustered (id) 
) 

是正確的,沒有錯誤執行?

create table A 
(
    id int, 
    primary key nonclustered id 
) 

是一個錯誤?給予

')'附近的語法不正確。

抵押品的問題:爲什麼

create table c(id int primary key clustered) 

執行

create table c(id int primary key nonclustered) 


是一個錯誤?
對不起,這兩個工作。

建議糾正的語法是否不一致?

回答

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 

注:逗號意味着「表級約束」。其他語法沒有逗號表示「列級約束」。當然的PK每桌,你可以有不能在列級定義組合鍵:

create table A (
    id int, 
    something datetime, 

    primary key clustered (id,something) 
) 

注:我始終明確我的限制太多了名。否則SQL Server會生成一個十六進制。以及明確定義可空性和索引風格。所以我寫這篇文章:

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

    CONSTRAINT PK_A PRIMARY KEY CLUSTERED (id,something) 
) 

最後,根據CREATE TABLE,這是OK,我的SQL Server上的作品2008實例:

create table c(id int primary key nonclustered) 
+0

+1,謝謝,但我不明白沒有逗號的語法。問題是關於語法與(不)括號過多。 – 2010-11-07 20:32:41

0

括號是必需的,因爲否則就沒有辦法從它後面的任何內容(可能是列或其他約束)中分隔PRIMARY KEY列列表。

相關問題