SQL Server creating table with clustered index without a primary key

前端 未结 3 1423
时光说笑
时光说笑 2021-02-04 02:58

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

The purpose of this is for a table in SQL Azure,

3条回答
  •  梦如初夏
    2021-02-04 03:23

    The code below is compatible with Azure. It creates a primary key non-clustered and a clustered index in a single create table statement. 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. In Azure you can not specify a clustered index in any other place other than the table create statement.

提交回复
热议问题