目录
任务3:数据的更新
任务4:数据的删除
📱任务1:数据的插入
准备工作:
创建一个数据库:
create database c default character set utf8;
指定要使用的数据库:
use c;
建表:
create table student(
sid int primary key,
sname varchar(20) not null,
grade int
);
往student表插入数据:语法格式1:一次插入一条数据(中括号表示可选项)
insert into 表名[(字段1,字段2,……字段n)] values(值1,值2,……值n);
【示例1】
insert into student(grade,sname,sid) values(3,'lily',1);
【示例2】
insert into student values(2,'lucy',5);
【示例3】
insert into student(sid,sname) values(3,'Jack');
【示例4】
insert into student(sid,sname,grade) values(4,'Jim',null);
语法格式2:一次插入多条数据(中括号表示可选项)
insert into 表名[(字段1,字段2,……字段n)]
values(值1,值2,……值n),
(值1,值2,……值n),
……
(值1,值2,……值n);
【示例5】
insert into student(sid,sname,grade)
values(5,'xiaobai',4),
(6,'xiaohei',2);
SQL文件的导入和导出:
SQL文件的导出步骤:双击你要导出的数据库将其激活——》右击数据库,选择“转储SQL文件”——》
选择“结构和数据”
SQL文件的导入步骤:双击你要导入的数据库将其激活——》右击数据库,选择“运行SQL文件”——》
选择你要导入的SQL文件运行。
📱任务2:数据的查询
准备工作:导入已经准备好的student.sql文件,会导入如下三张表:
xsb 学生表
kcb 课程表
cjb 成绩表
2.1 查询所有的字段
select * from 表名;
select 字段1,字段2,……字段n from 表名;
【示例6】
select * from xsb;
2.2 查找特定的字段
【示例7】
select xm,nl from xsb;
【示例8】
select bj from xsb;
select distinct bj from xsb; -- 使用distinct过滤重复的记录
【示例9】
select distinct xm,bj from xsb; -- 只有当姓名和班级同时取值相同的时候才会过滤重复值
2.3 条件查询
select *|字段1,字段2,……字段n
from 表名
where 查询条件;
注解1:
from子句的作用是明确数据的来源;
select子句的作用是过滤出满足需求的列(字段);
where子句的作用是过滤出满足需求的行(记录);
注解2:
where条件里经常用到的关系运算符:= != > >= < <=
条件查询还支持多条件,多条件有三个常用的逻辑运算符:
and 与 并且的意思
or 或 或者的意思
not 非 取反的意思
between and 在……之间
in 在一个集合里任选其一
not between and
not in
【示例10】
查询张三学员的性别和籍贯
select xb,jg
from xsb
where xm='张三';
【示例11】
查询年龄大于20岁的学员信息
select *
from xsb
where nl>20;
【示例12】
查询年龄在19-21岁之间的学员信息(包含边界)
select *
from xsb
where nl>=19 and nl<=21;
以下写法不对,因为查询结果不满足要求:
select *
from xsb
where 19<=nl<=21;
还可以使用between and来写:
select * from xsb where nl between 19 and 21;
注意:使用between and时,between后面跟的是较小的值,and后面跟的是较大的值,且这种用法包含边界。
以下写法查询不出满足条件的记录:
select * from xsb where nl between 21 and 19;
还可以写成如下形式:
select * from xsb where nl=19 or nl=20 or nl=21;
select * from xsb where nl in(19,20,21);
【示例13】
查询张三和李四的学员信息
select * from xsb where xm='张三' or xm='李四';
select * from xsb where xm in('张三','李四');
【示例14】
查询除了张三和李四以外的学员信息
select * from xsb where xm not in('张三','李四');
select * from xsb where not (xm='张三' or xm='李四');
2.4 模糊查询
条件查询支持模糊查询,模糊查询使用like关键字。
常用的两个通配符:
% 表示此处有0个或1个或多个字符

_ 标识此处有1个字符
【示例15】
查询姓张的学员信息
select * from xsb where xm like '张%';
【示例16】
查询姓张的学员信息(该学员名字就2个字)
select * from xsb where xm like '张_';
【示例17】
查询名字里包含“飞”的学员信息
select * from xsb where xm like '%飞%';
2.5 空值查询
条件查询还支持空值查询,空值查询用is连接,而不是=:
is null 是空值
is not null 不是空值
【示例18】
查询班级为空值的学员信息
select * from student where grade is null;
不能写成以下这种,因为查询不到相关的结果:
select * from student where grade=null;
2.6 排序显示
语法:
select *|字段1,字段2,……字段n
from 表名
where 查询条件
order by 字段 [asc|desc];
注解:
asc:ascend 升序
desc:descend 降序
【示例19】
查询学员信息,按年龄从大到小排序
select * from xsb order by nl desc;
【示例20】
按年龄从小到大排序
select * from xsb order by nl asc;
select * from xsb order by nl; -- 默认升序
【示例21】
查询学员信息,按年龄从大到小排序,如果年龄相同,则按学号从大到小排序
select * from xsb order by nl desc,xh desc;
2.7 聚合函数
聚合函数一般用于分组统计:
常用的聚合函数有:
count() 统计个数
avg() 求平均值
sum() 求和
max() 求最大值
min() 求最小值
【示例22】
查询学生表里有多少个学员
select count(*) 学员个数 from xsb;
select count(xh) 学员个数 from xsb;
【示例23】
select count(*),count(sid),count(sname),count(grade),count(sbirth)
from student;
注意:count(*)和count(字段)在字段不取空值的情况下二者等效,否则不等效。
【示例24】
查询001号学员的最高分、最低分、平均分和总分
select max(cj),min(cj),round(avg(cj),1),sum(cj)
from cjb
where xh='001';
2.8 分组查询
语法格式:
select *|字段1,字段2,……字段n
from 表名
where 查询条件
group by 分组字段
having 过滤条件
order by 字段 [asc|desc];
注解:
group by 子句:按某个字段做分组
having子句:对分组之后的数据做进一步过滤,针对组的过滤
where子句:对分组之前的数据做过滤,针对行的过滤,聚合函数不能出现在where子句里
【示例25】
统计各个班级的人数
select bj,count(*)
from xsb
group by bj;
【示例26】
统计班级人数在2个人以上的分组
select bj,count(*)
from xsb
group by bj
having count(*)>2;
📱任务3:数据的更新
语法:
update 表名 set 修改的内容 [where 更新条件];
注解:
如果不跟更新条件,则表里所有的记录都会更新;
如果跟了更新条件,则只有满足条件的记录会更新;
【示例27】
将成绩表里所有人的成绩减少2分
select * from cjb;
update cjb set cj=cj-2;
【示例28】
将001号学员成绩增加2分
update cjb set cj=cj+2 where xh='001';
【示例29】
将张三的性别更新为女,年龄更新为21岁
select xb,nl from xsb where xm='张三';
update xsb set xb='女',nl=21 where xm='张三';
-
📱任务4:数据的删除
语法:
delete from 表名 [where 删除条件];
注解:
如果不跟条件则会删除表里所有的数据(保留表结构)
如果跟条件则会删除满足条件的记录
【示例30】
删除001号学员的01号课程的成绩记录
delete from cjb where xh='001' and kch='01';
【示例31】
删除所有学员的课程成绩
delete from cjb;
【示例32】
删除001号学员的信息
delete from xsb where xh='001';
直接删除001号学员的信息会失败,因为001号学员在cjb里有记录,
并且cjb(子表)的xh参照了xsb(父表)的xh,即主外键关联了,此时,
我们通常需要先删除cjb里的相关数据,再删除xsb的数据。
delete from cjb where xh='001';
delete from xsb where xh='001';
select * from xsb where xh='001';




版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/112797.html