In a database, what is the difference betwen a key and an index?

A key (declaration) is a constraint. It prevents rows from being entered into the table if any key data is missing, or if the key data duplicates a row that's already in the table.

An index is a data structure that allows rapid lookup of a row or rows given values for the columns (fields) used in the index. Indexes can be used to speed up certain other kinds of queries as well. For example, a merge join makes use of indexes on the two columns that make up the join condition, if those indexes are both there. Whether your DBMS will do a merge join depends on the query optimizer, the size of the tables, and the presence of the needed indexes.

To confuse the issue a little, some of the literature refers to the columns that an index uses as "index keys". The same literature usually refers to primary keys and foreign keys as "logical keys". That same literature will often refer to keys as logical features of a table, while indexes are called physical features of the table.

Most DBMSes will create an index for you when you declare a primary key. There are two reasons for this behavior. The first is that detecting duplicates without an index takes a long time on a big table. The second is that you will presumably be doing lots of lookups based on the primary key, and those lookups run much faster with an index.


Keys are used to maintain data integrity, indexes are used to maintain database performance. Think about whatever problem you're trying to solve and that will lead you in the right direction.


An field which has unique values is, essentially, a key. However, a key is used to uniquely identify a row in a table, while an index is used to sort, or group, the rows in the table. A key should not change once it has been initially set, as it might be referenced to elsewhere in your database. An indexed field, however, can change freely.


A key identifies the row stored in the database. An index is a structure like the one at the one at the end of a book. At the end of a book you see several pages with words and where you can find those words. Those pages are an index and the same is the case for a database. The index contains key and their locations. It helps you finding the location of rows. In case of a book the index tells you on which page you can find the word. The database index has the same function.

As many mention the index is implemented with b-trees. However this is only an implementation detail. There are many ways to implement an index.