gpt4 book ai didi

mysql - 如何识别列是聚集索引还是非聚集索引?

转载 作者:行者123 更新时间:2023-11-28 23:25:58 24 4
gpt4 key购买 nike

我有这张 table

CREATE TABLE question2 (
question_id INT(10) not null auto_increment,
lesson INT(10) not null,
questionNum INT(10) not null,
question varchar(60) not null,
answer varchar(60) not null,
primary key (question_id) )
CHARACTER SET=utf8 ENGINE=InnoDB

第一个问题:因为我有一个主键列,是否意味着我不能有另一个聚集索引?

我写这个命令

ALTER TABLE `question2` ADD INDEX( `lesson`);

现在显示创建表如下所示:

question2 | CREATE TABLE `question2` (
`question_id` int(10) NOT NULL AUTO_INCR
`lesson` int(10) NOT NULL,
`questionNum` int(10) NOT NULL,
`question` varchar(60) NOT NULL,
`answer` varchar(60) NOT NULL,
PRIMARY KEY (`question_id`),
KEY `lesson` (`lesson`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

在 php 中我的管理员在索引部分我可以看到这个:

enter image description here

我需要一个“类(class)”列的聚集索引。因为有很多使用此列的读取查询。

第二个问题:那么,“类(class)”列是聚簇索引列还是非聚簇索引列?

最佳答案

您的表(集群)只能有 1 个物理排序。您可以拥有大量的二级索引。因此,相应地选择您的主键。如果你有一个 auto_inc,它必须是一个键(innodb 不需要 PRIMARY)。

show indexes from myTableName;

将为您的主索引显示 PRIMARY(Key_name 列)。

在 InnoDB 中允许以下内容用于 auto_increment 列:

create table t9
( col1 int not null primary key,
col2 int auto_increment,
key `key099` (col2)
)engine=InnoDB;

注意:auto_increment 列不是可以通过显示的主键

show indexes from t9;

但它必须是 key 。

create table t10
( col1 varchar(20) primary key,
col2 datetime not null,
col3 int auto_increment,
key `key101` (col2,col3),
key `key102` (col3)
)engine=InnoDB;

正是 key102 使上面的操作成功。 (因为 AI 必须是一个键,但它不能被埋在组合中,除非它在最左边)。

How does InnoDB behave without a Primary Key?

关于mysql - 如何识别列是聚集索引还是非聚集索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39325713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com