SQL Server creating table with clustered index without a primary key(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 使用没有主键的聚集索引创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL Server 使用没有主键的聚集索引创建表


基础教程推荐
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01