根据已有表来创建新表(也适用于临时表)

语法1:(不导入数据)
CREATE TABLE new_tbl LIKE orig_tbl;
这种语法,将从源表赋值列名、数据类型、大小、非空约束以及索引。而源表的内容以及其他约束不会复制,新表是一张空表

mysql> create table copy_teams like TEAMS;
Query OK, 0 rows affected (0.03 sec)

mysql> desc copy_teams;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| TEAMNO   | int(11) | NO   | PRI | NULL    |       |
| PLAYERNO | int(11) | NO   |     | NULL    |       |
| DIVISION | char(6) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc TEAMS;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| TEAMNO   | int(11) | NO   | PRI | NULL    |       |
| PLAYERNO | int(11) | NO   |     | NULL    |       |
| DIVISION | char(6) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from copy_teams;
Empty set (0.00 sec)


语法2:(在建表同时也导入数据)

CREATE TABLE new_tbl [AS] SELECT {*|column,…} FROM orig_tbl; -------select子句也可以从多个表中查询不同的列(可以非常复杂,例如多表连接)

1、新表的结构由select列表决定。同时把查询返回的结果集中的行插入到目标表中。这种语法只能把非空约束带入到新表中。也不会复制索引

mysql> create table copy_teams as select * from TEAMS;
Query OK, 2 rows affected (0.04 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from copy_teams;
+--------+----------+----------+
| TEAMNO | PLAYERNO | DIVISION |
+--------+----------+----------+
|      1 |        6 | first    |
|      2 |       27 | second   |
+--------+----------+----------+
2 rows in set (0.00 sec)

mysql> desc copy_teams;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| TEAMNO   | int(11) | NO   |     | NULL    |       |
| PLAYERNO | int(11) | NO   |     | NULL    |       |
| DIVISION | char(6) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc TEAMS;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| TEAMNO   | int(11) | NO   | PRI | NULL    |       |
| PLAYERNO | int(11) | NO   |     | NULL    |       |
| DIVISION | char(6) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

2、如果在表名后面指定了列名(和原始表中的列名相同),则可以改变列的大小和非空约束

mysql> desc TEAMS;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| TEAMNO   | int(11) | NO   | PRI | NULL    |       |
| PLAYERNO | int(11) | NO   |     | NULL    |       |
| DIVISION | char(6) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> create table teams_bak(TEAMNO int(10),PLAYERNO int(10), DIVISION char(10)) as select * from TEAMS;
Query OK, 2 rows affected (0.06 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> desc teams_bak;
+----------+----------+------+-----+---------+-------+
| Field    | Type     | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| TEAMNO   | int(10)  | YES  |     | NULL    |       |
| PLAYERNO | int(10)  | YES  |     | NULL    |       |
| DIVISION | char(10) | YES  |     | NULL    |       |
+----------+----------+------+-----+---------+-------+

3、如果想要控制表的列名,则在select查询中使用列别名

mysql> create table teams_bak as select TEAMNO 队号,PLAYERNO 球员编号,DIVISION 分级 from TEAMS;
Query OK, 2 rows affected (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from teams_bak;
+--------+--------------+--------+
| 队号   | 球员编号     | 分级   |
+--------+--------------+--------+
|      1 |            6 | first  |
|      2 |           27 | second |
+--------+--------------+--------+
2 rows in set (0.00 sec)

4、如果表名后面指定的列名和原始表中列名不同,则它作为一个新的列

mysql> create table teams_bak(address varchar(10)) as select * from TEAMS;
Query OK, 2 rows affected (0.04 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from teams_bak;
+---------+--------+----------+----------+
| address | TEAMNO | PLAYERNO | DIVISION |
+---------+--------+----------+----------+
| NULL    |      1 |        6 | first    |
| NULL    |      2 |       27 | second   |
+---------+--------+----------+----------+

你可能感兴趣的:(MYSQL开发学习)