MySQL操作数据库和表的常用命令新手教程(2)
复制代码 代码如下: mysql create table tb_test2 select * from db_test.tb_test; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 将向数据库增加一个相同的表t
复制代码 代码如下:
mysql> create table tb_test2 select * from db_test.tb_test;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
将向数据库增加一个相同的表tb_test2。而有的时候,可能希望只基于现有表的几个列创建一个表。通过create select语句中指定列就可以实现:
复制代码 代码如下:
mysql> describe tb_test;
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| firstname | varchar(25) | NO | | NULL | |
| lastname | varchar(25) | NO | | NULL | |
| email | varchar(45) | NO | | NULL | |
| phone | varchar(10) | NO | | NULL | |
+-----------+------------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
mysql> create table tb_test2 select id, firstname, lastname, email from tb_test;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe tb_test2;
+-----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | | 0 | |
| firstname | varchar(25) | NO | | NULL | |
| lastname | varchar(25) | NO | | NULL | |
| email | varchar(45) | NO | | NULL | |
+-----------+------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
4.创建临时表
有的时候,当工作在非常大的表上时,可能偶尔需要运行很多查询获得一个大量数据的小的子集,不是对整个表运行这些查询,而是让MySQL每次找出所需的少数记录,将记录保存到一个临时表可能更快一些,然后对这些临时表进行查询操作。可以通过使用temporary关键字和create table语句来实现。
复制代码 代码如下:
mysql> create temporary table emp_temp select firstname, lastname from tb_test;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
临时表的创建与其它表一样,只是它们存储在操作系统指定的临时目录中。临时表将在你连接MySQL期间存在,当你断开时,MySQL将自动删除表并释放所有的内存空间;当然了,你也可以手动的使用drop table命令删除临时表。
5.查看数据库中可用的表
收藏文章
精彩图集
精彩文章