【问题标题】:Copy a MySQL table including indexes复制包含索引的 MySQL 表
【发布时间】:2011-01-25 19:54:28
【问题描述】:

我可以复制一个 MySQL 表来创建一个新表:

CREATE TABLE newtable SELECT * FROM oldtable

这可行,但索引不会复制到新表中。如何复制包含索引的表?

【问题讨论】:

  • indexes not create vs prevent the indexes 模棱两可,你想做什么?
  • @luchaninov 该问题的答案提到了这个问题。这是一个无限循环。

标签: mysql indexing create-table


【解决方案1】:
CREATE TABLE newtable LIKE oldtable; 
INSERT INTO newtable SELECT * FROM oldtable;

【讨论】:

  • 另外,create table newtable like oldtable; 复制列数据类型,因此您的记录不会被强制转换为意外的数据类型。
  • 使用这个我发现 NotNull 和 PrimaryKey 属性被删除了 - 对此有什么想法吗?
  • @AndrewSeabrook,因为我已经尝试过 PrimaryKey 并且 NotNull 也会被保存。
  • 要复制没有索引,只需使用结构和数据CREATE TABLE tbl_new AS SELECT * FROM tbl_old
  • 插入新表 SELECT * FROM oldtable;必须是 INSERT INTO newtable SELECT * FROM oldtable;
猜你喜欢
  • 1970-01-01
  • 2014-01-22
  • 2011-03-17
  • 2019-06-06
  • 1970-01-01
  • 2010-09-16
  • 2021-01-04
相关资源
最近更新 更多