SQL Server 使用没有主键的聚集索引创建表 [英] SQL Server creating table with clustered index without a primary key

查看:59
本文介绍了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.

这篇关于SQL Server 使用没有主键的聚集索引创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆