mysql

1 mysql命令

# mysqld install  安装mysql服务  mysql服务就被注册到操作系统中
# net start mysql 启动mysql服务
# net stop mysql

# 命令行\c取消执行

# 启动客户端连接server
# mysql -uroot -p
# mysql -uroot -p123 -h192.168.14.12        #123是密码  192.168.。。。。是连接哪台设备上的数据库

# mysql>select user();   查看当前登录的用户
# mysql>set password = password('123'); 给当前用户设置密码
#
# 创建一个其他用户
# create user 'guest'@'192.168.14.%' identified by '123';
# 给一个用户授权
# grant 权限类型 on ftp作业.* to 'guest'@'192.168.14.%';
# grant all
# grant select on day37.* to 'guest'@'192.168.14.%';    .*代表库里的所有的表
# grant select,insert

# 创建账号并授权
# mysql> grant all on *.* to 'eva'@'%' identified by '123'

# 本机ip地址192.168.2.196

# 操作数据库
# 查看所有数据库  show databases;
# 创建一个数据库  create database 数据库名;
# 切换到这个库下  use 数据库的名字
# 查看这个库下有多少表 show tables;

# 操作表
# 创建一张表
# create table student(name char(12),age int);
# 查看表结构
# desc student;

# 操作数据
# 插入数据 : insert into student values ('xianming',73),('xianhong',75);
# 查询数据 : select * from student;
# 修改数据 : update student set age=85 where name='jack';
# 删除数据 : delete from student where name = 'jack';

my.ini文件

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=D:\mysql-5.6.49-winx64
# 设置mysql数据库的数据的存放目录
datadir=D:\mysql-5.6.49-winx64\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

# 数据库 : DB
    # 所有的数据存放的仓库
    # 每一个文件夹也是一个数据库
# 数据库管理系统  -- 软件 DBMS
    # 关系型数据库 : mysql oracle sqllite sql server db2 access
    # 非关系型数据库 : redis mongodb memcache
# 数据库管理员  DBA
    # 管理数据库软件
# 数据库服务器 一台跑着一个数据库管理软件的机器
# 表 : 文件,一张存储了数据的表
# 数据/记录 : 表中的信息,一行就是一条记录

# 用户相关操作
# 查看当前用户是谁? select user();
# 给当前用户设置密码 set password = password('123');
# 创建用户 create user '用户名'@'主机的ip/主机域名' identified by '密码'
# 授权 grant select on 数据库名.* to '用户名'@'主机的ip/主机域名' identified by '密码'
# 授权并创建用户 grant select on 数据库名.* to '用户名'@'主机的ip/主机域名'

# 基础的库\表\数据操作
# 库 - 文件夹
    # 创建库             create database 数据库名;
    # 切换到这个库下     use 库名
    # 查看所有库         show databases;
# 表 - 文件
    # 查看这个库下的所有表  show tables;
    # 创建表                create table 表名(字段名 数据类型(长度),字段名 数据类型(长度),..);
    # 删除表                drop table 表名;
    # 查看表结构            desc 表名;
        # describe 表名;  查看表结构

# 数据(记录) - 文件中的内容
    # 增 : insert into 表 values (一行数据),(一行数据),(一行数据);
    # 删 : delete from 表 where 条件;
    # 改 : update 表 set 字段名=,字段2=2 where 条件;
    # 查 : select 字段 from;

# ip和域名
# 搜索的机器
# 10.125.23.1  sogou.search01.org
# 10.125.23.2  sogou.search02.org
# 10.125.23.3  sogou.search03.org
# 10.125.23.4  sogou.search04.org
# 浏览器
# 10.135.24.7
# 10.135.24.8
# 10.135.24.9
# 10.135.24.10
#
# www.baidu.com 136.17.2.2

存储引擎

# 存储引擎 -- 存储数据的方式
# 一张表
    # 数据
    # 表的结构
    # 索引(查询的时候使用的一个目录结构)

# Innodb存储引擎    mysql5.6之后的默认的存储引擎
# 数据和索引存储在一起 2个文件
    # 数据索引\表结构
# 数据持久化
# 支持事务   : 为了保证数据的完整性,将多个操作变成原子性操作   : 保持数据安全
# 支持行级锁 : 修改的行少的时候使用                          : 修改数据频繁的操作
# 支持表级锁 : 批量修改多行的时候使用                        : 对于大量数据的同时修改
# 支持外键   : 约束两张表中的关联字段不能随意的添加\删除      : 能够降低数据增删改的出错率


# Myisam存储引擎    mysql5.5之前的默认的存储引擎
# 数据和索引不存储在一起  3个文件
    # 数据\索引\表结构
# 数据持久化
# 只支持表锁

# Memory存储引擎
# 数据存储在内存中, 1个文件
    # 表结构
# 数据断电消失

表和数据的基本操作

# create table 表名(
# id int,
# name char(18),
# 字段名3 类型[(宽度) 约束条件]
# );
# 放在中括号里的内容可以不写


# 写入数据的方式
# insert into 表 values (1,2,3);
    # 这张表有多少的字段,就需要按照字段的顺序写入多少个值
# insert into 表 values (1,2,3),(1,2,3),(1,2,3);
    # 一次性写入多条数据
# insert into  (字段1,字段3 ) values (1,3);
    # 指定字段名写入,可以任意的选择表中你需要写入的字段进行

# 查表中的数据
    # select * from 表

# 查看表结构
    # desc 表名;
        # 能够查看到有多少个字段\类型\长度,看不到表编码,引擎,具体的约束信息只能看到一部分
    # show create table 表名;
        # 能查看字段\类型\长度\编码\引擎\约束

数据类型数字

# int 不约束长度,最多表示10位数
# float(m,n)
    # m 一共多少位,
    # n 小数部分多少位

# create table t1(
#     id int,               # 默认是有符号的
#     age tinyint unsigned  # 如果需要定义无符号的使用unsigned
# );

# create table t2(
#   f1 float(5,2),   # 保留2位小数 并四舍五入
#   f2 float,
#   f3 double(5,2),
#   f4 double
# )

# insert into t2(f2,f4) values(5.1783682169875975,5.1783682169875975179);

# create table t3(
#   f1 float,   # 保留2位小数 并四舍五入
#   d1 double,
#   d2 decimal(30,20),
#   d3 decimal
# );
# insert into t3 values(5.1783682169875975179,5.1783682169875975179,
#                       5.1783682169875975179,5.1783682169875975179);

数据类型时间

# date  20190620
# time  121953
# datetime 20190620121900

# select now()    # 获取当前时间


# datetime
# year
# date
# time
# timestamp

# create table t4(
#     dt datetime,
#     y year,
#     d date,
#     t time,
#     ts timestamp
# );

# mysql> create table t5(
#     -> id int,
#     -> dt datetime NOT NULL                        # 不能为空
#                   DEFAULT CURRENT_TIMESTAMP        # 默认是当前时间
#                   ON UPDATE CURRENT_TIMESTAMP);    # 在更新的时候使用当前时间更新字段

数据类型字符串

# char
# varchar

# char(18)    最多只能表示255个字符
    # 定长存储,浪费空间,节省时间
    # 'alex'   'alex                 '
# varchar(18) 最多能表示65535个字符
    # 变长存储,节省空间,存取速度慢
    # 'alex'   'alex4'

# 适合使用char
    # 身份证号
    # 手机号码
    # qq号
    # username 12-18
    # password 32
    # 银行卡号
# 适合使用varchar
    # 评论
    # 朋友圈
    # 微博

# create table t6(c1 char(1),v1 varchar(1),c2 char(8),v2 varchar(8));
# create table t6(c1 char,v1 varchar(1),c2 char(8),v2 varchar(8));

数据类型enum和set

# enum 单选
# set 多选

# create table t8(
#     id int,
#     name char(18),
#     gender enum('male','female')
# )

# create table t9(
#     id int,
#     name char(18),
#     hobby set('唱歌','打乒乓球','打羽毛球','旅游','听音乐')
# );
# insert into t9 values (1,'小明','打乒乓球,打羽毛球');
# insert into t9 values (1,'小红','唱歌,唱歌,唱歌,旅游,听音乐'); # 会剔除重复的和不在范围内的

完整性约束

# 约束某一个字段
# 无符号的 int unsigned
# 不能为空 not null
# 默认值  default
# 唯一约束 unique
    # 联合唯一 unique(字段1,字段2)
# 自增 auto_increment
    # 只能对数字有效.自带非空约束
    # 至少是unique的约束之后才能使用auto_increment
# 主键 primary key
    # 一张表只能有一个
    # 如果不指定主键,默认是第一个 非空+唯一
    # 联合主键 primary key(字段1,字段2)
# 外键 Foreign key
    # Foreign key(自己的字段) references 外表(外表字段)
    # 外表字段必须至少是"唯一"的

# create table t10(
#   id int unsigned
# );

# create table t11(
#   id int unsigned not null,
#   name char(18) not null
# );

# create table t12(
#   id int unsigned not null,
#   name char(18) not null,
#   male enum('male','female') not null default 'male's
# );

# 不能重复  unique   值不能重复,但是null可以写入多个
# create table t13(
#   id1 int unique,
#   id2 int
# )

# 联合唯一 unique
# create table t14(
#     id int,
#     server_name char(12),
#     ip char(15),
#     port char(5),
#     unique(ip,port)
# );

# 非空 + 唯一约束
# 第一个被定义为非空+唯一的那一列会成为这张表的primary key
# 一张表只能定义一个主键
# create table t15(
#     id int not null unique,
#     username char(18) not null unique
# );
# create table t16(
#     username char(18) not null unique,
#     id int not null unique
# );
# create table t17(
#     username char(18) not null unique,
#     id int primary key    # 指定主键primary key
# );

# 联合主键
# create table t18(
#     id int,
#     server_name char(12),
#     ip char(15) default '',
#     port char(5) default '',
#     primary key(ip,port)
# );

# create table t19(
#     id int primary key,
#     server_name char(12),
#     ip char(15) not null,
#     port char(5) not null,
#     unique(ip,port)
# );

# 自增
# create table t20(
#     id int primary key auto_increment,
#     name char(12)
# );
# insert into t20(name) values('alex');

# 外键
# 班级表
# create table class(
#     cid int primary key auto_increment,
#     cname char(12) not null,
#     startd date
# )
'''
# 学生表
create table stu(
    id int primary key auto_increment,
    name char(12) not null,
    gender enum('male','female') default 'male',
    class_id int,
    foreign key(class_id) references class(cid)
)
'''

# create table stu2(
#     id int primary key auto_increment,
#     name char(12) not null,
#     gender enum('male','female') default 'male',
#     class_id int,
#     foreign key(class_id) references class(cid)
#     on update cascade
#     on delete cascade  # 尽量不用
# )


表与表之间的关系

# 多对一  foreign key
    # 学生    班级
    # 多个学生是一个班级的
    # 学生表有一个外键 关联班级表

    # 书籍    作者
    # 多本书可以都是一个作者写的
    # 书籍表有一个外键 关联作者表

    # 书籍   出版社
    # 多本书可以同是一个出版社出版的
    # 书籍表有一个外键 关联出版社表

    # 商品  订单
    # 多个商品可以在一个订单中
    # 商品表有一个外键 关联订单表

# 多对多   出现第三张表(两个外键)
    # 学生    班级   多对一
        # 多个学生是一个班级的
    # 班级    学生   多对一
        # 多个班级对应一个学生


    # 一本书可以有多个作者
    # 一个作者可以写多本书

    # 一个订单可以有多个商品
    # 一个商品可以属于多个订单

# 一对一
    # 客户 -  学生
    # unique   foreign key unique

# 存储引擎
# Innodb mysql5.6之后的默认存储引擎
# 2个文件,4个支持(支持事务,行级锁,表级锁,外键)
# Myisam mysql5.5之前的默认存储引擎
# 3个文件 支持表级锁
# Memory
# 1个文件 数据断电消失
# 数据类型
# 数字 : bool int float(7,2)
# 日期 : date time datetime year
# 字符串 :
# char    定长 效率高浪费空间 255
# varchar 变长 效率低节省空间 65535
# enumset :
# 单选和多选
# 约束
# unsigned 无符号的
# not null 非空
# default  设置默认值
# unique   唯一,不能重复
# unique(字段1,字段2,字段3) 联合唯一
# auto_increment 自增
# int 必须至少unique字段,自带not null
# primary key 主键
# not null + unique
# 一张表只能有一个主键
# foreign key 外键
# a表中有一个字段关联b表中的一个unique
# a表中的是外键
# 建表
# create table 表名(
#   字段名1 类型(长度) 约束,
#   字段名1 类型(选项) 约束,
# );
# 修改表结构
# alter table 表名 rename 新名字;
# alter table 表名 add 字段名 类型(长度) 约束 after 某字段;
# alter table 表名 drop 字段名;
# alter table 表名 modify 字段名 类型(长度) 约束 first;
# alter table 表名 change 旧字名 新名字 类型(长度) 约束;
# 表之间的关系
# 一对一
# 一对多
# 多对多

# 删除表
# drop table 表名;

数据的增删改

# create table t1(
#   id int primary key auto_increment,
#   username char(12) not null,
#   sex enum('male','female') default 'male',
#   hobby set('上课','写作业','考试') not null
# );

# 增 insert into (字段,...) values (,...);
# insert into t1 value (1,'小明','male','上课,写作业');
# insert into t1 values(2,'小红','male','写作业,考试');
# insert into t1 values(3,'小绿','male','写作业'),(4,'小白','male','考试');
# insert into t1(username,hobby) values ('小青','上课,写作业,考试'),('小蓝','考试')
# insert into t2(id,name) select id,username from t1;   查询别的表再插入

# 删
# 清空表
    # delete from;
        # 会清空表,但不会清空自增字段的offset(偏移量)值
    # truncate table 表;
        # 会清空表和自增字段的偏移量
# 删除某一条数据
    # delete from 表 where 条件;

# 改
    # update 表 set 字段=值 where 条件;
    # update 表 set 字段=,字段=值 where 条件;

单表数据查询

# 1.select语句
# 最简单的select
    # select * from;
    # select 字段,... from;
# select id,emp_name from employee;


# 重命名字段 # 常用
    # select 字段 as 新名字,... from;
# select id,emp_name as name from employee;     # 给emp_name重命名为name,只是显示出来的名字变了,
    # select 字段 新名字,... from;
# select id,emp_name name from employee;        # 给emp_name重命名为name
# select id i,emp_name name from employee;      # 给id重命名为i,给emp_name重命名为name


# 去重 # 常用
    # select distinct 字段 from;
# select distinct post from employee;   # 去除post中重复的
    # select distinct age,sex from employee;
# select distinct age,sex from employee;    # 以age和sex为标准去重

# 使用函数# 常用
    # concat 拼接函数
# select concat(emp_name,':',salary) from employee; #把emp_name和salary拼接在一起
    # concat_ws
# select concat_ws('|','alex','3714','nihao');  # 把alex 3714 nihao 用|为分割拼接在一起

# 四则运算的 # 不常用
    #  select emp_name,salary*12 from employee; 乘法
    #  select emp_name,salary*12 as annual_salary from employee;    # salary*12重命名为annual_salary

# 使用判断逻辑 # 不常用
    # case when语句 相当于 if条件判断句

# SELECT
#        (
#            CASE
#            WHEN emp_name = 'xiaoming' THEN
#                emp_name
#            WHEN emp_name = 'xiaohong' THEN
#                CONCAT(emp_name,'nihao')
#            ELSE
#                concat(emp_name, 'hello')
#            END
#        ) as new_name
#    FROM
#        employee;




# where 筛选所有符合条件的行
    # 比较运算符
        # > < >= <= <>不等于 !=不等于
    # 范围
        # between 10000 and 200001w-2w之间的
        # in (10000,2000030000)   只要10000或者20000或者30000的
        #not in
    # 模糊匹配
        # like
            # % 通配符 表示任意长度的任意内容
# select * from employee where emp_name like 'j%';  #以j开头
# select * from employee where emp_name like '%j';  #以j结尾
# select * from employee where emp_name like '%j%';  #含有j
            # _ 通配符 一个字符长度的任意内容
# select * from employee where emp_name like '成_';  #一个_代表一个字符
        # regexp
            # '^a'
# select * from employee where emp_name regexp '^j';    #以j开头
            # 'g$'  以g结尾
    # 逻辑运算
        # not\and\or

# 查看岗位描述不为NULL的员工信息
    # is
    # select * from employee where post_comment is not null;
# 查看岗位是teacher且薪资不是10000900030000的员工姓名、年龄、薪资
    # select emp_name, age, salary
    # from employee wherepost = 'teacher' and salary not in(10000,9000,30000)
# 查看岗位是teacher且名字是jin开头的员工姓名、年薪
    #  select emp_name,salary*12 from employee where post = 'teacher' and emp_name like 'jin%';

# 分组 group by 根据谁分组,可以求这个组的总人数,最大值,最小值,平均值,求和 但是这个求出来的值只是和分组字段对应
    # 并不和其他任何字段对应,这个时候查出来的所有其他字段都不生效.
# 聚合函数
    # count 求个数
# select sex,count(id) from employee group by sex;  #以sex分组查看男女的人数
# select age,count(id) from employee group by age;  #查看每一个年龄的人数
    # max  求最大值
# select post,max(salary) from employee group by post;  #以post分组查看每个post中最大的salary
    # min  求最小值
# select post,min(salary) from employee group by post;  #以post分组查看每个post中最小的salary
    # sum  求和
# select post,sum(salary) from employee group by post;  #以post分组查看每个post中salary的和
    # avg  求平均
# select post,avg(salary) from employee group by post;  #以post分组查看每个post的平均salary

    # SELECT post,emp_name FROM employee GROUP BY post;
    # SELECT post,GROUP_CONCAT(emp_name) FROM employee GROUP BY post;

# having 过滤语句
# select post,avg(salary) from employee group by post having avg(salary)>10000;
# 筛选每个post中平均salary大于10000的post
    # 在having条件中可以使用聚合函数,在where中不行
    # 适合去筛选符合条件的某一组数据,而不是某一行数据
    # 先分组再过滤 : 求平均薪资大于xx的部门,求人数大于xx的性别,求大于xx人的年龄段
# 查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数
# group by post having count(id) < 2;

# 排序 order by
# select * from employee order by salary;   # 按照salary从小到大排序
# select * from employee order by salary desc;  # 从大到小
    # 默认是升序  asc
    # 降序  desc
    # order by age ,salary desc
        # 优先根据age从小到大排,在age相同的情况下,再根据薪资从大到小排

# limit m,n
# select * from employee order by salary desc limit 1;  # # 按照salary从大到小排序,取第一个
    # 从m+1项开始,取n项
# select * from employee order by salary desc limit 3;  # 取前3
# select * from employee order by salary desc limit 2,1;    #取第3个  从2+1开始取1项
    # 如果不写m,m默认为0

    # limit n offset m      #从m+1开始取n项,和limit m,n一样

连表查询

# 所谓连表
    # 总是在连接的时候创建一张大表,里面存放的是两张表的笛卡尔积
    # 再根据条件进行筛选就可以了

# 表与表之间的连接方式
    # 内连接 inner join ... on ...
        # select * from1,2 where 条件;(了解)
        # select * from1 inner join 表2  on 条件
        # select * from department inner join employee on department.id = employee.dep_id;
        # select * from department as t1 inner join employee as t2 on t1.id = t2.dep_id;    #后面可以用where条件筛选
    # 外连接
        # 左外连接 left join ... on ...
            # select * from1 left join 表2 on 条件
            # select * from department as t1 left join employee as t2 on t1.id = t2.dep_id;
        # 右外连接 right join ... on ...
            # select * from1 right join 表2 on 条件
            # select * from department as t1 right join employee as t2 on t1.id = t2.dep_id
        # 全外连接 full join
        #     select * from department as t1 left join employee as t2 on t1.id = t2.dep_id
        #     union
        #     select * from department as t1 right join employee as t2 on t1.id = t2.dep_id;

# 1.找到技术部的所有人的姓名
# select * from department d inner join employee e on e.dep_id = d.id;
# select e.name from department d inner join employee e on e.dep_id = d.id where d.name='技术';

# 2.找到人力资源部的年龄大于40岁的人的姓名
# select * from department d inner join employee e on e.dep_id = d.id
# select * from department d inner join employee e on e.dep_id = d.id where d.name='人力资源' and age>40;

# 3.找出年龄大于25岁的员工以及员工所在的部门
# select * from department d inner join employee e on e.dep_id = d.id;
# select e.name,d.name from department d inner join employee e on e.dep_id = d.id where age>25;

# 4.以内连接的方式查询employee和department表,并且以age字段的升序方式显示
# select * from department d inner join employee e on e.dep_id = d.id order by age;

# 5.求每一个部门有多少人
# select d.name,count(e.id) from department d left join employee e on e.dep_id = d.id group by d.name;
# 且按照人数从高到低排序
# select d.name,count(e.id) c from department d left join employee e on e.dep_id = d.id group by d.name order by c desc;

# 所谓连表就是把两张表连接在一起之后 就变成一张大表  从from开始一直到on条件结束就看做一张表
# 之后 where 条件 group by 分组 order by limit 都正常的使用就可以了


子查询

# 查询平均年龄在25岁以上的部门名
# select name from department where id in (
#     select dep_id from employee group by dep_id having avg(age)>25);

# 查看技术部员工姓名
    # 先查询技术部的部门id
    # select id from department where name = '技术';
    # 再根据这个部门id找到对应的员工名
    # select name from employee where dep_id =(select id from department where name = '技术');
    # select name from employee where dep_id in (select id from department where name = '技术');

# 查看不足1人的部门名
    # 先把所有人的部门id查出来
    # select distinct dep_id from employee;
    # 然后查询部门表,把不在所有人部门id这个范围的dep_id找出来
    # select name from department where id not in (select distinct dep_id from employee);

# 查询大于所有人平均年龄的员工名与年龄
    # 求平均年龄
    # select avg(age) from employee;
    # select * from employee where age >28;
    # select name,age from employee where age >(select avg(age) from employee);

# 查询大于部门内平均年龄的员工名、年龄
    # select dep_id,avg(age) from employee group by dep_id;
    # select name,age from employee as t1 inner join (select dep_id,avg(age) avg_age from employee group by dep_id) as t2
    # on t1.dep_id = t2.dep_id where age>avg_age;




正确使用索引

# 数据库使用的时候有什么注意事项
    # 从搭建数据库的角度上来描述问题
    # 建表的角度上
        # 1.合理安排表关系
        # 2.尽量把固定长度的字段放在前面
        # 3.尽量使用char代替varchar
        # 4.分表: 水平分,垂直分
    # 使用sql语句的时候
        # 1.尽量用where来约束数据范围到一个比较小的程度,比如说分页的时候
        # 2.尽量使用连表查询而不是子查询
        # 3.删除数据或者修改数据的时候尽量要用主键作为条件
        # 4.合理的创建和使用索引
            # 1.查询的条件字段不是索引字段
                # 对哪一个字段创建了索引,就用这个字段做条件查询
            # 2.在创建索引的时候应该对区分度比较大的列进行创建
                # 1/10以下的重复率比较适合创建索引
            # 3.范围
                # 范围越大越慢
                # 范围越小越快
                # like 'a%'  快
                # like '%a'  慢
            # 4.条件列参与计算/使用函数
            # 5.and和or
                # id name
                # select * from s1 where id = 1800000 and name = 'eva';
                # select count(*) from s1 where id = 1800000 or name = 'eva';
                # 多个条件的组合,如果使用and连接
                    # 其中一列含有索引,都可以加快查找速度
                # 如果使用or连接
                    # 必须所有的列都含有索引,才能加快查找速度
            # 6.联合索引 : 最左前缀原则(必须带着最左边的列做条件,从出现范围开始整条索引失效)
                # (id,name,email)
                # select * from s1 where id = 1800000 and name = 'eva' and email = 'eva1800000@oldboy';
                # select * from s1 where id = 1800000 and name = 'eva';
                # select * from s1 where id = 1800000 and email = 'eva1800000@oldboy';
                # select * from s1 where id = 1800000;
                # select * from s1 where name = 'eva' and email = 'eva1800000@oldboy';
                # (email,id,name)
                # select * from s1 where id >10000 and email = 'eva1800000@oldboy';
            # 7.条件中写出来的数据类型必须和定义的数据类型一致
                # select * from biao where name = 666   # 不一致
            # 8.select的字段应该包含order by的字段
                # select name,age from 表 order by age;  # 比较好
                # select name from 表 order by age;  # 比较差





# select * from 表 where 条件 group by 分组 having 聚合;
# 300万条数据
# 分页
# page = 1
# num_per = 10
# tmp = (page-1)*num_per = 1-1=0*10 = 0
# select * from 表 where id between tmp and tmp+num_per
# page +=1 = 2
# tmp = (page-1)*num_per = 10
# select * from 表 where id between 10 and 20
#
# select * from 表 limit 10,10
# select * from 表 limit 20,10
#
# select * from 表 limit 2999990,10



索引合并与覆盖索引

# 联合索引
# (id,email)
# id = 100 and email like 'eva%';

# 索引合并 :分开创建在查询过程中临时合并成一条 Using union(ind_id,ind_email)
    # 创建索引的时候
    # create index ind_id on s1(id)
    # create index ind_email on s1(email)
    # select * from s1 where id=100 or email = 'eva100@oldboy'
    # 临时把两个索引ind_id和ind_email合并成一个索引


# 覆盖索引:在查询过程中不需要回表   Using index
    # 对id字段创建了索引
    # select id from s1 where id =100     覆盖索引:在查找一条数据的时候,命中索引,不需要再回表
    # select count(id) from s1 where id =100     覆盖索引:在查找一条数据的时候,命中索引,不需要再回表
    # select max(id) from s1 where id =100     覆盖索引:在查找一条数据的时候,命中索引,不需要再回表
    # select name from s1 where id =100   相对慢

# 什么是mysql的执行计划?用过explain么?
    # 在执行sql语句之前,mysql进行的一个优化sql语句执行效率的分析(计划),可以看到有哪些索引,实际用到了那个索引,执行的type等级
    # id name email
    # select * from s1 where id = 1000000 and name=eva and email = 'eva1000000@oldboy';
        # 有没有索引
        # 有几个
        # 用哪一个索引比较效率高
    # explain select * from s1 where id = 1000000 and name=eva and email = 'eva1000000@oldboy';



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值