如何在没有索引或键的情况下在 mysql 中创建 TEMPORARY TABLE?

How to create TEMPORARY TABLE in mysql without indexes or keys?

CREATE TABLE person (
    firstname varchar(20),
    lastname varchar(20),
    UNIQUE KEY unique_lstanme (lastname)
);

创建临时文件:

CREATE TEMPORARY TABLE person_tmp LIKE person;
SHOW INDEX FROM person_tmp;

结果:显示唯一键。为什么?我一直以为 TEMPORARY table 是没有索引创建的?

我可以阻止这种情况吗,或者我是否必须显式删除临时文件中的索引 table?是否有 sql 命令可以删除临时 table 上的 任何 现有索引而无需命名它们?

在 mysql 中,临时表可以有索引,而 create temporary table ... like ... 确实保留了索引,对此您无能为力。

使用 create temporary table ... select ... 语句而不是 limit 0 子句,因为

CREATE TABLE ... SELECT does not automatically create any indexes for you. This is done intentionally to make the statement as flexible as possible. If you want to have indexes in the created table, you should specify these before the SELECT statement.

因此,您的陈述将类似于:

CREATE TEMPORARY TABLE person_tmp
AS SELECT * FROM person LIMIT 0;