您的位置:首页 > 数据库 > MySQL

MySQL Cookbook学习笔记第四章

2014-05-06 21:19 811 查看

1,克隆表(创建一个恰好与某个已有表结构一致的表)

create table … like克隆表结构;使用insert into … select语句克隆部分或者全部表数据





2,将查询结果保存到表中

a,使用insert into...select将语句查询结果插入表中,若表不存在需要使用create table …select 语句为查询结果新建一张表。

insert into dst_tb1(i,s) select val, name from src_tb1

insert into dst_tb1 select * from src_tb1

insert into dst_tb1 select * from src_tb1 where val>100 and name like ‘A%’

create table dst_tb1 select * from src_tb1

重命名某列:

create table dst_tb1 select inv_no, sum(unit_cost*quantity) as total_cost from src_tb1 group by inv_no;

在目的表中使用索引,可以在sql语句中指明:

create table dst_tb1 (primary key(id), index(state,city)) select * from src_tb1

有些列属性不能克隆岛目的表,例如auto_increment,可以在目的表创建并完成数据复制之后,使用alter table

来设置目的表相应地属性。

create table dst_tb1 (primary key(id)) select * from src_tb1;

alter table dst_tb1 modify id int unsigned not null auto_increment;

3,使用临时表

所谓临时表就是仅供临时使用,使用结束后MySQL自动删除。

普通的建表语句:

create temporary table tbl_name (列定义);

克隆表:

create temporary table new_table like original_table;

根据查询结果建表:

create temporaray table tbl_name select …;





临时表使用原表的表名,可以创建一个普通表的临时备份,对临时表做任意更改不影响真实数据





4,检查或改变某个表的存储引擎

MySQL支持多种引擎,每一种都有不同的特性,例如InnoDB和BDB支持事务而MyISAM不支持事务

使用information_schema或者show table status或show create table

如获取mail表的信息:













使用alter以及一个engine子句来更改一张表所用的引擎。





5,生成唯一的表名

a,采用随机数;b,采用进程号(同一时间pid肯定不同),但不同时间不能保证;c,连接标识符是唯一值的另一个来源

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: