4

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 create primary key constraint on a column, it by default creates a clustered index. So in that case, how should I deactivate clustered index ?)

  2. 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).

2 Answers 2

7

(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

1
  • that perfectly answered my questions the way I could chew/digest. Thank you so much :) :) Cheers
    – Jasmine
    Aug 8, 2012 at 17:09
2

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.

3
  • 1
    Also, InnoDB always has a clustered index even if you don't have a primary key.
    – Vatev
    Aug 8, 2012 at 12:49
  • @Dems: Hi Dems, thank you for the answer. Apologize, I duno what is InnoDB, I am being newbie and sorry for silly questions. And, how do I create a PK without Clluetersted index ? I mean if I just create a table with syntax and mentioning primary key and or using the GUI tool for creating table wherein I just tick the column I need as primary key. I am really new :( Hope you understand what exactly I am behind :( I need to know the concrete way of achieving it.
    – Jasmine
    Aug 8, 2012 at 12:52
  • 2
    The statement SQL-SERVER ... will let you create a PK without the CLUSTERED keyword, and let you use it within another index instead. is incorrect -- omitting the CLUSTERED keyword will still create a clustered index by default. You have to use the NONCLUSTERED keyword.
    – Jon Seigel
    Aug 8, 2012 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.