Sobes.tech
Junior — Middle

What distinguishes the creation of a clustered index from defining a PRIMARY KEY in SQL?

sobes.tech AI

Answer from AI

PRIMARY KEY is a constraint that guarantees the uniqueness and non-null values in a column or a set of columns in a table. Usually, when creating a PRIMARY KEY, a clustered index is automatically created, which determines the physical order of data storage in the table.

A clustered index is an index that defines the order of storing rows in a table. In some DBMSs, it is possible to create clustered indexes separately from the PRIMARY KEY, for example, if the PRIMARY KEY is non-clustered.

Differences:

  • PRIMARY KEY is a logical data integrity constraint.
  • A clustered index is a physical data organization mechanism.

In most cases, creating a PRIMARY KEY results in the creation of a clustered index, but this is not always necessary and depends on the DBMS and its settings.