Indexes and Keys

Chaitanya Kiran 796 Reputation points
2022-02-22T11:13:21.657+00:00

Creating a primary key automatically creates clustered index. What if the primary key is created on 2 columns? Will it create clustered index on 2 columns?
Non-clustered index doesn't sort?
I am asking because I have read mixed and multiple answers and I am confused. So, please confirm.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,865 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sreeju Nair 12,351 Reputation points
    2022-02-22T11:26:18.507+00:00

    When you create a composite primary key with 2 columns, it will create a single clustered index that uses both column values as clustering key.

    Hope this helps


3 additional answers

Sort by: Most helpful
  1. Dan Guzman 9,236 Reputation points
    2022-02-22T12:15:05.837+00:00

    SQL Server creates a unique index on the primary key columns. This index includes all key columns in the case of a composite key.

    You have the choice regarding whether the primary key index is clustered or non-clustered. SQL Server will create the primary key as clustered by default unless a clustered index already exists on the table. Although clustered is often the best choice for the clustered index (a table can have only one clustered index), you can explicitly specify non-clustered for the primary key index when a different clustered index is more beneficial to overall query performance.

    Non-clustered index doesn't sort?

    The leaf nodes of all b-tree indexes (clustered and non-clustered) are ordered by the index key(s). Clustered index leaf nodes are the actual table rows so those have all columns in the table. This allows the clustered index can be scanned to provide ordering by the clustered index key without the need for additional key lookups in the execution plan to retrieve additional columns required by a query.

    2 people found this answer helpful.
    0 comments No comments

  2. YufeiShao-msft 7,116 Reputation points
    2022-02-23T06:46:36.86+00:00

    Hi @Chaitanya Kiran

    A primary key that is made up of 2 or more columns is called a composite primary key, it is just like a primary key on a single comlumn, all the rules still apply.
    Create a composite key on table columm like:

    create table table_name (  
    column1 datatype1   ,  
    column1 datatype1  ,  
    column1 datatype1  , ..  
    primary key (column_name1, column_name2,..)   
    

    -------------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  3. Olaf Helper 44,816 Reputation points
    2022-02-22T11:58:54.88+00:00

    Creating a primary key automatically creates clustered index

    A PK as CI is default, but you can create the PK-index as non clustered index as well and the index can be composed over more then one column.

    Non-clustered index doesn't sort?

    A index is no garantuee for sorting, only ORDER BY clause do.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.