CREATE TABLE IF NOT EXISTS new_table LIKE existing_table;
INSERT new_table
SELECT * FROM existing_table;
USE yiibaidb;
CREATE TABLE IF NOT EXISTS offices_bk
SELECT * FROM
offices;
SELECT
*
FROM
offices_bk;
+------------+---------------+------------------+--------------------------+--------------+-------+-----------+------------+-----------+
| officeCode | city | phone | addressLine1 | addressLine2 | state | country | postalCode | territory |
+------------+---------------+------------------+--------------------------+--------------+-------+-----------+------------+-----------+
| 1 | San Francisco | +1 650 219 4782 | 100 Market Street | Suite 300 | CA | USA | 94080 | NA |
| 2 | Boston | +1 215 837 0825 | 1550 Court Place | Suite 102 | MA | USA | 02107 | NA |
| 3 | NYC | +1 212 555 3000 | 523 East 53rd Street | apt. 5A | NY | USA | 10022 | NA |
| 4 | Paris | +33 14 723 4404 | 43 Rue Jouffroy Dabbans | NULL | NULL | France | 75017 | EMEA |
| 5 | Beijing | +86 33 224 5000 | 4-1 Haidian Area | NULL | BJ | China | 110000 | NA |
| 6 | Sydney | +61 2 9264 2451 | 5-11 Wentworth Avenue | Floor #2 | NULL | Australia | NSW 2010 | APAC |
| 7 | London | +44 20 7877 2041 | 25 Old Broad Street | Level 7 | NULL | UK | EC2N 1HN | EMEA |
+------------+---------------+------------------+--------------------------+--------------+-------+-----------+------------+-----------+
7 rows in set
USE yiibaidb;
CREATE TABLE IF NOT EXISTS offices_usa
SELECT *
FROM
offices
WHERE
country = 'USA';
SELECT *
FROM
offices
WHERE
country = 'USA';
+------------+---------------+-----------------+----------------------+--------------+-------+---------+------------+-----------+
| officeCode | city | phone | addressLine1 | addressLine2 | state | country | postalCode | territory |
+------------+---------------+-----------------+----------------------+--------------+-------+---------+------------+-----------+
| 1 | San Francisco | +1 650 219 4782 | 100 Market Street | Suite 300 | CA | USA | 94080 | NA |
| 2 | Boston | +1 215 837 0825 | 1550 Court Place | Suite 102 | MA | USA | 02107 | NA |
| 3 | NYC | +1 212 555 3000 | 523 East 53rd Street | apt. 5A | NY | USA | 10022 | NA |
+------------+---------------+-----------------+----------------------+--------------+-------+---------+------------+-----------+
3 rows in set
CREATE TABLE offices_dup LIKE offices;
INSERT office_dup
SELECT * FROM offices;
CREATE TABLE destination_db.new_table
LIKE source_db.existing_table;
INSERT destination_db.new_table
SELECT *
FROM source_db.existing_table;
CREATE DATABASE IF NOT EXISTS testdb;
CREATE TABLE testdb.offices LIKE yiibaidb.offices;
INSERT testdb.offices
SELECT *
FROM yiibaidb.offices;
SELECT
*
FROM
testdb.offices;
mysql> SELECT
*
FROM
testdb.offices;
+------------+---------------+------------------+--------------------------+--------------+-------+-----------+------------+-----------+
| officeCode | city | phone | addressLine1 | addressLine2 | state | country | postalCode | territory |
+------------+---------------+------------------+--------------------------+--------------+-------+-----------+------------+-----------+
| 1 | San Francisco | +1 650 219 4782 | 100 Market Street | Suite 300 | CA | USA | 94080 | NA |
| 2 | Boston | +1 215 837 0825 | 1550 Court Place | Suite 102 | MA | USA | 02107 | NA |
| 3 | NYC | +1 212 555 3000 | 523 East 53rd Street | apt. 5A | NY | USA | 10022 | NA |
| 4 | Paris | +33 14 723 4404 | 43 Rue Jouffroy Dabbans | NULL | NULL | France | 75017 | EMEA |
| 5 | Beijing | +86 33 224 5000 | 4-1 Haidian Area | NULL | BJ | China | 110000 | NA |
| 6 | Sydney | +61 2 9264 2451 | 5-11 Wentworth Avenue | Floor #2 | NULL | Australia | NSW 2010 | APAC |
| 7 | London | +44 20 7877 2041 | 25 Old Broad Street | Level 7 | NULL | UK | EC2N 1HN | EMEA |
+------------+---------------+------------------+--------------------------+--------------+-------+-----------+------------+-----------+
7 rows in set