Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy MySQL table structure to table in memory?

Tags:

mysql

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).

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
like image 223
Marek Augustyn Avatar asked Feb 23 '11 21:02

Marek Augustyn


People also ask

What command is used to create a table by copying the structure of another table in MySQL?

What command is used to create a table by copying the structure of another table? CREATE TABLE As SELECT Command. Explanation: To copy only the structure the where clause of the SELECT command should have a FALSE statement.


1 Answers

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 !!!

like image 98
RolandoMySQLDBA Avatar answered Nov 15 '22 22:11

RolandoMySQLDBA