zoukankan      html  css  js  c++  java
  • MySQL Cookbook学习笔记第四章

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

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

    image

    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 …;

    image

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

    image

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

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

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

    如获取mail表的信息:

    image

    image

    image

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

    image

    5,生成唯一的表名

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

  • 相关阅读:
    xdoj 1237 (贪心+逆向思维)
    linux系统 (实验一)实验楼的课程笔记
    fc_net.py cs231n
    cnn.py cs231n
    optim.py cs231n
    layers.py cs231n
    Python数据分析与展示[第一周]
    配置了两天python【python可以的】
    PYTHON网络爬虫与信息提取[scrapy框架应用](单元十、十一)
    PYTHON网络爬虫与信息提取[正则表达式的使用](单元七)
  • 原文地址:https://www.cnblogs.com/burness/p/3712551.html
Copyright © 2011-2022 走看看