MySQL学习day2
目录
2、where条件子句(配合查询、修改、删除操作)
1、查询
3、表记录管理
1、删除表记录
2、更新表记录
4、运算符操作
1、数值比较 & 运算符
1、数值比较运算符:
2、字符比较运算:
2、练习
2、逻辑比较
1、运算符
2、练习:
3、范围内比较(between and、 in、 not in)
1、运算符
2、语法
3、练习
4、匹配空、非空(is null、is not null)
1、运算符
2、示例
4、注意
5、模糊比较
6、正则匹配查询 regexp
5、SQL查询
1、总结(执行顺序)[序号表示执行顺序]
2、order by ...
1、作用
2、语法格式
3、排序方式
4、示例
3、limit(永远放在SQL语句最后写)
1、作用
2、用法:
3、示例
4、聚合函数
1、分类
2、示例
5、group by(先分组再聚合)
1、作用
2、示例
3、注意
6、having
1、作用
2、示例
3、注意
7、distinct
1、作用
2、示例
3、注意
8、查询表记录时可以做数学运算
1、运算符
2、示例
6、约束
1、作用:
2、约束分类
1、默认约束
3、非空约束
1、作用
2、格式
3、示例
7、索引
1、定义
2、索引的优点
3、索引的缺点
4、索引示例
2、where条件子句(配合查询、修改、删除操作)
1、查询
select * from 表名 where 条件;
3、表记录管理
1、删除表记录
delete * from 表名 where 条件;
例如:
| name | age | score |
|---|---|---|
| k | 25 | 100 |
| s | 21 | 90 |
| z | 23 | 89 |
| f | 23 | 100 |
delect * from where score=100; # 删除条件为score=100的字段
| name | age | score |
|---|---|---|
| s | 21 | 90 |
| z | 23 | 89 |
2、更新表记录
updata 表名 set 字段名1=值1,字段名2=值2,...,where 条件; 例如: +------+------+-------+ | name | age | score | +------+------+-------+ | s | 21 | 90 | | z | 23 | 89 | +------+------+-------+update t1 set score=100 where name='s'; # 将name="s"的score改为100 +------+------+-------+ | name | age | score | +------+------+-------+ | s | 21 | 100 | | z | 23 | 89 | +------+------+-------+ 注意:update语句后如果不加where条件子句会将表中所有的记录全部更改
4、运算符操作
1、数值比较 & 运算符
1、数值比较运算符:
=、 !=、 >、>=、<、<=
2、字符比较运算:
=、!=
2、练习
create table sanguo(id int unsigned,name char(20),gongji int unsigned,fangyu int unsigned,sex char(10),country char(20));insert into sanguo values(1,"诸葛亮",120,20,"男","蜀国"),(2,"司马懿",119,25,"男","蜀国"),(3,"关6羽",188,60,"男","蜀国"),(4,"赵云666",200,66,"男","蜀国"),(5,"0孙权",110,20,"男","吴国"),(6,"貂蝉",666,10,"女","魏国"),(7,1000,99,"男","蜀国"),(8,"",1005,88,"女","蜀国");+------+-----------+--------+--------+------+---------+| id | name | gongji | fangyu | sex | country |+------+-----------+--------+--------+------+---------+| 1 | 诸葛亮 | 120 | 20 | 男 | 蜀国 || 2 | 司马懿 | 119 | 25 | 男 | 蜀国 || 3 | 关6羽 | 188 | 60 | 男 | 蜀国 || 4 | 赵云666 | 200 | 66 | 男 | 蜀国 || 5 | 0孙权 | 110 | 20 | 男 | 吴国 || 6 | 貂蝉 | 666 | 10 | 女 | 魏国 || 7 | NULL | 1000 | 99 | 男 | 蜀国 || 8 | | 1005 | 88 | 女 | 蜀国 |+------+-----------+--------+--------+------+---------+
1、找出攻击力高于150的英雄的名字和攻击力值
select name,gongji from sanguo where gongji>150; +-----------+--------+ | name | gongji | +-----------+--------+ | 关6羽 | 188 | | 赵云666 | 200 | | 貂蝉 | 666 | | NULL | 1000 | | | 1005 | +-----------+--------+
2、找出防御力不等于100的英雄信息
select * from sanguo where fangyu!=100;
2、逻辑比较
1、运算符
and(两个或多个条件同时满足) or(两个或多个条件有一满足即可)
2、练习:
1、找出攻击值大于200的蜀国英雄的名字和攻击值select name,gongji from sanguo where gongji>200 and country="蜀国";+------+--------+| name | gongji |+------+--------+| NULL | 1000 || | 1005 |+------+--------+ 2、查找蜀国和魏国的英雄信息select * from sanguo where country="蜀国" or country="魏国";+------+-----------+--------+--------+------+---------+| id | name | gongji | fangyu | sex | country |+------+-----------+--------+--------+------+---------+| 1 | 诸葛亮 | 120 | 20 | 男 | 蜀国 || 2 | 司马懿 | 119 | 25 | 男 | 蜀国 || 3 | 关6羽 | 188 | 60 | 男 | 蜀国 || 4 | 赵云666 | 200 | 66 | 男 | 蜀国 || 6 | 貂蝉 | 666 | 10 | 女 | 魏国 || 7 | NULL | 1000 | 99 | 男 | 蜀国 || 8 | | 1005 | 88 | 女 | 蜀国 |+------+-----------+--------+--------+------+---------+ 3、将吴国英雄中攻击力为110的英雄攻击值设为100,防御力设为60update sanguo set gongji=100,fangyu=60 where gongji=110 and country="吴国";select * from sanguo where country="吴国";+------+-----------+--------+--------+------+---------+| id | name | gongji | fangyu | sex | country |+------+-----------+--------+--------+------+---------+| 5 | 0孙权 | 100 | 60 | 男 | 吴国 |+------+-----------+--------+--------+------+---------+
3、范围内比较(between and、 in、 not in)
1、运算符
between and、 in、 not in
2、语法
between 值1 and 值2 in(值1, 值2, ..., 值N) not in(值1, 值2, ..., 值N)
3、练习
1、查找攻击值在100-200之间的蜀国英雄信息select * from sanguo where gongji between 100 and 200 and country="蜀国";+------+-----------+--------+--------+------+---------+| id | name | gongji | fangyu | sex | country |+------+-----------+--------+--------+------+---------+| 1 | 诸葛亮 | 120 | 20 | 男 | 蜀国 || 3 | 关6羽 | 188 | 60 | 男 | 蜀国 || 4 | 赵云666 | 200 | 66 | 男 | 蜀国 |+------+-----------+--------+--------+------+---------+
2、找到蜀国和吴国以外国家的女英雄信息select * from sanguo where country not in ("吴国", "蜀国") and sex="女";+------+--------+--------+--------+------+---------+| id | name | gongji | fangyu | sex | country |+------+--------+--------+--------+------+---------+| 6 | 貂蝉 | 666 | 10 | 女 | 魏国 |+------+--------+--------+--------+------+---------+
3、找到编号为1 3 或 5 的蜀国英雄和貂蝉的编号、姓名和国家select id,name,country from sanguo where id in(1, 3, 5) and country="蜀国" or name="貂蝉";+------+-----------+---------+| id | name | country |+------+-----------+---------+| 1 | 诸葛亮 | 蜀国 || 3 | 关6羽 | 蜀国 || 6 | 貂蝉 | 魏国 |+------+-----------+---------+
4、匹配空、非空(is null、is not null)
1、运算符
1、空:is null 2、非空:is not null
2、示例
1、查找姓名为NULL的蜀国女英雄信息 select * from sanguo where name is NULL and sex="女" and country="蜀国"; 2、查找姓名为“”的英雄的id、姓名和国家 select id,name,country from sanguo where name="";
4、注意
1、null:空值,必须用is 或 is not 去匹配 2、"":空字符串,用 = 或者 != 去匹配
5、模糊比较
1、where 字段名 like 表达式; 2、表达式1、_:匹配的是单个字符2、%:匹配0到多个字符3、示例1、select name from sanguo where name like "_%_"; # 匹配名字里至少两个字符的2、select name from sanguo where name like "%";#匹配0到多个字符,null不会被统计3、select name from sanguo where name like"___"; #匹配三个字符4、select name from sanguo where name like"赵%"; # 匹配姓赵的英雄
6、正则匹配查询 regexp
1、where 字段名 regex "正则表达式"; 2、正则表达式符号^ :以...开头$ :以...结尾. :匹配任意一个字符[]:包含...内容[0-9]:匹配带数字的[a-z]:匹配小写字母的[A-Z]:匹配大写字母的×:星号前面的字符出现0次或多次 示例:1、select name from sanguo where name regexp"[0-9]"; # 匹配名字中带数字的2、select name from sanguo where name regexp"^[0-9]"; # 以0-9数字开头的3、select name from sanguo where name regexp"[0-9]$"; # 以0-9数字结尾的4、select name from sanguo where name regexp"^司.*懿$"; # ^司:以司开头,.*:中间可以是任意值,懿$:懿结尾。5、select name from sanguo where name regexp"^...$";
5、SQL查询
1、总结(执行顺序)[序号表示执行顺序]
3. select ... 聚合函数 from 表名 1. where... 2. grop by... 4. having... 5. order by... 6. limit...
2、order by ...
1、作用
给查询的结果进行排序
2、语法格式
order by 字段名 排序方式;
3、排序方式
1、ASC(默认):升序 2、DESC:降序
4、示例
1、将英雄按防御值从低到高排序:select * from sanguo order by fangyu asc;
2、将蜀国英雄按攻击值从高到底排序:select * from sanguo where country in ("蜀国") order by gongji asc;
3、将魏蜀两国中名字为三个字的男英雄按防御值升序排序
select * from sanguo where country in ("魏国","蜀国") and sex="男" and name like"___" order by fangyu asc;
3、limit(永远放在SQL语句最后写)
1、作用
限制显示查询记录的个数
2、用法:
1、limit n: -->显示 n 条记录 2、limit m,n: -->从第(m+1)条开始,显示n条记录limit 4,5:显示第5-第9名## m的值是从0开始计数的
3、示例
1、查找防御值倒数第2名到倒数第四名蜀国英雄记录
select * from sanguo where country in("蜀国") order by fangyu ASC limit 1,3;
2、查找攻击值前三名且名字不为空的蜀国英雄的姓名,攻击值和国家
select name,gongji,country from sanguo where country="蜀国" and name is not null order by gongji asc limit 3;
4、聚合函数
1、分类
1、avg(字段名):求字段的平均值 2、sum(字段名):求和 3、max(字段名):求最大值 4、min(字段名):求最小值 5、count(字段名):统计该字段记录的个数
2、示例
1、攻击力最强值:select max(gongji) from sanguo; 2、统计id,name两个记录分别有多少条记录select count(id),count(name) from sanguo; 3、统计蜀国英雄蜀国英雄中攻击值大于200的英雄数量select count(*) from sanguo where gongji>200 and country="蜀国";
5、group by(先分组再聚合)
1、作用
给查询的结果进行分组
2、示例
1、统计sanguo表中共有的国家:select country from sanguo group by country; 2、分别计算所有国家平均攻击率select country,avg(gongji) from sanguo group by country;执行过程,先按照国家来分组,后执行每一组的平均攻击值,最后去重 3、查找所有国家中英雄数量为前两名的国家名称及英雄数量select country,count(name) from sanguo group by country order by count(name) DESC limit 0,2;
3、注意
1、group by之后的字段名必须要是select之后的字段名 2、如果色了传统之后的字段没有在group by语句之后,必须对该字段进行聚合处理
6、having
1、作用
对查询的结果进行进一步筛选
2、示例
1、找出平均攻击力大于150的国家的前2名,显示国家的名称和平均攻击力select country,avg(gongji) as pjgj from sanguo group by country having pjgj>105 order by pjgj desc limit 2;
3、注意
1、having通常与group by语句来用,用来过滤由group up语句返回的记录集 2、having语句的存在弥补了where条件子句不能与聚合函数联合使用的不足,where操作是表中实际存在的字段,having操作的是聚合函数生成的显示列
7、distinct
1、作用
不显示字段的重复值
2、示例
1、sanguo表中一共有哪些国家select distinct country from sanguo; 2、计算蜀国一共有多少个英雄select count(distinct name) from sanguo where country="蜀国";
3、注意
1、distinct处理的是distinct和from之间的所有字段,所有字段值必须完全相同才能去重 2、distinct不拿呢个对任何字段做聚合处理
8、查询表记录时可以做数学运算
1、运算符
+ - × / %
2、示例
1、查询时显示所有英雄的攻击力 × 10select name,gongji*10,country from sanguo;
6、约束
1、作用:
保证数据的完整性、一致性、有效性,可以限制无效的数据插入到数据列表中
2、约束分类
1、默认约束
1、作用:插入纪录时,如果不给该字段赋值,则使用默认值 2、格式字段名 数据类型 default 默认值,
3、非空约束
1、作用
不允许该字段的值有空值NULL记录
2、格式
字段名 数据类型 not null,
3、示例
create table day(id int not null,name varchar(15) not null,sex enum("M","F","secret") default "secret");+-------+------------------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+------------------------+------+-----+---------+-------+| id | int(11) | NO | | NULL | || name | varchar(15) | NO | | NULL | || sex | enum('M','F','secret') | YES | | secret | |+-------+------------------------+------+-----+---------+-------+
7、索引
1、定义
对数据苦中表的一列或者多列的值进行排序的一种结构(MySQL中索引用Btree方式)
2、索引的优点
加快索引的检索速度
3、索引的缺点
1、当对表中的数据进行增加、修改、删除的时候,索引需要动态维护,降低了数据的维护速度 2、索引需要占用物理存储空间(数据库目录/var/lib/mysql)
4、索引示例
1、开启索引分析:profiling = 1; 2、执行查询语句:select name from t1 where name="xxxx"; 3、查看性能分析:show profiles; 4、在name字段创建索引create index name on t1(name); 5、执行查新语句select name from t1 where name="xxxx"; 6、查看性能分析结果:show profiles; 7、关闭性能分析:set profiling = 0;
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
