我刚刚学习了如何在mysql中创建索引并在连接到mysql 8.0实例的mysql工作台中执行了这样的查询:
activityViewController(_:thumbnailImageForActivityType:suggestedSize)
结果网格为:
use test;
create table idx (
id int not null unique,
name char(5),
primary key(id),
key(name)
);
show index from idx;
这让我感到困惑,因为# 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
是主键,所以有两个索引key_name = PRIMARAY和key_name =“ id”。它们之间有什么区别?
答案 0 :(得分:2)
id
索引来自unique
列的id
选项。 PRIMARY
索引来自primary key (id)
。
由于主键始终是唯一的,因此不需要unique
选项,这样就不会创建额外的索引。
答案 1 :(得分:0)