目录
- 前置操作
- CRUD的基础操作
前置操作
1.创建数据库:
create database 数据库名
2.创建表:
create table 表名(
字段1 字段类型,
字段2 字段类型,
......
字段n 字段类型);
--例如
create table student(
id int,
name varchar(20),
score int);
3.查看表结构
desc 表名;
show create table 表名;
4. 查看有哪些数据库、有哪些表,有哪些警告以及选中数据库
-- 查看有哪些数据库
show databases;
-- 查看某个库里面有哪些表
show tables;
-- 查看警告
show warnings;
-- 选中数据库
use 数据库名; //在对表进行一系列操作时,要先选中数据库
CRUD的基础操作
1.插入(create)
1.1单行数据+全列插入:
insert into student values (1,'张三',99);
1.2单行数据+指定列插入:
insert into student (id,name) values (1,'张三');
1.3多行数据指定列插入:
insert into student (id,name,score) values (1,'张三',99),(2,'李四',88),(3,'王五',77);
2.查询(Retrieve)
2.1全列查询:
select * from student; -- 通常情况下不建议使用,风险很大
2.2 指定列查询:
select id,score,name from student; -- 列名的顺序不需要按照表的列名顺序来
2.3 表达式查询,以这张表举例:
假如我要查询每个人的总成绩,并且只显示姓名和总分两列:
select name,chinese+math+english from exam_score;
查询结果:
2.4 别名(as)
以上查询结果的表看起来太丑陋了,于是我们可以给 列 起别名:
select name,chinese+math+english as total from exam_score;
此时查询结果看起来就紧凑一些了:
当然,我们也可以给表指定别名,下期博客会具体讲到。
2.5 去重查询(distinct)
select distinct name from exam_score; -- 指定列名去重
select distinct * from exam_score; -- 每列都相同的数据的去重
2.6 排序(order by)
按总成绩升序排序:
select name,chinese+math+english as total from exam_score order by total asc;
-- asc 可以省略不写,,默认为升序排序
按总和成绩降序排序:
select name,chinese+math+english as total from exam_score order by total desc;
-- desc 不能省略
升序(asc)结果: 降序(desc)结果:
注意:
1.没有order by子句的查询,返回的顺序是未定义的。
2.null数据的排序,升序永远在最上面,降序永远在最下面。
3.order by 可以和前面的表达式,别名,指定列搭配使用。
2.7 条件查询(where)
比较运算符:
运算符
|
说明
|
>,>=,<,<= |
大于,大于等于,小于,小于等于
|
= |
等于,
NULL
不安全,例如
NULL = NULL
的结果是
NULL
|
<=> |
等于,
NULL
安全,例如
NULL <=> NULL
的结果是
TRUE(1)
|
!=,<> |
不等于
|
between a0 and a1 |
范围匹配,
[a0, a1]
,如果
a0 <= value <= a1
,返回
TRUE(1)
|
in (option...) |
如果是
option
中的任意一个,返回
TRUE(1)
|
is null |
是
NULL
|
is not null |
不是
NULL
|
like |
模糊匹配。
%
表示任意多个(包括
0
个)任意字符;
_
表示任意一个字
符
|
逻辑运算符:
运算符
| 说明 |
and | 多个条件必须都为 TRUE(1),结果才是 TRUE(1) |
or | 任意一个条件为 TRUE(1), 结果为 TRUE(1) |
not | 条件为 TRUE(1),结果为 FALSE(0) |
注意:
1.where条件可以使用表达式,但不能使用别名。
2.and的优先级高于or,在同时使用的时候,需要给先执行的部分带上小括号().
where条件的使用:
- 基本查询:
-- 查询英语不及格的同学及英语成绩 ( < 60 )
select name, english from exam_score where english < 60;
-- 查询语文成绩好于英语成绩的同学
select name, chinese, english from exam_score where chinese > english;
-- 查询总分在 200 分以下的同学
select name, chinese + math + english 总分 from exam_score
where chinese + math + english < 200;
- and 和 or
-- 查询语文成绩大于80分,且英语成绩大于80分的同学
select * from exam_score where chinese > 80 and english > 80;
-- 查询语文成绩大于80分,或英语成绩大于80分的同学
select * from exam_score where chinese > 80 or english > 80;
-- 观察and 和 or 的优先级:
select * from exam_score where chinese > 80 or math>70 and english > 70;
select * from exam_score where (chinese > 80 or math>70) and english > 70;
- 范围查询
//1.between and --> 用and也可以实现
-- 查询语文成绩在 [80, 90] 分的同学及语文成绩
select name, chinese from exam_score where chinese between 80 and 90;
//2.in --> 用or也可以实现
-- 查询数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
select name, math from exam_score where math in (58, 59, 98, 99);
- 模糊查询
% 匹配 任意多个 (包括 0 个)字符
select name from exam_score where name like '孙%'; -- 匹配到孙悟空、孙权
select name from exam_score where name like '%孙%'; -- 匹配到包含孙的同学
_ 匹配严格的 一个任意 字符
select name from exam_score where name like '孙_'; -- 匹配到孙权
select name from exam_score where name like '孙__'; -- 匹配到孙悟空
- null查询
--查询数学成绩为null的同学
select name,math <=> null from exam_score; //写法一
select name,math is null from exam_score; //写法二
--查询数学成绩不为null的同学
select name,math != null from exam_score; //写法一
select name,math is not null from exam_score; //写法二
2.8 分页查询(limit)
-- 查询数学成绩第二的同学的学号、姓名
select id,name from exam_score order by math desc limit 1 offset 1; //从第一页后面的一条记录
--offset也可以省略
select id,name from exam_score order by math desc limit 1,1;
-- 查询数学成绩前三的同学
select id,name from exam_score order by math desc limit 3; //选择从第一条记录开始的 3 条记录
分两步理解:
limit 和 offset 可以搭配使用,例如:limit 1 offset 2 意思就是选择第2条记录后面的1条记录,可以把每一条记录理解成一页。也就是从第2页之后的那1页。
3.修改(update)
基本用法:
update exam_score set name = '周瑜' where id = 2;
与条件查询,分页查询,排序搭配使用的案例:
-- 将孙悟空同学的数学成绩变更为 80 分
update exam_score set math = 80 where name = '孙悟空';
-- 将曹孟德同学的数学成绩变更为 60 分,语文成绩变更为 70 分
update exam_score set math = 60, chinese = 70 where name = '曹孟德';
-- 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
update exam_score set math = math + 30 order by chinese + math + english limit 3;
-- 将所有同学的语文成绩更新为原来的 2 倍
update exam_score set chinese = chinese * 2;
4.删除(delete)
基本用法:
-- 删除学号为 2 的同学
delete from exam_score where id = 2;
也可以和前面的各种查询搭配使用,删除这一块比较简单,就不赘述了!
4.1 删除表
drop table 表名;
4.2 删除数据库
drop database 数据库名;
注意:以上的增杀查改操作,很多操作都存在着一定都风险,当数据库很重要,或者表很大的时候,我们在进行增删查改的时候,一定要慎重,一定要注意条件的搭配使用,否则可能会导致大量数据吃满磁盘IO,就可能导致服务器挂了!!
本期讲解就到这里了,谢谢观看!!