Can I have a primary key without clustered index ? Also can I have multivalued clustered index?

后端 未结 2 1805
遇见更好的自我
遇见更好的自我 2021-01-19 10:23

Folks, I would like to understand the answer for the following questions:

  1. Can I have a primary key without clustered index ? ( I am aware that when we creat

2条回答
  •  天命终不由人
    2021-01-19 11:02

    (This answer is for SQL Server 2005+ only. I know nothing about MySQL.)


    Can I have a primary key without clustered index?

    Yes. As you mentioned, a primary key constraint is backed by a clustered index by default. You can tell SQL Server to back the constraint with a nonclustered index by declaring the constraint as follows:

    ALTER TABLE MyTable
        ADD CONSTRAINT PK_MyTable
            PRIMARY KEY NONCLUSTERED(Col1);
    

    Can I have a clustered index with multiple columns together ? (Like in non-clustered where I can join different columns for a single non-clustered index).

    Yes, you can define an index with more than one column in the index key. It's really no different than a nonclustered index.

    CREATE UNIQUE CLUSTERED INDEX IX_MyTable_Clus
        ON MyTable(Col1, Col2, Col3);
    

    References: ALTER TABLE, CREATE INDEX

提交回复
热议问题