Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use primary key or index?

When should I use a primary key or an index?

What are their differences and which is the best?

like image 749
Sein Kraft Avatar asked May 20 '10 22:05

Sein Kraft


People also ask

When should you use primary key?

A primary key is a special relational database table column (or combination of columns) designated to uniquely identify each table record. A primary key is used as a unique identifier to quickly parse data within the table. A table cannot have more than one primary key.

What is the difference between primary key and primary index?

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.

Which is faster primary key or index?

If there are many rows covered by the index than it's can be faster to simply do a table scan instead. An index adds some overhead while querying so if the between covers more than 80% (completely arbitrary, but you get the idea) of the rows in the table, the table scan can be faster.

When should you use an index?

You should use an index on columns that you use for selection and ordering - i.e. the WHERE and ORDER BY clauses. Indexes can slow down select statements if there are many of them and you are using WHERE and ORDER BY on columns that have not been indexed.


2 Answers

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.
like image 65
Michael Borgwardt Avatar answered Sep 19 '22 18:09

Michael Borgwardt


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.

like image 37
marapet Avatar answered Sep 21 '22 18:09

marapet