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
表是在没有索引的情况下创建的?
我可以阻止这种情况,还是必须在临时表上显式删除索引?是否有一个 sql 命令可以删除临时表上的任何现有索引而无需命名它们?
最佳答案
在mysql中,临时表可以有索引,并且创建临时表...就像...
确实保留索引,您对此无能为力。
使用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;
关于mysql - 如何在mysql中创建没有索引或键的临时表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58622577/