SQL Server使用没有主键的聚集索引创建表 [英] SQL Server creating table with clustered index without a primary key
问题描述
是否可以通过不是主键的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 Database 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.
这篇关于SQL Server使用没有主键的聚集索引创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!