2010-04-22 50 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