If you want to create a table with the same description as another table, you can do this using like. In the example below, I create a table called andrews_table with 1 row of data. Then I use like to create a table called another_table with the same description. This table is empty to start with. To copy the data from andrews_table into another_table, I use a simple insert statement:
mysql> create table andrews_table
-> (col1 varchar(10));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into andrews_table values ('Fred');
Query OK, 1 row affected (0.12 sec)
mysql> create table another_table
-> like andrews_table;
Query OK, 0 rows affected (0.00 sec)
mysql> desc another_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| col1 | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.06 sec)
mysql> select * from another_table;
Empty set (0.00 sec)
mysql> insert into another_table
-> (select * from andrews_table);
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from another_table;
+------+
| col1 |
+------+
| Fred |
+------+
1 row in set (0.00 sec)
mysql>
No comments:
Post a Comment