day04数据库

昨日知识点回顾

1.单表操作

  1. 1.单表的操作
  2. 条件查询的优先级别:
  3. where > group by >having > order by > limit;
  4. 分组:group by
  5. select gender count(id) from xxx group by gender;
  6. 过滤筛选:having
  7. select gender count(id) from xxx group by gender having count(id)>13;
  8. 排序:order by asc升序,默认,desc 降序
  9. order by 要排序的字段名(id);
  10. 升序:asc 例子:order by id asc;
  11. 降序:desc 例子:order by id desc;
  12. order by age asc,id desc;
  13. 先根据年龄升序,然后如果年龄一样的时候,再根据id进行降序。
  14. 分页:
  15. limit offset,size;
  16. offset:行数据的索引 size:取多少航数据
  17. limit 起始值,终止值;
  18. select * from 表名 limit 0,10; 取出前十条数据

2.多表关系

  1. 表与表之间的关系分为:一对多、一对一、多对多关系。
  2. 一对多:
  3. create table department(
  4. id int,
  5. name varchar(32) not null default ''
  6. )charset utf8;
  7. create table userinfo(
  8. id int,
  9. name varchar(32) not null default '',
  10. depart_id int,
  11. constraint 外键名 foreign key(depart_id) references department(id)
  12. )charset utf8;
  13. 多对多:
  14. create table boy(
  15. id int,
  16. bname varchar(32) not null default ''
  17. )charset utf8;
  18. create table girl(
  19. id int,
  20. gname varchar(32) not null default ''
  21. )charset utf8;
  22. create table boy2girl(
  23. id int,
  24. bid int,
  25. gid int,
  26. constraint 外键名 foreign key(bid) references boy(id),
  27. constraint 外键名 foreign key(gid) references girl(id)
  28. )charset utf8;
  29. 一对一关系:
  30. create table userinfo(
  31. id int,
  32. name varchar(32)
  33. )charset utf8;
  34. create table priv(
  35. id int,
  36. salary decimal(20,3),
  37. pid int,
  38. unique(pid),
  39. constraint 外键名 foreign key(pid) references userinfo(id)
  40. )charset utf8;

3.多表查询

  1. 多表查询中的语法:
  2. left join......on
  3. right join.....on
  4. inner join......on
  5. 举例:
  6. select * from department left join userinfo on department.id=userinfo.depart_id;

今日内容

2.python 操作MySQL数据库

​ 安装pymysql

  1. sql注入问题
  2. 输入用户名:zekai 'or 1=1 #
  3. 输入密码:dsadsa
  4. select * from user where name='zekai' or 1=1 #' and password='dsdfssdd''
  5. 直接就可以登录成功,产生问题的原因在于:
  6. 特殊符号没有过滤,以及使用的关键字没有过滤,没有任何的校验。#相当于是注释,or只要满足一个条件即可,and是需要同时满足两个条件。
  1. 解决sql注入问题的方法:
  2. 通过execute执行函数,这是Python内部封装好的方法,只需要拿来使用即可。
  3. sql='select * from user where name=%s and password=%s'
  4. cursor.execute(sql,(user,pwd))
  5. 连接数据库:
  6. conn=pymysql.connect(
  7. host='localhost',
  8. user='root',
  9. password='root',
  10. database='test',
  11. charset='utf8'
  12. )
  13. cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)##返回的是字典类型
  14. 查:
  15. fetchall() :取出所有的数据,返回的是列表套字典
  16. fetone() :取出一条数据,返回的是字典
  17. fetchmany(size) :取出size条数据,返回的是列表套字典
  18. 增:
  19. sql='insert into user(name,password) values(%s,%s)'
  20. #cursor.execute(sql,('xxx','qwe'))这个是新增一条数据
  21. 下面是新增多条数据:
  22. data=[
  23. ('zekai','qqq'),
  24. ('zekai2','www'),
  25. ('zekai3','eee'),
  26. ('zekai4','rrr'),
  27. ('zekai5','ttt')
  28. ]
  29. cursor.executemany(sql,data) 新增多条数据
  30. conn.commit() 加上这句代码,表示提交数据
  31. 打印最后一行的ID值:
  32. print(cursor.lastrowid)
  33. 修改:
  34. sql='update user set name=%s where id=%s'
  35. cursor.execute(sql,('huaheshang',300))
  36. conn.commit()
  37. cursor.close()
  38. conn.close()
  39. 删除:
  40. sql='delete from user where id=%s'
  41. cursor.execute(sql,(2,))
  42. conn.commit()
  43. cursor.close()
  44. conn.close()

索引

为啥使用索引以及索引的作用:

​ 使用索引就是为了提高查询效率的。

类比:

​ 字典中的目录

索引的本质:

​ 一个特殊的文件

索引的底层原理:

​ B+ 树

索引的种类:************************

分为:主键索引、唯一索引、联合唯一索引、普通索引

1.主键索引:加速查找+不能重复+不能为空 primary key

2.唯一索引:加速查找+不能重复 unique(字段名)

2.1联合唯一索引:unique(name,email) 就是这两个字段都是唯一并且不会重复的字段

  1. 联合唯一索引:uniquename,email 就是这两个字段都是唯一并且不会重复的字段
  2. 例子如下:
  3. zekai 123@qq.com
  4. zekai 123@qq.com (error错误数据)
  5. 因为zekai 123@qq.com 已经存在了,所以下面的一条数据就不能重复再次出现,
  6. 所以执行的时候才会报错。

3.普通索引:加速查找 index(name)

3.1联合普通索引:index(name,email)

索引的创建:

​ 主键索引:

  1. 新增主键索引:
  2. 增加主键索引的3种方式:
  3. 1.
  4. create table xxx(
  5. id int auto_increment primary key,
  6. )charset utf8;
  7. 2.
  8. alter table xxx change id id int auto_increment primary key;
  9. 3.
  10. alter table t1 add primary key(id);
  11. 删除主键索引:
  12. alter table t1 drop primary key;
  13. 唯一索引:
  14. 增加索引的3种方式:
  15. 1.
  16. create table t2(
  17. id int auto_increment primary key,
  18. name varchar(60) not null default '',
  19. unique u_name(name)
  20. )charset utf8;
  21. 2.
  22. create unique index 索引名 on 表名(字段名);
  23. create unique index ix_name on t2(name);
  24. 3.
  25. alter table t2 add unique index ix_name(name);
  26. 删除索引的sql命令:
  27. alter table t2 drop index u_name;

普通索引:

  1. 新增的三种方式:
  2. 1.
  3. create table t3(
  4. id int auto_increment primary key,
  5. name varchar(60) not null default '',
  6. index u_name(name)
  7. )charset utf8;
  8. 2.
  9. create index 索引名 on 表名(字段名);
  10. create index ix_name on t3(name);
  11. 3.
  12. alter table t3 add index ix_name(name);
  13. 删除普通索引:
  14. alter table t3 drop index u_name;
  15. 索引的优缺点:
  16. 通过观察*.ibd 文件可知:
  17. 1.索引加快了查询速度
  18. 2.但是在加了索引之后,会占用大量的磁盘空间
  19. 索引是不是加的越多越好?
  20. 不是!!!!!!
  21. 不会命中索引的情况:
  22. 1.不能在sql语句中,进行四则运算,会降低sql的查询效率
  23. 2.使用函数,也会降低sql的查询效率
  24. select * from tb1 where reverse(email)='zekai';
  25. 3.查询的数据类型不一致,也会降低sql的查询效率
  26. 如果列是字符串类型,where条件必须用引号引起来,不然会被警告
  27. select * from tb1 where email=999; #这里面正确的应该是email='999'
  28. 4.排序条件为索引,则select查询的字段name必须也是索引字段,否则无法命中
  29. order by
  30. select name from s1 order by name desc; ----快
  31. 当根据索引排序的时候,select查询的字段如果不是索引,则速度仍然很慢
  32. select email from s1 order by email desc; ----慢
  33. 特别的:
  34. 如果对主键排序,则还是速度很快,如下:
  35. select * from tb1 order by id desc;
  36. 5.我们使用count(列名)对要查询的分组计算数量,更加具有针对性,不推荐使用count(*)
  37. 6.组合索引最左前缀
  38. 什么时候会创建联合索引?
  39. 根据公司的业务场景,在最常用的几个列上添加索引!!!
  40. select * from user where name='zekai' and email='zekai@qq.com';
  41. 如果遇到上述业务情况,错误的做法是添加两个普通索引:
  42. index ix_name(name),
  43. index ix_email(email)
  44. 正确的做法是创建联合索引:
  45. index ix_name_email(name,email) ----这个才是正确的做法!!!
  46. 如果组合索引为:ix_name_email(name,email)*******
  47. where name='zekai' and email='xxxx' ---命中索引
  48. where name='zekai' ---命中索引
  49. where email='zekai@qq.com' ---未命中索引
  50. 例子:
  51. index(a,b,c,d)
  52. where a=2 and b=3 and c=4 and d=5 ----命中索引,因为只要a存在就可以
  53. where a=2 and c=3 and d=4 ---命中索引,因为只要a存在就可以
  54. where c=3 and d=4 ---未命中索引,因为a不存在查询失败

explain解释sql查询语句

  1. explain select * from user where name='zekai' and email='zekai@qq.com'\G
  2. id: 1
  3. select_type: SIMPLE
  4. table: user
  5. partitions: NULL
  6. type: ref 索引指向 all全部
  7. possible_keys: ix_name_email 可能用到的索引
  8. key: ix_name_email 确实用到的索引
  9. key_len: 214 索引长度
  10. ref: const,const
  11. rows: 1 扫描的长度
  12. filtered: 100.00
  13. Extra: Using index 使用到了索引
  14. 索引覆盖:
  15. select id from user where id=2000;

慢查询日志,查看慢sql的相关变量

  1. 展示sql慢查询的变量:
  2. show variables like '%slow%';
  3. +---------------------------+-----------------------------------------------+
  4. | Variable_name | Value |
  5. +---------------------------+-------------------------------+
  6. | log_slow_admin_statements | OFF |
  7. | log_slow_slave_statements | OFF |
  8. | slow_launch_time | 2 |
  9. | slow_query_log | OFF ### 默认关闭慢SQl查询日志,on是开启 |
  10. | slow_query_log_file | D:\mysql-5.7.28910UNQE-slow.log | ## 慢SQL记录的位置
  11. +---------------------------+------------------------------------+
  12. 5 rows in set, 1 warning (0.08 sec)
  1. 展示sql慢查询的时间变量:
  2. show variables like '%long%';
  3. +----------------------------------------------+-----------+
  4. | Variable_name | Value |
  5. +----------------------------------------------------------+
  6. | long_query_time | 10.000000 |

配置慢sql的变量:

  1. 固定语法为:
  2. set global 变量名 =
  3. 设置慢sql查询日志打开:
  4. set global slow_query_log = on;
  5. 设置慢sql查询文件的保存位置:
  6. set global slow_query_log_file = "D:/mysql-5.7/day004";
  7. 设置慢sql查询文件的时间:【sql查询时间超过1秒的sql语句,都会保存到指定的慢sql查询文件中】
  8. set global long_query_time = 1;

MySQL高级查询之索引创建、删除、增加、修改、慢sql、explain解释sql的更多相关文章

  1. 第三章 MySQL高级查询(一)

    第三章 MySQL高级查询(一) 一.SQL语言的四个分类 1.       DML(Data Manipulation Language)(数据操作语言):用来插入,修改和删除表中的数据,如INSE ...

  2. MySQL 高级查询操作

    目录 MySQL 高级查询操作 一.预告 二.简单查询 三.显示筛选 四.存储过程 五.查询语句 1.作为变量 2.函数调用 3.写入数据表 备注 附表一 附表二 相关文献 博客提示 MySQL 高级 ...

  3. MySQL高级查询与编程作业目录 (作业笔记)

    MySQL高级查询与编程笔记 • [目录] 第1章 数据库设计原理与实战 >>> 第2章 数据定义和操作 >>> 2.1.4 使用 DDL 语句分别创建仓库表.供应 ...

  4. mysql 优化实例之索引创建

    mysql 优化实例之索引创建 优化前: pt-query-degist分析结果: # Query 23: 0.00 QPS, 0.00x concurrency, ID 0x78761E301CC7 ...

  5. 第四章 MySQL高级查询(二)

    第四章 MySQL高级查询(二) 一.EXISTS子查询 在执行create 或drop语句之前,可以使用exists语句判断该数据库对像是否存在,返回值是true或false.除此之外,exists ...

  6. python进阶09 MySQL高级查询

    python进阶09 MySQL高级查询 一.筛选条件 # 比较运算符 # 等于:= 不等于:!= 或<> 大于:> 小于:< 大于等于>= 小于等于:<= #空: ...

  7. MySQL高级查询与编程笔记 • 【目录】

    章节 内容 实践练习 MySQL高级查询与编程作业目录(作业笔记) 第1章 MySQL高级查询与编程笔记 • [第1章 数据库设计原理与实战] 第2章 MySQL高级查询与编程笔记 • [第2章 数据 ...

  8. C#操作Access的查询、添加、删除、修改源程序

    C#操作Access的查询.添加.删除.修改源程序 using System; using System.Collections.Generic; using System.ComponentMode ...

  9. mysql中一半会选择什么样的字段为索引?(含索引创建删除查看公式)

    一.数据量庞大的数据做索引 二.该字段经常出现在where的后面,以条件形式存在,经常被用户搜索的字段 三.很少被增删改的字段,因为增删改后,索引会重新排序 索引的创建 create index 索引 ...

随机推荐

  1. Netflix 开源 Polynote:对标 Jupyter,一个笔记本运行多种语言

    谈到数据科学领域的开发工具,Jupyter 无疑是非常知名的一种.它具有灵活高效的特点,非常适合进行开发.调试.分享和教学.近日,Netflix(奈飞)居然也玩起了跨界,他们开源了一个名为 Polyn ...

  2. Docker (一) 安装 Oracle18c

    通过Docker 安装 Oracle18c 1.拉取 oracle18c 镜像 docker pull registry.cn-hangzhou.aliyuncs.com/zhengqing/orac ...

  3. 【Luogu 3275】[SCOI2011]糖果

    Luogu P3275 显然是一道经典的差分约束系统 相关知识可以查看:[Luogu 1993]差分约束系统问题--小K的农场 值得注意的是这题使用最长路更合适,因为每一个人都要取得至少一个糖果.在添 ...

  4. DBEntry.Net 简介

    [尊重作者:本文转自:http://www.cnblogs.com/lephone/]   这是我设计的一个轻量级的 .Net ORM (Object Relational Mapping) 数据访问 ...

  5. 1像素border的实现(vue.js)

  6. selenium webdriver学习--------iframe的处理

    有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,代 码也没有任何问题.这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的原因之一.如果你在 ...

  7. Java的 FileWriter类 和 FileReader类

    一.FileReader类1,构造方法:FileReader fr = new FileReader(String fileName);//使用带有指定文件的String参数的构造方法.创建该输入流对 ...

  8. 从spring源码汲取营养:模仿spring事件发布机制,解耦业务代码

    前言 最近在项目中做了一项优化,对业务代码进行解耦.我们部门做的是警用系统,通俗的说,可理解为110报警.一条警情,会先后经过接警员.处警调度员.一线警员,警情是需要记录每一步的日志,是要可追溯的,比 ...

  9. LNMP 源码发布Thinksaas论坛

    第一步:搭建LNMP架构 LNMP架构 注意:搭建php服务时,初始化 ./configure --prefix=/usr/local/php5 \ --enable-fpm \ --enable-d ...

  10. MyEclispe启动Tomcat7时出现错误The servlets named [LoginServlet] and [com.liu.control.LoginServlet] are both

    刚开始尝试写Servlet代码,第一天就碰到这个错误,在网上找了很多资料才找到解决办法,在此记录一下. org.apache.catalina.LifecycleException: Failed t ...