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,连接标识符是唯一值的另一个来源

  • 相关阅读:
    Typescript类、命名空间、模块
    TypeScript 基础类型、变量声明、函数、联合类型、接口
    JS中的单线程与多线程、事件循环与消息队列、宏任务与微任务
    wangEditor上传本地视频
    java版excel转pdf,word转pdf
    idea2019.3 没有 Autoscroll from Source
    mysql 实现类似oracle函数bitand功能
    spring boot 配置文件动态更新原理 以Nacos为例
    spring boot 发布自动生成svn版本号
    spring boot JPA 数据库连接池释放
  • 原文地址:https://www.cnblogs.com/burness/p/3712551.html
Copyright © 2011-2022 走看看