如果我创建一个没有主键的 table 然后添加一个主键,主键是聚簇索引吗?
If I create a table without a primary key then add a primary key, is the primary key a clustered index?
来自 Getting Started with Indexes: Primary Key 它说:
In InnoDB tables, all indexes contain the primary key as a suffix. Thus, when using this storage engine, keeping the primary key as small as possible is particularly important. If a primary key does not exist and there are no UNIQUE indexes, InnoDB creates a 6-bytes clustered index which is invisible to the user.
因此,在没有主键的情况下,我们将创建一个带有隐藏聚集索引的 table。然后在同一部分后面的几段后面说:
You cannot create a primary key with the CREATE INDEX command. If you do want to add one after the table has already been created, use ALTER TABLE, for example:
但没有说明新主键是否会成为聚簇索引。
也ALTER TABLE: ADD PRIMARY KEY只说:
For PRIMARY KEY indexes, you can specify a name for the index, but it is silently ignored, and the name of the index is always PRIMARY.
但不评论聚集索引。
我的困惑导致了这些问题:
- 如果我以后添加主键,主键会聚簇吗?
- 如果是这样,存储布局会发生什么变化?是否必须将所有数据复制到新的聚簇索引,然后删除之前隐藏的聚簇索引?
- 主键总是聚簇吗?
最小复制示例:
create database test
use test
create table test_table ( id bigint(20) NULL );
describe test_table;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | bigint(20) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.003 sec)
show index from test_table;
Empty set (0.000 sec)
alter table test_table add primary key(id);
Query OK, 0 rows affected (0.067 sec)
Records: 0 Duplicates: 0 Warnings: 0
describe test_table;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | bigint(20) | NO | PRI | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.007 sec)
show index from test_table;
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test_table | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.001 sec)
如果 table 有一个主键,那么它总是聚簇索引(假设它在 InnoDB 引擎中)。
如果您更改 table 以添加主键,那么它将成为聚簇索引。
添加(或更改)主键确实需要重构 table,因为布局将完全改变。
来自 Getting Started with Indexes: Primary Key 它说:
In InnoDB tables, all indexes contain the primary key as a suffix. Thus, when using this storage engine, keeping the primary key as small as possible is particularly important. If a primary key does not exist and there are no UNIQUE indexes, InnoDB creates a 6-bytes clustered index which is invisible to the user.
因此,在没有主键的情况下,我们将创建一个带有隐藏聚集索引的 table。然后在同一部分后面的几段后面说:
You cannot create a primary key with the CREATE INDEX command. If you do want to add one after the table has already been created, use ALTER TABLE, for example:
但没有说明新主键是否会成为聚簇索引。
也ALTER TABLE: ADD PRIMARY KEY只说:
For PRIMARY KEY indexes, you can specify a name for the index, but it is silently ignored, and the name of the index is always PRIMARY.
但不评论聚集索引。
我的困惑导致了这些问题:
- 如果我以后添加主键,主键会聚簇吗?
- 如果是这样,存储布局会发生什么变化?是否必须将所有数据复制到新的聚簇索引,然后删除之前隐藏的聚簇索引?
- 主键总是聚簇吗?
最小复制示例:
create database test
use test
create table test_table ( id bigint(20) NULL );
describe test_table;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | bigint(20) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.003 sec)
show index from test_table;
Empty set (0.000 sec)
alter table test_table add primary key(id);
Query OK, 0 rows affected (0.067 sec)
Records: 0 Duplicates: 0 Warnings: 0
describe test_table;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | bigint(20) | NO | PRI | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.007 sec)
show index from test_table;
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test_table | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.001 sec)
如果 table 有一个主键,那么它总是聚簇索引(假设它在 InnoDB 引擎中)。
如果您更改 table 以添加主键,那么它将成为聚簇索引。
添加(或更改)主键确实需要重构 table,因为布局将完全改变。