2010-04-22 53 views
14
Create table FavoriteDish  
( 
FavID int identity (1,1) primary key not null,  
DishID int references Dishes(DishID) not null ,  
CelebrityName nvarchar(100) nonclustered not null  
) 

此結果Incorrect syntax near the keyword 'nonclustered'。 我引用了MSDN對create table語法的幫助。我不知道這裏有什麼問題。如何在創建表中創建非聚集索引?

+0

的[創建CREATE TABLE語句中使用SQL Server非聚簇非唯一索引(可能的複製http://stackoverflow.com/questions/6193293/create-a-nonclustered-non-unique-index -within-the-create-table-statement-with-sq) – AaronLS 2016-06-01 22:56:27

+0

@AaronLS首先詢問這個特定問題的含義是否早於您指向的問題。這意味着你指向的那個可以被稱爲這個的重複,而不是反之亦然 – 2017-04-13 09:58:03

回答

16

書籍在線幫助確實提到了關鍵字CLUSTERED,但它僅與UNIQUE或PRIMARY KEY約束有關。這兩個約束都會創建一個索引,並且您可以指定該索引是聚類還是非聚類。

您不能使用該語法來創建標準的非聚集索引。

Create table FavoriteDish  
( 
FavID int identity (1,1) primary key not null,  
DishID int references Dishes(DishID) not null ,  
CelebrityName nvarchar(100) constraint ux_CelebrityName unique NONCLUSTERED not null  
) 
+0

aha,有道理。它應該是msdn上的一個條款。 – 2010-04-22 11:19:39

+5

不要忘記添加適當的命名約定,指數 PK_主鍵 UK_獨特的鍵 IX_非聚集的非唯一索引 UX_爲唯一索引 我所有的索引名的取 <索引或鍵的類型> _

_ _ _ mario 2012-07-27 13:45:03

10

刪除此非聚集關鍵字和使用CREATE INDEX語句添加索引,該表中,這個文件可以讀取:

http://msdn.microsoft.com/en-us/library/ms188783.aspx

語法如下:

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name 
    ON <object> (column [ ASC | DESC ] [ ,...n ]) 
    [ INCLUDE (column_name [ ,...n ]) ] 
    [ WHERE <filter_predicate> ] 
    [ WITH (<relational_index_option> [ ,...n ]) ] 
    [ ON { partition_scheme_name (column_name) 
     | filegroup_name 
     | default 
     } 
    ] 
    [ FILESTREAM_ON { filestream_filegroup_name | partition_scheme_name | "NULL" } ] 

[ ; ] 

代碼是在這裏:

CREATE NONCLUSTERED INDEX index_clustered ON FavoriteDish(CelebrityName asc) 
+4

Svisstack,謝謝你,我意識到這一點,但我可以不創建它在創建表語句。正如Create Table語法所說的那樣,它可以使用Create Table語句創建非唯一無規則索引 – 2010-04-22 10:27:23

+0

請參閱此處:http://stackoverflow.com/questions/6193293/create-a-nonclustered-non-unique-index -within-the-create-table-statement-with-sq – 2014-04-11 09:50:43