views:

192

answers:

5

When i should use primary key or index?

Which are the differences between primary key and index and which is the best?

+4  A: 

This might help Back to the Basics: Difference between Primary Key and Unique Index

The differences between the two are:

  1. Column(s) that make the Primary Key of a table cannot be NULL since by definition, the Primary Key cannot be NULL since it helps uniquely identify the record in the table. The column(s) that make up the unique index can be nullable. A note worth mentioning over here is that different RDBMS treat this differently –> while SQL Server and DB2 do not allow more than one NULL value in a unique index column, Oracle allows multiple NULL values. That is one of the things to look out for when designing/developing/porting applications across RDBMS.
  2. There can be only one Primary Key defined on the table where as you can have many unique indexes defined on the table (if needed).
  3. Also, in the case of SQL Server, if you go with the default options then a Primary Key is created as a clustered index while the unique index (constraint) is created as a non-clustered index. This is just the default behavior though and can be changed at creation time, if needed.
alejandrobog
+2  A: 

Basically, a primary key is (at the implementation level) a special kind of index. Specifically:

  • A table can have only one primary key, and with very few exceptions, every table should have one.
  • A primary key is implicitly UNIQUE - you cannot have more than one row with the same primary key, since its purpose is to uniquely identify rows.
  • A primary key can never be NULL, so the row(s) it consists of must be NOT NULL

A table can have multiple indexes, and indexes are not necessarily UNIQUE. Indexes exist for two reasons:

  • To enforce a uniquness constraint (these can be created implicitly when you declare a column UNIQUE)
  • To improve performance. Comparisons for equality or "greater/smaller than" in WHERE clauses, as well as JOINs, are much faster on columns that have an index. But note that each index decreases update/insert/delete performance, so you should only have them where they're actually needed.
Michael Borgwardt
A primary key is a *constraint*, **not an index**. An index can be associated *with* a primary key, but it's not required. And unless using SQL Server, an index doesn't ensure uniqueness.
OMG Ponies
@OMG: technically, the primary key constraint (just like a unique constraint) will always be implemented through an index. And an index ensures uniquness if it's declared so.
Michael Borgwardt
OMG Ponies
@OMG: MySQL and PostgreSQL both have a CREATE UNIQUE INDEX syntax. And the Postgres manual says "PostgreSQL automatically creates a unique index when a unique constraint or a primary key is defined for a table. The index (...) is the mechanism that enforces the constraint." http://www.postgresql.org/docs/8.0/interactive/indexes-unique.html -- I think our disagreement stems from me considering the concept of index as an implementation detail while you concentrate on indexes as an abstract part of the SQL DDL.
Michael Borgwardt
My point is that indexes are not ANSI, and your statements don't accurately cover all databases.
OMG Ponies
A key is not an index and the existence of one does not imply the existence of the other.
dportas
@David: I'm pretty sure that no database programmer in their right mind would allow the existence of a unique or primary key constraint without an index to implement it efficiently, even if that index is an invisible implementation detail. But I'm open to being proven wrong: show me a DB that allows you to define a primary key that is implemented by doing a full table scan for each insert...
Michael Borgwardt
A number of DBMSs can support keys without corresponding indexes. Oracle is one example already mentioned. You might want a key without an index if the table is small or seldom updated, or if indexes on other attributes are already sufficient (especially if the table has more than one key or if the key in question is a subset or superset of an index already). In all cases keys and indexes are quite different concepts and it's a mistake to think of a key as a type of index.
dportas
@David: Someone at Stanford disagrees about Oracle: "Oracle automatically creates an index for each UNIQUE or PRIMARY KEY declaration. Note that you cannot drop indexes for UNIQUE and PRIMARY KEY attributes." http://infolab.stanford.edu/~ullman/fcdb/oracle/or-nonstandard.html#indexes - And yes, keys and indexes are conceptually different, but on the implementation level, and index will be used to efficiently enforce a key or unique constraint.
Michael Borgwardt
+2  A: 

The primary key is by definition unique: it identifies each individual row. You always want a primary key on your table, since it's the only way to identify rows.

An index is basically a dictionary for a field or set of fields. When you ask the database to find the record where some field is equal to some specific value, it can look in the dictionary (index) to find the right rows. This is very fast, because just like a dictionary, the entries are sorted in the index allowing for a binary search. Without the index, the database has to read each row in the table and check the value.

You generally want to add an index to each column you need to filter on. If you search on a specific combination of columns, you can create a single index containing all of those columns. If you do so, the same index can be used to search for any prefix of the list of columns in your index. Put simply (if a bit inaccurately), the dictionary holds entries consisting of the concatenation of the values used in the columns, in the specified order, so the database can look for entries which start with a specific value and still use efficient binary search for this.

For example, if you have an index on the columns (A, B, C), this index can be used even if you only filter on A, because that is the first column in the index. Similarly, it can be used if you filter on both A and B. It cannot, however, be used if you only filter on B or C, because they are not a prefix in the list of columns - you need another index to accomodate that.

A primary key also serves as an index, so you don't need to add an index convering the same columns as your primary key.

Michael Madsen
+1  A: 

Differences

A table can only have one primary key, but several indexes.

A primary key is unique, whereas an index does not have to be unique. Therefore, the value of the primary key identifies a record in a table, the value of the index not necessarily.

Primary keys usually are automatically indexed - if you create a primary key, no need to create an index on the same column(s).

When to use what

Each table should have a primary key. Define a primary key that is guaranteed to uniquely identify each record.

If there are other columns you often use in joins or in where conditions, an index may speed up your queries. However, indexes have an overhead when creating and deleting records - something to keep in mind if you do huge amounts of inserts and deletes.

Which is best?

None really - each one has its purpose. And it's not that you really can choose the one or the other.

I recommend to always ask yourself first what the primary key of a table is and to define it.

Add indexes by your personal experience, or if performance is declining. Measure the difference, and if you work with SQL Server learn how to read execution plans.

marapet
A: 

Keys and indexes are quite different concepts that achieve different things. A key is a logical constraint which requires tuples to be unique. An index is a performance optimisation feature of a database and is therefore a physical rather than a logical feature of the database.

The distinction between the two is sometimes blurred because often a similar or identical syntax is used for specifying constraints and indexes. Many DBMSs will create an index by default when key constraints are created. The potential for confusion between key and index is unfortunate because separating logical and physical concerns is a highly important aspect of data management.

As regards "primary" keys. They are not a "special" type of key. A primary key is just any one candidate key of a table. There are at least two ways to create candidate keys in most SQL DBMSs and that is either using the PRIMARY KEY constraint or using a UNIQUE constraint on NOT NULL columns. It is a very widely observed convention that every SQL table has a PRIMARY KEY constraint on it. Using a PRIMARY KEY constraint is conventional wisdom and a perfectly reasonable thing to do but it generally makes no practical or logical difference because most DBMSs treat all keys as equal. Certainly every table ought to enforce at least one candidate key but whether those key(s) are enforced by PRIMARY KEY or UNIQUE constraints doesn't usually matter. In principle it is candidate keys that are important, not "primary" keys.

dportas