简体   繁体   English

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

[英]SQL Server creating table with clustered index without a primary key

Is it possible to create a clustered index from a create table statement in SQL Server 2008 that is not a primary key? 是否可以从SQL Server 2008中不是主键的create table语句创建聚簇索引?

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. 这个目的是针对SQL Azure中的表,因此我不能首先创建表,然后在表上创建聚簇索引。

Edit: 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. 编辑:显然它是导致我的问题的FluentMigrator,它的版本表没有聚集索引,因此尝试创建版本控制表而不是我的表是错误的。

Yes, it is possible to create a clustered index that is not the primary key. 是的,可以创建不是主键的聚簇索引。 Just use a CREATE CLUSTERED INDEX statement. 只需使用CREATE CLUSTERED INDEX语句即可。

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

CREATE CLUSTERED INDEX myIndex ON dbo.myTable(myColumn)

Prior to version Azure SQL Database v12, you had to have a clustered index before you could insert any data to a table. 在Azure SQL数据库v12版本之前,您必须先拥有聚簇索引,然后才能向表中插入任何数据。 As of Azure SQL Database v12 , heaps (tables without a clustered index) are now supported. Azure SQL数据库v12开始 ,现在支持堆(没有聚簇索引的表)。

If your database was created prior to June 2016, here are the instructions for upgrading to version 12 . 如果您的数据库是在2016年6月之前创建的,则以下是升级到版本12的说明

CREATE TABLE dbo.Table_1
    (
    Id int NOT NULL IDENTITY (1, 1) PRIMARY KEY NONCLUSTERED,
    SomeOtherUniqueColumn int NOT NULL CONSTRAINT Item4 UNIQUE CLUSTERED
)  ON [PRIMARY]

note the specification of nonclustered on the primary key 请注意主键上的非聚集规范

This will still work. 这仍然有效。

CREATE TABLE dbo.Table_1
    (
    SomeOtherUniqueColumn int NOT NULL CONSTRAINT Item4 UNIQUE CLUSTERED
)  ON [PRIMARY]

The code below is compatible with Azure. 以下代码与Azure兼容。 It creates a primary key non-clustered and a clustered index in a single create table statement. 它在单个create table语句中创建主键非聚簇和聚簇索引。 This syntax also allows for specifying more than one column in your key. 此语法还允许在密钥中指定多个列。

CREATE TABLE MyTable (
    ID uniqueidentifier  NOT NULL,
    UserID uniqueidentifier  NOT NULL,
    EntryDate DATETIME NOT NULL,
    CONSTRAINT PK_MyPrimaryKey_Name PRIMARY KEY NONCLUSTERED (ID),
    CONSTRAINT UCI_MyClusteredIndexName UNIQUE CLUSTERED (UserID ASC,EntryDate ASC,ID ASC)
);

In order to change a tables clustered index, the clusteredd index must be dropped, which converts the table into a heap and then the new clustered index is applied. 为了更改表聚簇索引,必须删除聚簇索引,这会将表转换为堆,然后应用新的聚簇索引。 Because Azure does not support heaps (tables without clustered indexes) it is not possible to change the clustered index without dropping the table and recreating it. 由于Azure不支持堆(没有聚簇索引的表),因此无法在不删除表并重新创建聚簇索引的情况下更改聚簇索引。 In Azure you can not specify a clustered index in any other place other than the table create statement. 在Azure中,除了表create语句之外的任何其他位置都不能指定聚簇索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server-更改聚簇索引而不删除主键? - SQL Server - Change clustered index without dropping primary key? 复合聚集索引作为主键与SQL Server中的堆表 - Composite clustered index as primary key vs heap table in SQL Server SQL Server:没有聚簇索引的关键查找 - SQL Server: Key Lookup without Clustered Index 在 SQL Server 中创建聚集主键索引需要很长时间? - Creating clustered primary key index takes a very long time in SQL Server? 使用 SQL Server Management Studio UI 将主键上的聚集索引替换为非聚集索引而不生成脚本 - Replace a clustered index with a non-clustered one on primary key using SQL Server Management Studio UI without generating a script SQL Server中主键(群集)和群集唯一索引之间的区别 - Difference between primary key (cluster) and clustered unique index in SQL Server Sql Server群集索引“分区”。 主键问题 - Sql Server Clustered Index “Partitioning”. Primary Key issue SQL Server中主键和唯一聚簇索引之间的性能差异 - Performance difference between Primary Key and Unique Clustered Index in SQL Server 使用唯一索引但没有主键在SQL Server中创建表有什么影响? - What is the impact of creating a table in SQL Server with a unique index but no primary key? 具有聚集索引主键的表中非聚集索引的结构 - Structure of non clustered index in table with clustered index primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM