Comparison between the two
Below is a simple comparison table
|
Primary Key |
Clustered Index |
Purpose |
Forced table entity integrity |
Sorts data rows for easy Query |
Number of tables in a table |
A table has at most one primary key. |
A table can have at most one clustered index. |
Can multiple fields be defined? |
One primary key can be defined by multiple fields |
One index can be defined by multiple fields. |
|
|
|
Whether NULL data rows are allowed |
If the data in the data column to be created is null, a primary key cannot be created. The specified primary key constraint column during table creation is implicitly converted to not null. |
Columns that do not limit the creation of clustered indexes must not be null. That is, the column data can be null. See the last comparison. |
Must the data be unique? |
Data must be unique |
Data can be unique or not unique. See the unique settings you have defined for this index. (In this case, we need to look at the following comparison. Although your data columns may not be unique, the system will generate a unique column that you cannot see for you) |
|
|
|
Created Logic |
When the database creates a primary key, it automatically creates a unique index. If no clustered index exists in the table and the non-clustered index is not specified when the primary key is created, a unique clustered index is created when the primary key is created. |
If the unique attribute is not used to create a clustered index, the database engine automatically adds a four-byte uniqueifier column to the table. If necessary, the database engine automatically adds a uniqueifier value to the row to make each key unique. This column and column value are used internally and cannot be viewed or accessed by users. |