What is the PRIMARY index in mysql

jl0x61 :

I have just learnt how to create indexes in mysql and executed such a query in mysql workbench connecting to a mysql 8.0 instance:

use test;
create table idx (
id int not null unique,
name char(5),
primary key(id),
key(name)
);
show index from idx;

The result grid is:

# Table, Non_unique, Key_name, Seq_in_index, Column_name, Collation, Cardinality, Sub_part, Packed, Null, Index_type, Comment, Index_comment, Visible, Expression
'idx', '0', 'PRIMARY', '1', 'id', 'A', '0', NULL, NULL, '', 'BTREE', '', '', 'YES', NULL
'idx', '0', 'id', '1', 'id', 'A', '0', NULL, NULL, '', 'BTREE', '', '', 'YES', NULL
'idx', '1', 'name', '1', 'name', 'A', '0', NULL, NULL, 'YES', 'BTREE', '', '', 'YES', NULL

It makes me confused that there are two indexes of key_name=PRIMARAY and key_name="id" because id is the primary key. What's the difference between them?

Barmar :

The id index comes from the unique option for the id column. The PRIMARY index comes from primary key (id).

Since a primary key is always unique, you don't need the unique option, and then it won't create the extra index.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=336972&siteId=1