Primary indexing require sorted and unique value where clustered index require sorted value only. if we create primary index on table then it is hard to be other data in sorted form as well. so can we create both on same table? and what happens if we do that. i am not talking about relation between clustered index and primary key. let's give me one scenario, i have created table into that i have primary key on employee id , i want to create primary indexing on employee id, and assume that all the employees are also in sorted order by salary , so i want to create clustered index on salary. so is this possible ?
Asked
Active
Viewed 249 times
0
-
Possible duplicate of [Relationship of Primary Key and Clustered Index](https://stackoverflow.com/questions/15051869/relationship-of-primary-key-and-clustered-index) – tworogue Oct 20 '18 at 07:40
-
i am not talking about relation between clustered index and primary key. let's give me one scenario, i have created table that i have primary key on employee id , i want to create primary indexing on employee id, and assume that all the employees are also in sorted order by salary , so i want to create clustered index on salary. so is this possible ? – Vishant Akhani Oct 20 '18 at 10:14
-
1) A primary key doesn't have to be clustered, but usually is; 2) A table can only have one clustered index; 3) A clustered index does not have to be unique, but usually is. Combining these, you could in theory create a clustered index on salary and a non-clustered primary key on employee id, but it's almost certainly a bad idea. What exactly are you trying to achieve? See also https://stackoverflow.com/questions/4332982/do-clustered-indexes-have-to-be-unique – Joe Oct 20 '18 at 10:25
-
Thanks @Joe. i know it is bad idea but i just wanted to know is this possible or not. – Vishant Akhani Oct 20 '18 at 11:30
-
i have one doubt if you can solve it then please help me. When we create table with primary key then it will create clustered index on it. why not primary index ? – Vishant Akhani Oct 20 '18 at 11:32
-
When you create a table, you can create a nonclustered primary key if you want; it doesn't have to be clustered. See the following link for some examples (SQL Server): https://learn.microsoft.com/en-us/sql/relational-databases/tables/create-primary-keys?view=sql-server-2017 – Joe Oct 20 '18 at 15:46