Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What’s the difference between a primary key and a clustered index? [duplicate]

Tags:

sql

Possible Duplicate:
Performance difference between Primary Key and Unique Clustered Index in SQL Server

I make sure that I searched this forum but nobody asked this question before and I couldn't find any answer in anywhere too.

My question is = "What’s the difference between a primary key and a clustered index?"

like image 623
ms_jordan Avatar asked Jan 08 '13 10:01

ms_jordan


People also ask

Can clustered index have duplicates?

Yes, you can create a clustered index on key columns that contain duplicate values.

Is a clustered index a primary key?

The primary key is the default clustered index in SQL Server and MySQL. This implies a 'clustered index penalty' on all non-clustered indexes.

What is the difference between primary key and primary index?

The primary index is created automatically when the table is created in the database. Primary key is mandatory.it avoid the duplicate of data. for ex (student rollno, material no, employee id)it should be a unique. when you create the foreign key in the particular table before it should be one primary key.

Can primary key be duplicated?

You can define keys which allow duplicate values. However, do not allow duplicates on primary keys as the value of a record's primary key must be unique.


2 Answers

Well, for starters, one is a key, and the other one is an index.

In most database lingo, key is something that somehow identifies the data, with no explicit relation to the storage or performance of the data. And a primary key is a piece of data that uniquely identifies that data.

An index on the other hand is something that describes a (faster) way to access data. It does not (generally) concern itself with the integrity and meaning of the data, it's just concerned with performance and storage. In SQL Server specifically, a clustered index is an index that dictates the physical order of storage of the rows. The things that it does are quite complex, but a useful approximation is that the rows are ordered by the value of the clustered index. This means that when you do not specify a order clause, the data is likely to be sorted by the value of the clustered index.

So, they are completely different things, that kinda-sorta compliment each other. That is why SQL Server, when you create a primary key via the designer, throws in a free clustered index along with it.

like image 119
SWeko Avatar answered Oct 01 '22 21:10

SWeko


Primary key is unique identifier for record. It's responsible for unique value of this field. It's simply existing or specially created field or group of fields that uniquely identifies row.

And clustered index is data structure that improves speed of data retrieval operations through an access of ordered records. Index is copy of one part of table. It takes additional physical place on hard disk.

like image 23
kapandron Avatar answered Oct 01 '22 19:10

kapandron