MySQL临时表创建及旧表建新表

时间:2023-03-08 23:26:11
MySQL临时表创建及旧表建新表

1、创建临时表

  临时表是一张表,用来临时保存一些数据

特点:

  只对创建该临时表的用户可见;

  当会话结束时,MySQL自动删除临时表。

临时表的核心:建表和删表消耗资源极其少


创建临时表的基本格式:

  CREATE TEMPORARY TABLE  tbl_name(……);

①创建的临时表在当前会话,正常使用

②断开连接,再重新连接后执行查询,抛出异常:

  错误代码: 1146

  Table ‘db_name.temtbl_name’ doesn‘t exist。//该临时表在会话结束的时候被系统删除。


注意:用户可以创建一个和已有的普通表名字相同的临时表。

在这种情况下,

  该用户只能看到临时表而看不见同名的普通表;

  当临时表被删除后,才可以看到普通表。

示例:使用相同的名字创建一个普通表和临时表

mysql> create table test_table(num int);
Query OK, rows affected (0.12 sec) mysql> insert into test_table values();
Query OK, row affected (0.04 sec) mysql> create temporary table test_table(num int,name varchar());
Query OK, rows affected (0.04 sec) mysql> insert into test_table values(,'临时表测试');
Query OK, row affected (0.00 sec) mysql> select * from test_table;
+------+-----------------+
| num | name |
+------+-----------------+
| | 临时表测试 |
+------+-----------------+
row in set (0.00 sec) mysql> drop table test_table;
Query OK, rows affected (0.00 sec) mysql> select * from test_table;
+------+
| num |
+------+
| |
+------+
row in set (0.00 sec)

Q:当创建表时,如果表已经存在了,则MySQL会返回出错消息,我们不希望看到报错的信息,该如何处理?

A:添加IF NOT EXISTS选项,则强制不显示这个出错消息;但是,语句执行失败---建表失败。

mysql> create table if not exists PLAYERS(id int(),name varchar());
Query OK, rows affected, warning (0.00 sec) mysql> show warnings;
+-------+------+--------------------------------+
| Level | Code | Message |
+-------+------+--------------------------------+
| Note | | Table 'PLAYERS' already exists |
+-------+------+--------------------------------+
row in set (0.00 sec) mysql> show tables;
+-------------------+
| Tables_in_TENNIS |
+-------------------+
| COMMITTEE_MEMBERS |
| MATCHES |
| PENALTIES |
| PLAYERS |
| TEAMS |
| test_table |
+-------------------+
6 rows in set (0.00 sec)

2、根据已有的表来创建新表

语法1:只想拷贝表结构

  CREATE TABLE new_tbl LIKE orig_tbl;

将从源表复制列名、数据类型、大小、非空约束以及索引;而表的内容以及其它约束不会复制,新表是一张空表。

mysql> desc TEAMS;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| TEAMNO | int() | NO | PRI | NULL | |
| PLAYERNO | int() | NO | | NULL | |
| DIVISION | char() | NO | | NULL | |
+----------+---------+------+-----+---------+-------+
rows in set (0.00 sec) mysql> create table copy_TEAMS like TEAMS;
Query OK, rows affected (0.11 sec) mysql> select * from copy_TEAMS;
Empty set (0.00 sec) mysql> desc copy_TEAMS;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| TEAMNO | int() | NO | PRI | NULL | |
| PLAYERNO | int() | NO | | NULL | |
| DIVISION | char() | NO | | NULL | |
+----------+---------+------+-----+---------+-------+
rows in set (0.00 sec)

语法2:根据SELECT子查询的拷贝

  CREATE TABLE new_tbl [AS] SELECT {*|column,...} FROM orig_tbl;

新表的结构由select列表决定;同时把查询返回的结果集中的行插入到目标表中;只能把非空约束带入到新表中(在有的时候就会显得很鸡肋了),也不会复制索引

mysql> create table p_m
-> as
-> select a.NAME,a.SEX,b.MATCHNO,b.WON,b.LOST
-> from PLAYERS a,MATCHES b
-> where a.PLAYERNO=b.PLAYERNO;
Query OK, rows affected (0.15 sec)
Records: Duplicates: Warnings: mysql> select * from p_m;
+-----------+-----+---------+-----+------+
| NAME | SEX | MATCHNO | WON | LOST |
+-----------+-----+---------+-----+------+
| Parmenter | M | | | |
| Parmenter | M | | | |
| Parmenter | M | | | |
| Baker | M | | | |
| Hope | M | | | |
| Everett | M | | | |
| Brown | M | | | |
| Newcastle | F | | | |
| Collins | F | | | |
| Moorman | F | | | |
| Bailey | F | | | |
| Bailey | F | | | |
| Newcastle | F | | | |
+-----------+-----+---------+-----+------+
rows in set (0.00 sec)

通过根据已有表来创建新表,添加选项temporary,创建临时副本用来做练习最合适:表内容可一样,会话结束,临时表自动删除,原始表中的内容不受任何影响。

注意:

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

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

mysql> select * from TEAMS;
+--------+----------+----------+
| TEAMNO | PLAYERNO | DIVISION |
+--------+----------+----------+
| | | first |
| | | second |
+--------+----------+----------+ mysql> CREATE TABLE teams_copy
-> (
-> teamno INTEGER NOT NULL PRIMARY KEY,
-> playerno INTEGER NULL,
-> division char() NOT NULL,
-> coach varchar()
-> )
-> as
-> select * from TEAMS;
Query OK, rows affected (0.11 sec)
Records: Duplicates: Warnings: mysql> select * from teams_copy;
+-------+--------+----------+----------+
| coach | TEAMNO | PLAYERNO | DIVISION |
+-------+--------+----------+----------+
| NULL | | | first |
| NULL | | | second |
+-------+--------+----------+----------+