views:

128

answers:

5

It may looks a naive question but I am wondering about the relationship between primary keys and indexes in most common SQL databases.

Is it a standard rule for a SQL database to create automatically an index for every primary key?

I am asking that because I am designing a model with Django and I am wondering if it is redundant to set both primary_key=True and db_index=True.

+2  A: 

In MSSQL server, creating a primary key will normally create a clustered index by default .

ck
+3  A: 

Have a look at this

If a PRIMARY KEY constraint is created for a database table and no clustered index currently exists for that table, SQL Server automatically creates a clustered index on the primary key

from SQL Server Index Tuning

astander
+3  A: 

Yes, you can assume it is a standard rule that RDBMSes require an index for the primary key.

Mainly, the database engine needs the index to enforces data uniqueness for the primary key, and to do this quickly requires an index.

Daniel Vassallo
+1  A: 

In MySQL, primary key is a type of index (particular unique index).

Anwar Chandra
A: 

It tends to be the case in most RDBMSs that a primary key is implemented as an index.

This is not a requirement per-se, but a (pretty much) obvious optimization.

Oded