Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: what exactly do Primary Keys and Indexes do?

Tags:

sql

phpmyadmin

I've recently started developing my first serious application which uses a SQL database, and I'm using phpMyAdmin to set up the tables. There are a couple optional "features" I can give various columns, and I'm not entirely sure what they do:

  • Primary Key
  • Index

I know what a PK is for and how to use it, but I guess my question with regards to that is why does one need one - how is it different from merely setting a column to "Unique", other than the fact that you can only have one PK? Is it just to let the programmer know that this value uniquely identifies the record? Or does it have some special properties too?

I have no idea what "Index" does - in fact, the only times I've ever seen it in use are (1) that my primary keys seem to be indexed, and (2) I heard that indexing is somehow related to performance; that you want indexed columns, but not too many. How does one decide which columns to index, and what exactly does it do?

edit: should one index colums one is likely to want to ORDER BY?

Thanks a lot,

Mala

like image 302
Mala Avatar asked Aug 22 '09 08:08

Mala


People also ask

Why are primary keys and indexes needed?

A primary key is a logical concept. The primary key are the column(s) that serves to identify the rows. An index is a physical concept and serves as a means to locate rows faster, but is not intended to define rules for the table.

What is the purpose of primary key in SQL?

The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

What is the difference between primary index and primary key?

The primary index contains the key fields of the table and a pointer to the non-key fields of the table. The primary index is created automatically when the table is created in the database. Primary key is mandatory.it avoid the duplicate of data.

Do you need index If you have primary key?

But in the database world, it's actually not necessary to create an index on the primary key column — the primary index can be created on any non primary key column as well.


2 Answers

Primary key is usually used to create a numerical 'id' for your records, and this id column is automatically incremented.

For example, if you have a books table with an id field, where the id is the primary key and is also set to auto_increment (Under 'Extra in phpmyadmin), then when you first add a book to the table, the id for that will become 1'. The next book's id would automatically be '2', and so on. Normally, every table should have at least one primary key to help identifying and finding records easily.

Indexes are used when you need to retrieve certain information from a table regularly. For example, if you have a users table, and you will need to access the email column a lot, then you can add an index on email, and this will cause queries accessing the email to be faster.

However there are also downsides for adding unnecessary indexes, so add this only on the columns that really do need to be accessed more than the others. For example, UPDATE, DELETE and INSERT queries will be a little slower the more indexes you have, as MySQL needs to store extra information for each indexed column. More info can be found at this page.

Edit: Yes, columns that need to be used in ORDER BY a lot should have indexes, as well as those used in WHERE.

like image 150
Ali Avatar answered Oct 09 '22 18:10

Ali


The primary key is basically a unique, indexed column that acts as the "official" ID of rows in that table. Most importantly, it is generally used for foreign key relationships, i.e. if another table refers to a row in the first, it will contain a copy of that row's primary key.

Note that it's possible to have a composite primary key, i.e. one that consists of more than one column.

Indexes improve lookup times. They're usually tree-based, so that looking up a certain row via an index takes O(log(n)) time rather than scanning through the full table.

Generally, any column in a large table that is frequently used in WHERE, ORDER BY or (especially) JOIN clauses should have an index. Since the index needs to be updated for evey INSERT, UPDATE or DELETE, it slows down those operations. If you have few writes and lots of reads, then index to your hear's content. If you have both lots of writes and lots of queries that would require indexes on many columns, then you have a big problem.

like image 36
Michael Borgwardt Avatar answered Oct 09 '22 16:10

Michael Borgwardt