ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL修炼之路三

2019-10-19 14:50:29  阅读:228  来源: 互联网

标签:sanguo name country MySQL 之路 索引 修炼 表名 select


1. SQL查询

  1. 执行顺序
    3. select ... 聚合函数 from 表名
    1. where ...
    2. group by ...
    4. having ...
    5. order by ...
    6. limit ...

  2. group by
    1. 作用: 给查询结果进行分组
    2. 示例
      1. 查询表中一共有几个国家
        select country from sanguo group by country;
      2. 计算所有国家的平均攻击力
        select country,avg(gongji) from sanguo
        group by country;

        先分组 --> 再聚合 --> 再去重
        蜀国
        蜀国
        蜀国 --> 120 --> 蜀国
        魏国
        魏国 --> 110 --> 魏国
        吴国 --> 115 --> 吴国

      3. 查找所有国家中英雄数量最多的前2名的国家名称和英雄数量

        select country,count(id) as number from sanguo 
        group by country
        order by number desc
        limit 2;

    3. 注意
      1. group by 之后的字段名必须要为select之后的字段名
      2. 如果select之后的字段名和group by 之后的字段不一致,则必须对该字段进行聚合处理(聚合函数)

  3. having 语句
    1. 作用
      对查询的结果进行进一步筛选
    2. 示例
      1. 找出平均攻击力>105的国家的前2名,显示国家名和平均攻击力

        select country,avg(gongji) as power from sanguo
        group by country
        having power > 105
        order by power desc
        limit 2;
    3. 注意
      1. having语句通常和group by 语句联合使用, 过滤由group by 语句返回的记录集
      2. where 只能操作表中实际存在字段,having可操作由聚合函数生成的显示列

  4. distinct
    1. 作用: 不显示字段重复值
    2. 示例
      1. 表中都有哪些国家
        select distinct country from sanguo;

      2. 计算蜀国一共有多少个英雄
        select count(distinct id) from sanguo
        where country ='蜀国';
      3. 注意
        1. distinct和from之间所有字段都相同才会去重
        2. distinct不能对任何字段做聚合处理

  5. 查询表记录时做数学运算
    1. 运算符
      + - * / %
    2. 示例
      1. 查询时所有英雄攻击力翻倍
        select id,name,gongji*2 as gj from sanguo;


        ## sudo apt-get install python3-pip
        ## sudo pip3 install pymysql

2. 约束与索引

  1. 作用:保证数据的完整性、一致性、有效性
  2. 约束分类
    1. 默认约束(default)
      1. 插入记录,不给该字段赋值,则使用默认值
    2. 非空约束(not NULL)
      1.不允许该字段的值有NULL记录
        sex enum("M","F","S") not null default 'S'

  3. 索引
    1. 定义
      对数据库表的一列或多列的值进行排序的一种结构(Btree方式)
    2.优点
      加快数据检索速度
    3. 缺点
      1. 占用物理存储空间
      2. 当对表中数据更新时,索引需要动态维护,降低数据维护速度

    4. 索引示例
      1. 开启运行时间检测:
        show variables like "profiling"; # 查看是否开启
        set profiling=1;
      2. 执行查询语句
        select name from t1 where name='lucy99999';
      3. 查看执行时间
        show profiles;
      4. 在name字段创建索引
        create index name on t1(name);
      5. 再执行查询语句
        select name from t1 where name="lucy88888";
      6. 查看执行时间
        show profiles;

        set profiling=0; # 关闭运行时间检测

3.索引

  1. 普通索引(index)

    1. 使用规则
      1. 可设置多个字段
      2. 字段值无约束
      3. key标志:MUL
    2. 创建index
      1. 创建表时
        create table 表名(...
        index(字段名), index(字段名));
      2. 在已有表中创建索引
        create index 索引名 on 表名(字段名);
        create index name on t3(name);
    3. 查看索引
      1. desc 表名; --> KEY标志为: MUL
      2. show index from 表名\G
    4. 删除索引
      drop index 索引名 on 表名;

  2. 唯一索引(unique)
    1. 使用规则
      1. 可设置多个字段
      2. 约束:字段值不允许重复,但可为NULL
      3. KEY标志: UNI
    2. 创建
      1. 创建表时创建
        unique(字段名),
        unique(字段名)
      2. 已有表
        create unique index 索引名 on 表名(字段名);
    3.查看索引、删除索引 同 普通索引一样

  3. 主键索引(primary key)
    自增长属性(auto_increment,配合主键一起使用)
    1. 使用规则
      1. 只能有一个主键字段
      2. 约束:不允许重复,且不能为NULL
      3. KEY标志: PRI
      4. 通常设置记录编号字段id, 能唯一锁定一条记录
    2. 创建
      1. 创建表时
        (id int primary key auto_increment,)auto_increment=10000; ##设置自增长起始值
        已有表添加自增长属性:
          alter table 表名 modify id int
          auto_increment;
        已有表重新指定起始值:
          alter table 表名 auto_increment=20000;
      2. 已有表
        alter table 表名 add primary key(id);
    3. 删除
      1. 删除自增长属性(modify)
        alter table 表名 modify id int;
      2. 删除主键索引
        alter table 表名 drop primary key;

4.数据导入

  1. 作用:把文件系统的内容导入到数据库中
  2. 语法
    load data infile "文件名"
    into table 表名
    fields terminated by "分隔符"
    lines terminated by "\n";

  3. 将scoretable.csv文件导入到数据库的表中
    1. 在数据库中创建对应的表
      create table scoretab(
      id int,
      name varchar(15),
      score float(5,2),
      number bigint,
      class char(7)
      );
      show create table scoretab;
    2. 把文件拷贝到数据库的默认搜索路径中
      1. 查看默认搜索路径
        show variables like "secure_file_priv";
        /var/lib/mysql-files/
      2. 拷贝文件
        sudo cp ~/scoretable.csv /var/lib/mysql-files/
    3. 执行数据导入语句
      load data infile "文件名"
      into table 表名
      fields terminated by "分隔符"
      lines terminated by "\n";
    4. 文件权限
      rwxrw-rw- 1 chenchuan chenchuan scoretable.csv
        所有者 所属组

        rwx: chenchuan用户
        rw-: 同组其它用户
        rw-: 其他组的其它用户(mysql用户)
        r--> 4
        w--> 2
        x--> 1
      chmod 644 文件名 rw-r--r--
    5. Excel表格如何转化为CSV文件
      1. 打开Excel文件 -> 另存为 —> CSV(逗号分隔)
    6. 更改文件编码格式
      1.用记事本/编辑器 打开,文件->另存为->选择编码

5.数据导出

  1. 作用
    将数据库中表的记录导出到系统文件里
  2. 语法格式
    select ... from 表名
    into outfile "/var/lib/mysql-files/文件名"
    fields terminated by "分隔符"
    lines terminated by "\n";
  3. 把MOSHOU库下的sanguo表英雄的姓名、攻击值、国家导出来,sanguo.txt
    select name, gongji, country from MOSHOU.sanguo
    into outfile "/var/lib/mysql-files/sanguo.txt"
    fields terminated by " "
    lines terminated by "\n";

  4. 将mysql库下的user表 user、host两个字段的值导出到user.txt
    select user,host from mysql.user
    into outfile "/var/lib/mysql-files/user.txt"
    fields terminated by " "
    lines terminated by "\n";

 

标签:sanguo,name,country,MySQL,之路,索引,修炼,表名,select
来源: https://www.cnblogs.com/zhangchenchuan/p/11703797.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有