Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

For example, in SQLServer, if I have an index with 'unique' set on it, how is that different from a key?

How do I know when I want to use a Key vs. an Indexed field?

like image 540
jm. Avatar asked Apr 20 '09 17:04

jm.


People also ask

What is the difference between key and index in MySQL?

In MySQL, an index is a data structure used to quickly find rows. Indexes are also called keys and those keys are critical for good performance – as the data grows larger, the need of using indexes properly might become more and more important.

What is an index key in a database?

The index key is a null terminated string of null terminated tokens as shown in the following example: +Col1\0-Col2\0+Col3\0\0. The key in the example may be used to create an index over three columns in the table. Each column specified in the index is referred to as the "index segment" or "key column".

What is the difference between an index and a database?

An index is a database structure that you can use to improve the performance of database activity. A database table can have one or more indexes associated with it. An index is defined by a field expression that you specify when you create the index. Typically, the field expression is a single field name, like EMP_ID.

Is primary key and primary index same?

The primary index is created automatically when the table is created in the database. Primary key is mandatory.it avoid the duplicate of data. for ex (student rollno, material no, employee id)it should be a unique. when you create the foreign key in the particular table before it should be one primary key.


2 Answers

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.

like image 69
Elie Avatar answered Oct 13 '22 21:10

Elie


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.

like image 45
paweloque Avatar answered Oct 13 '22 20:10

paweloque