Tuesday, 2 June 2015

Mysql: Copy data from one tabe to a new table

Shows how to create new the same table and copy data.
CREATE TABLE table_a (
  id INT(11) DEFAULT NULL,
  column_a INT(11) DEFAULT NULL
);
INSERT INTO table_a VALUES 
  (1, 10),
  (2, 20),
  (3, 30),
  (4, NULL),
  (5, 0);

Create and copy data:
CREATE TABLE table_b SELECT * FROM table_a;

Check results:
SHOW CREATE TABLE table_b;
>
CREATE TABLE `table_b` (
  `id` int(11) DEFAULT NULL,
  `column_a` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1

SELECT * FROM table_b;
+------+----------+
| id   | column_a |
+------+----------+
|    1 |       10 |
|    2 |       20 |
|    3 |       30 |
|    4 |     NULL |
|    5 |        0 |
+------+----------+

0 comments:

Post a Comment