SQL Server 使用没有主键的聚集索引创建表

是否可以从 SQL Server 2008 中的非主键的 create table 语句创建聚集索引?

Is it possible to create a clustered index from a create table statement in SQL Server 2008 that is not a primary key?

这样做的目的是为了 SQL Azure 中的一个表,所以我不能先创建表,然后在表上创建聚集索引.

The purpose of this is for a table in SQL Azure, so it is not an option for me to first create the table, and then create the clustered index on the table.

显然是 FluentMigrator 导致了我的问题,它的版本表没有聚集索引,因此尝试创建版本控制表而不是我的表时出错.

Apparently it was FluentMigrator that was causing my problems, it's version table does not have a clustered index so it was erroring trying to create the versioning table not my table.

推荐答案

是的,可以创建一个不是主键的聚集索引.只需使用 CREATE CLUSTERED INDEX 语句.

Yes, it is possible to create a clustered index that is not the primary key. Just use a CREATE CLUSTERED INDEX statement.

CREATE TABLE dbo.myTable (
    myTableId int PRIMARY KEY NONCLUSTERED
    myColumn int NOT NULL
)

CREATE CLUSTERED INDEX myIndex ON dbo.myTable(myColumn)

在 Azure SQL 数据库 v12 版本之前,您必须先拥有聚集索引,然后才能将任何数据插入到表中.截至 Azure SQL 数据库 v12, 堆(没有聚集索引的表)现在支持.

Prior to version Azure SQL Database v12, you had to have a clustered index before you could insert any data to a table. As of Azure SQL Database v12, heaps (tables without a clustered index) are now supported.

如果您的数据库是在 2016 年 6 月之前创建的,这里是 升级到版本 12 的说明.

If your database was created prior to June 2016, here are the instructions for upgrading to version 12.

相关文章