如何将MySQL表结构复制到内存中的表?

时间:2022-09-16 09:51:14

How to create a table in memory which is identical to "regular" table? I want a copy without indexes and constraints. Pure SQL (no external tools).

如何在内存中创建一个与“常规”表相同的表?我想要一个没有索引和约束的副本。纯SQL(没有外部工具)。

This works (however indexes are created):

这有效(但是创建了索引):

create table t_copy (like t_original)

This doesn't:

create table t_copy (like t_original) engine=memory

3 个解决方案

#1


10  

CREATE TABLE t_copy ENGINE=MEMORY SELECT * FROM t_original;

I actually tried it, it works !!!

我实际上尝试过它,它的工作原理!

mysql> show create table queue\G
*************************** 1. row ***************************
       Table: queue
Create Table: CREATE TABLE `queue` (
  `ndx` int(11) NOT NULL AUTO_INCREMENT,
  `folderid` int(11) NOT NULL,
  PRIMARY KEY (`ndx`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
1 row in set (0.02 sec)

mysql> create table queue_memory engine=MEMORY as select * from queue;
Query OK, 0 rows affected (0.05 sec)<BR>
Records: 0  Duplicates: 0  Warnings: 0<BR>

lwdba@localhost (DB test) :: show create table queue_memory\G
*************************** 1. row ***************************
       Table: queue_memory
Create Table: CREATE TABLE `queue_memory` (
  `ndx` int(11) NOT NULL DEFAULT '0',
  `folderid` int(11) NOT NULL
) ENGINE=MEMORY DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

Give it a try !!!

试一试 !!!

#2


6  

This should get you an empty table without any indexes.

这应该会得到一个没有任何索引的空表。

create table t_copy
    select *
        from t_original
        where 0

#3


4  

To create an in-memory table with the same structure AND indexes as the first table, try:

要创建与第一个表具有相同结构和索引的内存表,请尝试:

create table t_copy like t_original;
alter table t_copy engine=mem;

#1


10  

CREATE TABLE t_copy ENGINE=MEMORY SELECT * FROM t_original;

I actually tried it, it works !!!

我实际上尝试过它,它的工作原理!

mysql> show create table queue\G
*************************** 1. row ***************************
       Table: queue
Create Table: CREATE TABLE `queue` (
  `ndx` int(11) NOT NULL AUTO_INCREMENT,
  `folderid` int(11) NOT NULL,
  PRIMARY KEY (`ndx`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
1 row in set (0.02 sec)

mysql> create table queue_memory engine=MEMORY as select * from queue;
Query OK, 0 rows affected (0.05 sec)<BR>
Records: 0  Duplicates: 0  Warnings: 0<BR>

lwdba@localhost (DB test) :: show create table queue_memory\G
*************************** 1. row ***************************
       Table: queue_memory
Create Table: CREATE TABLE `queue_memory` (
  `ndx` int(11) NOT NULL DEFAULT '0',
  `folderid` int(11) NOT NULL
) ENGINE=MEMORY DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

Give it a try !!!

试一试 !!!

#2


6  

This should get you an empty table without any indexes.

这应该会得到一个没有任何索引的空表。

create table t_copy
    select *
        from t_original
        where 0

#3


4  

To create an in-memory table with the same structure AND indexes as the first table, try:

要创建与第一个表具有相同结构和索引的内存表,请尝试:

create table t_copy like t_original;
alter table t_copy engine=mem;