zoukankan      html  css  js  c++  java
  • Python全栈 MySQL 数据库 (索引、数据导入、导出)

    ParisGabriel
     
     
             每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰
     
      开局一张图
     
     
    表字段重命名(change)
      alter table 表名 change 原名 新名 数据类型;
     
    SQL查询
     
    执行顺序:
        3. select ...聚合函数 from 表名
        1. where ...
        2. group by...
        4. having...
        5. order by...
        6. limit...
    group by 语句
       作用:给查询结果进行分组
       注意:
         1.group by之后字段必须要select之后字段
         2.如果select之后的字段和group by 之后的字段不一致,则必须
           字段进行聚合处理(聚合函数)
    having 语句
       作用:对查询结果进一步筛选
       注意:
          1.having语句通常和group by联合使用过滤group by语句返回的记录集
          2.where只能操作表中实际存在的字段having by可操作由聚合函数生成的显示列
    distinct
       作用:不显示字段重复值
       注意:
          1.distinct和from之间所有字段都相同 才会去重
          2.distinct不能对任何字段做聚合处理
     运算符:
          +     -    *    /    %
       ## sudo apt-get install python3-pip     安装pip3
       ## sudo pip3 install pymysql                安装mysql
     
      约束:
       1.作用保证数据完整性、一致性、有效性
       2.约束分类
            1.默认约束default
                  插入字段给该字段赋值 则使用默认值
            2.非空约束not NULL
                 不允许该字段值有NULL记录
                 sex enum(“M”,"F","S") not null defaulf "S"
    索引
      定义:
        对数据库表色一列或多列的值进行排序的一种结构
        (Btree方式)
     

     
      优点:
         加快数据的检索速度
      缺点:
         1.需要占用物理存储空间
         2.当对表中数据更新时,索引需要动态维护降低
           数据维护速度 占用系统资源
    运行时间检测:
       开启:set profiling=1;
       关闭:set profiling=0;
       查询MySQL变量:show variables like profiling;
       查询执行记录:show profilings;
    字段创建索引:
        create index name on t1(字段名);
    索引的分类:
        1.普通索引(index)
           使用规则:
                1.可设置多个字段
                2.字段值无约束
                3.key标志:MUL
           创建index
                创建表时创建
                create table(....
                  ...
                 index(字段名),
                 index(字段名2)...)
           已有表添加index
                 create index 索引名 on 表名(字段名);
           查看索引:
                1.desc 表名;   key:MUL
                2.show index from 表名
                3.show index fromG;
           删除索引:
                drop index 索引名 on 表名;
        2.唯一索引(unique)
           使用规则:
                1.可以设置多个字段
                2.约束:字段值不允许重复,但可以为NULL
                3.key标志:UNI
           创建unique:
              1.创建表时创建
                 unique(字段名),..
              2.已有表
                   create unique index 索引名 on 表名(字段名);
           查看、删除 和普通索引一致
              
        3.主键索引(primary key)
              自增属性auto_increment配合主键一起使用
           使用规则:
               1. 只能有一个主键字段
               2. 约束:不允许重复,且不能为NULL
               3.key标志:PRI
               4.通常设置记录编号字段id,能唯一锁定一条记录
           创建primary key
             创建表时:
               1.id int primary key auto_increment,
               2.起始值:表()auto_inctement=10000;
            已有表:
                alter table 表名 add primary key(id);
                添加:alter table 表名 modify id int auto_inctement
           删除:
              1.删除自增属性(modify)
                  alter table 表名 modify id int
              2.删除主键索引
                  alter table 表名 drop primary key
     
        4.外键索引........
    算法全是btree 节省时间都一样  不同的是约束不同
     
    这里btree 算法 有人说btree就是btree 不是二叉树  但是我觉得就是二叉树 没什么区别
    根据数据量的大小 提升速度  快能达到几百倍的提速
     
     
    数据导入:
       作用:
          把文件系统的内容导入到数据库
       语法:
         load data  infile “文件名”
         into table 表名
         fields terminated by “分隔符”
         lines terminated by “ ”;
    步骤:
           1.数据库创建对应
           2.把文件拷贝到数据库的默认搜索路径
              1.查看默认路径 
                  show variables like “secure_file_priv”;
                  /var/lib/mysql-files/
              2.拷贝文件
                  sudo cp ~/scoretable.csv /var/lib/mysql-files/
           3.把表导入到数据库
    数据导出:
       作用:
         数据库中表的记录导出到系统文件里
       语法:
          select ... from 表名
          into outfile “/var/lib/mysql-files/文件名”
          fields terminated by “分隔符”
          lines terminated by “ ”;
    步骤:
        1.直接执行导出命令
        2.自动创建文件
        3.默认导出到默认搜索路径
     
     
      文件权限:
           rwxrw-rw- 1 tarena tarena 
                                所有者 所属组
            rwx:tarena用户
            rw-:同组其他用户
            rw-:其他组的用户(mysql)
               r:  4
               w:  2
               x:  1
               最高权限:7
         查看权限:ls -l 文件名
            修改文件权限:chmod  644 文件名
     
    Excel表格如何化为CSV文件
            打开Excel文件 -> 另存为 -> CSV(逗号分隔)
    更改文件编码格式
            用记事本/编辑器 打开,文件->另存为->选择编码
     
     
    导入示例:

     

    将scoretable.csv文件导入到数据库的表中
        1、在数据库中创建对应的表
          create table scoretab(
          id int,
          name varchar(15),
          score float(5,2),
          number bigint,
          class char(7)
          );
        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 "/var/lib/mysql-files/scoretable.csv"
          into table scoretab
          fields terminated by ","
          lines terminated by "
    ";

    导出示例:

    把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 "
    ";
    
      将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 "
    ";
    
    查询
        $ sudo -i
        $ cd /var/lib/mysql-files/
        $ ls
        $ cat sanguo.txt
  • 相关阅读:
    如何手动封装 $ on off emit?
    Vue 实例身上的一些方法(二)
    Vue 实例身上的一些方法(一)
    Vue属性过滤
    Vue属性监听
    Vue实现简单的商品增减功能
    Vue 计算属性
    使用Vue实现一个简单地自定义拖拽功能
    数组的深拷贝与浅拷贝
    如何让html引用公共布局(多个html文件公用一个header.html和footer.html)
  • 原文地址:https://www.cnblogs.com/ParisGabriel/p/9404696.html
Copyright © 2011-2022 走看看