SQL 数据操作与查询全攻略:轻松掌握高效数据管理技巧


在本功率中,我们将探索 SQL 数据操作语言(DML)和查询语言(DQL)的全面用法,帮助你在数据库管理中游刃有余。从新增、修改、删除数据到复杂查询的高效执行,每一步都将为你的数据操作提供最佳实践和优化技巧。无论你是 SQL 新手还是资深专家,这里都有你提升数据库技能所需的实用知识与示例。 让我们一起深入了解,SQL 的全部潜能

DML

新增

格式:insert into 表名(字段名,…)values (值…)

日期 使用字符串的形式书写日期格式(yyyy-MM-dd HH:mm:ss)

  1. 使用 insert into
-- 方式一:
insert into student (sid,sname,birthday,ssex,classid) 
values(9,'张三','2003-1-2','男',1)
-- 方式二 1.default 2.null
insert into student values(default,'大白','2016-7-8','男',2);
insert into student values(null,'大黑','2016-7-8','男',2);
-- 部分字段插入
insert into student (sname,ssex) values('陆尘','男');
alter table student modify ssex varchar(10) not null default("保密");
insert into student (sname) values('壹.叁');
-- 一次添加多条数据
insert into student (sname,ssex) values ('晓晓','女'),('川子','男');
  1. insert into select(不常用)

插入表和被插入表都必须存在

insert into newstu(xingming,xingbie,classid)
select sname,ssex,classid from student;
  1. create table select(不常用)

被插入表不能存在

create table stu1
select sid,sname,birthday from student;

修改

update 表名 set 字段名=值 where 子句;

where 对表中的每一条数据

子句成立该数据父句执行,反之不执行

update stu1 set birthday='1974-7-10' where sname='晓晓';
update newstu set classid=20 where xingbie !='男';

update newstu set xingbie='地球' where classid in (30,50,70);

update newstu set xingbie='火星人' where classid between 30 and 90;

删除

delete from 表名

delete from newstu where xingming='川子';
select * from newstu;

清空表、截断表

truncate 表名

delete 只删数据 / truncate 删数据,删索引 / drop删数据,索引以及表结构

truncate stu1;

DQL

所有查询都会得到一张虚拟表,在内存

全字段获取数据

select * from student; -- 从效率上慢
select 字段名 from student;
-- 字段名起别名
select sname as '姓名',birthday '生日',ssex 性别 from student;

-- 添加字段
select sid,sname,birthday,ssex,classid,'山海' 学校 from student;

distinct

distinct 去重
所有字段的数据完全一致时才会去重

select distinct ssex from student;

带条件的查询 where

select * from student where classid=1 and ssex='女';
-- 查询年龄大于1990-1-1的学生
select * from student where birthday < '1990-1-1';
-- in 在某个特定的范围内(推荐in 不推荐or)
-- or 会使索引失效
select * from student where sid in (3,5,7,9);

like

模糊查询 like

模糊符号
% 任意多的任意字符
% 可以匹配任意数量的字符(包括零个字符),非常适合用来查找包含或以特定字符开头或结尾的字符串。
_ (下划线)仅匹配单个字符,用于查找具有特定字符长度和特定字符模式的字符串

insert into student (sname) values ('小白'),('小小白'),('小白白'),('白白');

select * from student where sname like '白%';
select * from student where sname like '小__';

is

null
is (是一个什么)

select * from student where birthday is null;

聚合函数

将多个值变为一个值
count() 统计个数 / max() 求最大值/ min() 最小值/ sum() 总和/ avg()平均值

count

不统计null
select count(字段/常量/*) from 表名

select count(sid) from student; -- 主键可(其余不统计null)
select count('a') from student; -- 不推荐
select count(1) from student; -- 推荐
select count(*) from student; -- 推荐

sum avg min max

select sum(score) from sc;
select avg(score) from sc;
select min(score) from sc;
select max(score) from sc;
  1. 统计共多少次考试 ,总成绩,avg,min,max
select count(score),avg(score),min(score),max(score) from sc;

group by 分组

select ssex,count(1) from student group by ssex;
-- 班
select classid,count(1) from student group by classid;

having

having 对分组聚合后的数据进行条件筛选

select sid,sum(score) '总分',avg(score) '平均分' from sc  group by sid having avg(score) < 60; 

order by 排序

先写先排
降序 desc
升序 asc(默认)

select * from student order by classid desc;

limit 分页

limit 分页 (页码-1)*步长,步长

select * from student limit 位置(0开始),步长
select * from student limit 03
select sid,sum(score) from sc 
group by sid  
order by sum(score) desc limit 1,1
  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值