What is the difference between primary key and index in mysql?

2

I wanted to know if there is any difference between primary key and index.

    
asked by anonymous 28.02.2016 / 21:43

1 answer

5

An index is simply an aid in searching (for example, a SELECT) on top of a column. For example, in a table with people's names, adding an index to the NAME helps to accelerate searches by name, and also selections that get rows in alphabetical order of name.

Finding a field without index takes a time linearly proportional to the number of rows. Searching a field without index takes a time proportional to the logarithm of the number of rows. When there are 100 million lines, this makes a lot of difference, because log (100 million) = 8.

A UNIQUE index indicates that there can only be one copy of the column for each row. This is appropriate for columns that are known to be unique (people's CPF type) but would not be appropriate for names, because homonyms are very common.

PRIMARY KEY, or primary key, is a different concept of index, although usually the primary key is indexed to accelerate the search. A primary key is a column that can only have one copy for each E line and this column serves to identify that row. The CPF could be a primary key for a physical persons table, but it is best to use an abstract primary key - a growing sequential code or a hash, to facilitate later changes (if your people table needs to include foreigners in the future , the CPF is no longer a good primary key and can no longer be indexed UNIQUE).

The FULLTEXT I do not know but I assume it is an index to facilitate searches involving parts of a textual column (type, search by fragment of the name instead of the full name)

    
29.02.2016 / 01:41