数据库基本增删改查语法和多表联查方式

目录

前言

一、增(insert)

二、删(delete)

三、改(update)

四、查(select)

        1、简单查询

        2、字段查询

        3、去除重复的数据(distinct)

        4、带条件查询、范围查询

        5、模糊查询

        6、查询null值

        7、聚合函数 

        8、分组(group by...)筛选(having)

        9、排序(order by ...)

        10、分页(limit)

 五、多表联查

        1、内连接

        2、外连接

        3、集合并集(union)

        4、子查询

总结


前言

        我们想要操作数据库,主要需要使用的基本指令就是增删改查,主要包括四个关键字,

增:insert,删:delete,改:update,查:select。接下来详细的讲一下这四种关键字的使用。

一、增(insert)

        基本语法:insert into 表名(字段名) value/values (值)

        1、全字段插入

全字段插入
insert into Student(Sid,Sname,birthday,Ssex,classid) 
						values(1,'张三','2001-6-2','男',1);

insert into Student values(2,'小红','2000-8-2','女',2);

        2、部分字段插入

         需要注意的是主键如果没有自增,需手动要插入数据,如果有非空约束并且没有设置默认值),需要插入数据。

不完全字段插入
insert into Student(Sid,Sname,Ssex) values(3,'小米','男');

        3、values和value的使用区别

        values:可以插入一条或多条数据,之间用‘,’逗号 隔开。

        value:一次性只能插入一条数据,比较之下,我们推荐使用values。

insert into student(sname) value('小米');
insert into Student(Sname) values('小明'),('哈哈');

         5、还有两种不推荐使用的方法

        (1)方式一  (查询表插入表都存在)

语法: insert into 表名 select
create table newstu(
	newname varchar(10),
	ssex varchar(2)
)
insert into newstu select Sname,Ssex from Student;

        (2)方式二  (查询表存在,插入表不能存在)

语法:create table 表名 select
create table newstu1 select Sid,Sname,birthday from Student;

二、删(delete)

        删除数据数据危险操作,我们在使用的时候必须注意要有条件的删除。

        1、语法:delete from 表名 where 字句【条件】;

整张表删除
delete from  Student;

单条件的删除
delete from Student where Sname='哈哈' ;

多条件删除
delete from Student where Sname='哈哈' and classid=1;

       2、 注意:delete不能清除主键自增的序号,比如删除了ID为3的学生,那么下一次新增的时候,ID是从4开始的,主键不能删除也不回溯,想要删除主键的自增,我们介绍一个关键字:

语法:truncate 表名;
truncate Student;
insert into Student(Sname) values('张三');  --ID从1开始

         3、常见面试题:delete、drop、truncate的区别?

        (1)delete:只是删除表中的数据。

        (2)drop:是删除表,包括表的结构和数据。

          (3)   truncate:是删除数据和自增索引,保留表结构。

三、改(update)

        修改数据也要带条件修改,否则整张表都会受到影响。 

        1、语法:update 表名 set 字段名=值,字段名=值... where 字句【条件】;

整张表进行修改(一般不用)
update newstu set ssex='女';

带条件修改
-- 修改名字为小米的性别为女
update newstu set ssex='女' where newname='小米';

-- 多条件修改
update Student set sname='小黑' where classid=1 and sname='小米';

         2、区间修改


-- classid在1到4之间的生日修改为2022-8-26
update Student set birthday='2022-8-26' where classid >=1 and classid <=4;

-- classid在1到4之间的姓名修改为无名
-- between 闭合区间 between 较小数据(向右) and 较大数据(向左)
update Student set Sname='无名' where classid between 1 and 4;

四、查(select)

        1、简单查询

select 123 ;
select 123+456;
select 'a';

        2、字段查询

       (1)sql优化 不要用*号代替字段
       (2)使用*会先去查看表的结构获取字段名,比直接写会慢一些

        全字段查询

select sid,sname,birthday,ssex,classid from student;
select * from student;

         部分字段查询(所有的查询都是虚拟表)

select sid,sname from student;
select sid,sname,birthday,ssex,classid,'西安' from student;

第二个语句的结果:显然不是我们想要的,我们想要的是表头危险显示地址那么就引入了起别名 

三种方式:通常我们用的是第三种

1、 select sname as '姓名' from student;
2、 select sname '姓名' from student;
3、 select sname 姓名 from student;
select sid,sname,birthday,ssex,classid,'西安' 地址 from student;

         3、去除重复的数据(distinct)

-- 根据性别去重
select distinct ssex from student;


(distinct后面的值要完全相同的时候才会去除重复)
insert into student(sname,ssex) values('赵雷','女'),('赵雷','女');
select distinct sname,ssex from student;

         当年龄和性别都相同的时候会去重,只显示一个。

        4、带条件查询、范围查询

查询男同学
select * from student where ssex='男';

查询一班的女同学
select * from student where ssex='女' and classid=1;

sid在3和7之间的
select * from student where sid>=3 and sid<=7;
select * from student where sid between 3 and 7;

-- in 某个特定范围内
select * from student where sid in(1,3,6,10,20);

查询出年龄大于1990的学生(日期要特别注意)
select * from student where birthday < '1990-1-1';

        5、模糊查询

        (1)符号% 任意多的任意字符

-- 前模糊 %key 
select * from student where sname like '%红';

-- 后模糊 key%
select * from student where sname like '红%';

-- 前后都模糊 %key%
select * from student where sname like '%红%';

         (2)符号 _ 一个任意字符

select * from student where sname like '红__'

        6、查询null值

-- 查询null值(null属于数据类型)
select * from student where birthday is not null;
select * from student where classid is null;

        7、聚合函数 

  •        count()统计个数(不统计null)
  •        sum() 统计综合
  •        max() 求最大值
  •        min() 求最小值
  •        avg() 求分均值
-- 一共有多少学生
-- count()
1、*
select count(*) from student;
2、字段名主键
select count(sid) from student;
3、常量
select count(1) from student;

-- sum()
select sum(score) from sc;
-- min()
select min(score) from sc;
-- max()
select max(score) from sc;
-- avg()
select avg(score) from sc;

(2)  加条件查询

找到男同学有多少人
select count(*) from student where ssex='男';

1号学生的总成绩
select sum(score) from sc where sid=1;

2号学生的最高分
select max(score) from sc where sid=2;

3号课程考的最差的成绩
select min(score) from sc where cid=3;

4号学生的平均分
select avg(score) from sc where sid=4;

统计三号学生考试次数,总成绩,最高分,最低分,平均分
select count(1) 次数,sum(score) 总成绩,max(score) 最高分,
min(score) 最低分,avg(score) 平均分 from sc where sid=3;

        8、分组(group by...)筛选(having)

男女同学人数
select ssex,count(*) from student group by ssex;

每个班有多少个人
select classid,count(*) from student group by classid;

每个同学平均成绩
select sid,avg(score) from sc group by sid;

每门课程最高分和最低分
select cid,max(score) 最高分,min(score) 最低分 from sc group by cid;
每个学生的总成绩(成绩大于200分)
select sid,sum(score) from sc group by sid having sum(score) > 200;

每个学生的总成绩(成绩大于200分,只统计成绩高于60分的)
select sid,sum(score) from sc where score>60 group by sid having sum(score) > 200;

        注意:where和having的区别

         1、where后面的筛选是根据表中的每一条数据进行筛选的。
         2、having 筛选(对分组之后的数据进行筛选),不能单独出现,必须要有group by 。

        9、排序(order by ...)

升序(默认)
select * from student order by sid asc;

降序
select * from student order by sid desc;

多个条件时,先写的优先排列
select * from sc order by score desc,cid desc,sid desc;

         10、分页(limit)

        分页公式:(页码-1)*步长,步长

-- limit 数字【长度】
-- limit 数字【位置】,数字【长度】
查看前三行 
select * from student limit 3;
每三行表示一页,查看第一页
select * from student limit 0,3;
查看第二页
select * from student limit 3,3;
查看第三页
select * from student limit 6,3;

 五、多表联查

        1、内连接

        隐式内连接:select 字段 from 表1,表2 where 条件....

        显式内连接:select 字段 from 表1 inner join on 表2 连接条件

        求的是红色区域:

 

查看学生信息和班级信息
-- 非等值联查 结果称为笛卡尔积
select * from student,class;
  
-- 内联联查 (数据少的时候)
select * from student,class where student.classid=class.classid; 

        2、外连接

        左连接:select 字段 from 表1 left join on 条件...

        右连接:select 字段 from 表1 right join on 条件...

 

 

主表先写用left 主表后写用right
-- left join on 左外联
select * from student left join class on student.classid=class.classid;

-- right join on 右外联
select * from class right join student on student.classid=class.classid;

        3、集合并集(union)

        注意:1、数据类型不同可以进行合并
                   2、两个集合的列数要一致
                   3、表头是第一个集合的信息(如果要起别名,只能给第一个集合起)
                   4、去除重复(完全一样才可以去掉) ,也可使用union all 不去除重复

select sname 姓名,ssex 性别,classid 其他 from student 
union 
select tname,tsex,temail from teacher;


-- 不带重复数据
select * from student 
left join class on student.classid=class.classid
union
select * from student 
right join class on student.classid=class.classid

-- 带重复数据
select * from student 
left join class on student.classid=class.classid
union all
select * from student 
right join class on student.classid=class.classid

        4、子查询

        (1)where子查询 

-- where 子查询(效率低)
select * from student where sid=(select max(sid) from student);

查询每个班下id最大的学生
select * from student 
where sid in(select max(sid) from student group by classid);

select student.sid,sname,ssex,student.classid,classname from student 
left join class on student.classid=class.classid 
where sid in(select max(sid) from student group by classid);

        (2)from子查询


查询大于5人的班级名称和人数(使用子查询)
select * from class
left join (select classid,count(*) 人数 from student group by classid) t
on class.classid=t.classid
where 人数>5;

        (3) exists子查询

-- 如果子句有数据,父句就执行;如果子句没有数据,父句就不会执行
select * from teacher where exists (select * from student where ssex='男');

        (4) any 子查询

查询出一班成绩比二班最低成绩高的学生
select * from student 
left join sc on student.sid=sc.sid
where classid=1 and score > any(
	select score from student left join sc on student.sid=sc.Sid	
	where classid=2
)

select * from student 
left join sc on student.sid=sc.sid
where classid=1 and score > some(
	select score from student left join sc on student.sid=sc.Sid	
	where classid=2
)

总结

        以上就是对数据库的增删改查,以及多表的查询做出的总结,总结内容有不足之处,大家可以指出,这些都是一些基础的操作,想要学好数据库,我们需要掌握好基础,小编就介绍到这里,希望对大家有所帮助,谢谢。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值