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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 20:51:45

(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

MySQL and SQL-SERVER are different RDBMS. They have different capabilities and different syntax.

When using InnoDB, MySQL always makes the PK the clustered index.

SQL-SERVER, however, will let you create a PK without the CLUSTERED keyword, and let you use it within another index instead.

In both cases, PrimaryKeys and Indexes (clustered or not) can cover multiple fields.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!