主に簡単にテーブル構造とデータが同じテーブルを作成する方法
は2パターンありますが、それぞのにも違いはあるので改めて確認。

その1
※テーブル構造+データ
create table ******* select ********


mysql> desc Y2008;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| comment | varchar(50) | NO | PRI | | |
+---------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> select * from Y2008;
+---------+
| comment |
+---------+
| TEST1 |
| TEST2 |
| TEST3 |
+---------+
3 rows in set (0.00 sec)

mysql> create table Y2009 select * from Y2008;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from Y2009;
+---------+
| comment |
+---------+
| TEST1 |
| TEST2 |
| TEST3 |
+---------+
3 rows in set (0.00 sec)

mysql> desc Y2009;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| comment | varchar(50) | NO | | | |
+---------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql>

create_select

その2
※テーブル構造+データ+インデックスなど
CREATE TABLE ****** LIKE **********


mysql> create table Y2010 like Y2008;
Query OK, 0 rows affected (0.01 sec)

mysql> desc Y2008;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| comment | varchar(50) | NO | PRI | | |
+---------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> desc Y2010;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| comment | varchar(50) | NO | PRI | | |
+---------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into Y2010 select * from Y2008;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from Y2010;
+---------+
| comment |
+---------+
| TEST1 |
| TEST2 |
| TEST3 |
+---------+
3 rows in set (0.00 sec)

mysql>


insert_table1

Comments are closed.

Post Navigation