MySQL(Python)-Day03

目录:

MySQL-Day03笔记

索引(index)

  1. 定义
    对数据库中表的一列或者多列的值进行排序的一种结构(MySQL中索引用BTREE方式)
  2. 优点
    可以加快数据的检索速度
  3. 缺点
    1、当对表中的数据进行增加、删除和修改的时候,索引需要动态维护,降低了数据的维护速度
    2、索引需要占用物理空间
  4. 索引示例
    1. 开启运行时间检测
      show variables like “%pro%”;
      set profiling=1;
    2. 执行查询语句
      select name from t1 where name=”lucy1009999”;
    3. 查看执行时间
      show profiles;
    4. 在name字段创建索引
      create index index_name on t1(name);
    5. 执行查询语句
      select name from t1 where name=”lucy1009998”;
    6. 查看执行时间
      show profiles;
  5. 索引类型

    1. 普通索引(index)
      1. 使用规则
        1. 一张表中可以有多个index字段
        2. 字段的值可以重复,且可以为NULL
        3. 经常把做查询条件的字段设置为index字段
        4. index字段的key标志为 MUL
      2. 创建index
        1. 创建表
          … index(字段名),index(字段名));
        2. 已有表
          create index 索引名 on 表名(字段名);
      3. 查看索引
        1. desc 表名;
        2. show index from 表名;
      4. 删除index
        drop index 索引名 on 表名;
        ## 删除index只能一个一个删
    2. 唯一索引(unique)
      1. 使用规则
        1. 一个表中可以有多个unique字段
        2. 对应字段的值不允许重复,但可以为NULL
        3. key标志:UNI
          2、创建
        4. 创建表
          … unique(字段名),unique(字段名));
          … unique(phnumber),unique(cardnumber));
        5. 已有表
          create unique index 索引名 on 表名(字段名);
        6. 查看/删除(同index)
          drop index 索引名 on 表名;
    3. 主键索引(primary key)&&自增长属性(auto_increment)配合主键一起使用
      1. 使用规则
        1. 一个表中只能有一个主键字段
        2. 对应字段值不允许重复,且不能为NULL
        3. key标志:PRI
        4. 把表中能够唯一标识一条记录的字段设置为主键,通常把记录编号的字段设置为主键
      2. 创建
        1. 创建表
          … id int primary key auto_increment,
          … …)[auto_increment=10000];
          … id int auto_increment,
          … primary key(id))[auto_increment=1000];
        2. 在已有表中创建
          alter table 表名 add primary key(字段名);
        3. 删除主键
          1. 删除auto_increment属性
            alter table 表名 modify id int;
          2. 删除主键
            alter table 表名 drop primary key;
        4. 在已有表中添加自增长属性并指定起始值
          1. 添加自增长属性
            alter table 表名 modify id int auto_increment;
          2. 指定起始值
            alter table 表名 auto_increment=值;
    4. 外键索引(foreign key)

      1. 定义
        让当前表的字段值在另一个表的范围内选择
      2. 语法格式
        foreign key(参考字段名)
        references 被参考表名(被参考字段名)
        on delete 级联动作
        on update 级联动作
      3. 示例
        表1:缴费信息表(财务)
        学号 姓名 班级 金额
        1 唐伯虎 二班 200
        2 点秋香 二班 300
        3 祝枝山 二班 500

        表2:班级信息表(班主任)
        学号 姓名 金额
        1 唐伯虎 200
        2 点秋香 300
        表1:
        create table jftab(
        id int primary key,
        name varchar(20),
        class varchar(7),
        money int)character set utf8;
        insert into jftab values
        (1,”唐伯虎”,”AID04”,200),
        (2,”点秋香”,”AID04”,300),
        (3,”祝枝山”,”AID04”,500);
        表2:
        create table bjtab(
        stu_id int,
        name varchar(20),
        money int,
        foreign key(stu_id) references jftab(id) on delete cascade on update cascade
        )character set utf8;
        insert into bjtab values
        (1,”唐伯虎”,200),
        (2,”点秋香”,300);

      4. 级联动作
        1. cascade :数据级联更新
          当主表删除记录或更改被参考字段值时,从表级联更新
        2. restrict(默认)
          当主表删除记录或更改被参考字段值时,从表中有相关联记录则不允许主表操作
        3. set null
          当主表删除记录或更改被参考字段值时,从表中相关联记录的字段值设置为 NULL
        4. no action
          同 restrict,都是立即检查外键限制
      5. 删除外键限制
        alter table 表名 drop foreign key 外键名;
        外键名查看方式:show create table 表名;
      6. 在已有表中添加外键限制
        alter table 表名 add
        foreign key(..) references 表名(字段名)
        on delete 级联动作
        on update 级联动作
      7. 使用规则
        1. 两张表参考字段和被参考字段数据类型要一致
        2. 被参考字段必须为key的一种,通常primary key

数据导入

  1. 作用 :把文件系统的内容导入到数据库中
  2. 语法格式
    load data infile “文件名”
    into table 表名
    fields terminated by “分隔符”
    lines terminated by “\n”
  3. 示例
    1. 把 /ect/passwd 文件中的内容导入到 db3下的userinfo表
      tarena : x : 1000 : 1000 :
      用户名 密码 uid gid
      tarena,,, : /home/tarena : /bin/bash
      用户描述 主目录 登录权限
  4. 操作步骤
    1、在数据库中创建对应的表
    create table userinfo(
    username varchar(20),
    password char(1),
    uid int,
    gid int,
    comment varchar(50),
    homedir varchar(50),
    shell varchar(50)
    )character set utf8;
    1. 把要导入的文件拷贝到数据库搜索路径中
      1. 查看数据库搜索路径
        show variables like “secure_file_priv”;
      2. sudo cp /etc/passwd /var/lib/mysql-files/
    2. 执行数据导入语句
      load data infile “/var/lib/mysql-files/passwd”
      into table userinfo
      fields terminated by “:”
      lines terminated by “\n”;
  5. 练习

    1. 将AID1709.csv文件导入到 aid1709 表中

    2. 创建表
      create table aid1709(
      id int,
      name varchar(20),
      score float(5,2),
      phone bigint,
      class char(7)
      )character set utf8;

    3. 拷贝文件
      $ : sudo cp ~/AID1709.csv /var/lib/mysql-files/
    4. 执行数据导入语句
      load data infile “/var/lib/mysql-files/AID1709.csv”
      into table aid1709
      fields terminated by “,”
      lines terminated by “\n”;

数据导出

  1. 作用
    将数据库中表记录保存到系统文件里
  2. 语法格式
    select … from 表名
    into outfile “文件名”
    fields terminated “分隔符”
    lines terminated by “\n”
  3. 示例
    1. 把userinfo表中的用户名、密码和uid号三个字段的值给导出来
      -> select username,password,uid from userinfo
      -> into outfile “/var/lib/mysql-files/user1.txt”
      -> fields terminated by ” ”
      -> lines terminated by “\n”;
    2. 把 mysql库下的user表中 user、host的值导出来,user.csv
      -> select user,host from mysql.user
      -> into outfile “/var/lib/mysql-files/user.csv”
      -> fields terminated by “,”
      -> lines terminated by “\n”;
  4. 注意
    1、导出的内容由SQL查询语句决定
    2、执行导出命令时路径必须指定对应的数据库目录下
    ### chmod 777 文件名
    ### chmod +rw 文件名
  5. 表的复制
    1. 表的重命名:alter table 表名 rename 新表名;
    2. 复制语法格式
      create table 表名 select … from 表名 …;
    3. 示例
      1、复制 aid1709 表的全部记录和字段,aid17092
      create table aid17092 select * from aid1709;
      2、复制 aid1709 表中成绩低于65分的学生到表next
      create table next select * from aid1709 where socre<65;
    4. 只复制表结构
      create table 表名 select * from 表名 where false;
  6. 嵌套查询(子查询)
    1. 定义 :把内层的查询结果作为外层查询的条件
    2. 语法
      SQL查询语句 where 条件(SQL查询语句)
    3. 示例
      1. 把uid的值小于这个平均值的用户名和uid号显示出来
        select username,uid from userinfo
        where uid<(select avg(uid) from userinfo);
      2. 找出每个国家攻击力最高的英雄的名字和攻击值
        -> select name,gongji from sanguo
        -> where gongji in(
        -> select max(gongji) from sanguo group by country);

猜你喜欢

转载自blog.csdn.net/luohongtucsdn/article/details/80911832