栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据库的相关知识

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据库的相关知识

文章目录
  • 查询
    • 1. 基础查询
    • 2.条件查询
    • 3. 分组聚合/聚合函数
    • 4.分组查询 --->group by
    • 5.排序 order by
    • 6.限制查询
    • 7.多表查询
    • 8.嵌套查询
  • 添加 insert
  • 修改 update
  • 删除 delete


查询 1. 基础查询

查询学生的姓名

select name from student

查询学生的姓名和性别

select name,sex from student

查询学生的全部信息

select * from student
2.条件查询

条件运算符:

=------------------------等于
<> 或者 !=    ---------- 不等于
<     --------------------小于
<=    ---------------------小于等于
>     -------------------大于
>=    ------------------大于等于   
between and -------两个值之间
is null------------------为null
and---------------------并且
or-----------------------或者
in-----------------------包含
not-----------------not可以取非,主要在用在is或者in
like----------------like为模糊查询,支持%或者_匹配

查询班级号为2001的学生

select * from student where class_num = '2001'

查询年龄为20岁的学生

select name from student where age = 20

查询班级号为2001学生的姓名和性别 多次查询时,需要用,进行分割

select name,sex from student where class_num = '2001'

查询出年龄不是20岁的学生

select name form student where age <>20
select name from student where age != 20

查询出年龄小于21岁的学生

select * from student where age < 21

查询出年龄在21-25岁的学生

select * from student where age between 21 and 25

查询出班级号为空的同学

select * from student where class_num is null

查询出班级号为2001的男同学

select * from student where class_num = '2001' and sex ='男'

查询出班级号为2001和2002的同学

select * from student where class_num = '2001'or class_num = '2002'

查询出用户id为1和3的用户记录

-- in操作符允许我们在where子句中规定多个值
select * from student where id in(1,3)

查询出所有姓王的同学-----模糊查询like通配符(%任意多个字符—_单个字符)

select * from student where name like '王%'

查询出姓王且只有两个字的学生

select * from student where name like '王_'

查询出所有包含‘花’字的同学

select * from student where name like '%花%'
3. 分组聚合/聚合函数
sum        求和
avg        取平均
max        取最大值
min        取最小值
count      取得记录数
count(*) 表示取得当前查询的所有记录
count(字段名称)  表示不会统计为null的记录

求学生年龄的总和

select sum(age) from student

查询出学生的平均年龄

select avg(age) from student

查询出年龄最大的同学

select max(age),name from student

查询出所有学生的数量

select count(*) from student

查询出班级号不为空的学生数

select count(class_num) from student
4.分组查询 —>group by
group by  通过那个或那些字段进行分组  
group by 字段名称

求这个班的平均年龄

select avg(age),class_num from student group by class_num

查询出各个班年龄最大值

select max(age),class_num from student group by class_num
5.排序 order by
order by 字段名称   
ASC  升序--可以省略  DESC 降序

按照年龄从低到高进行排序

select * from student order by age

按照年龄从高到低进行排序

select * from student order by age DESC

按照年龄降序,并且按照学号升序

select * from student order by age DESC,sno
6.限制查询
limit 限定的条数 offset 从哪一条开始查询 (每页固定展示固定数量的数据)
select * from studet limit 3 offset 0;---第一页
select * from studet limit 3 offset 3;---第二页
select * from studet limit 3 offset 6;---第三页

分页查询需要给数据库两个参数 pageSize:每一页查询的条数 ;pageIndex:当前的页码数
select * form 表 limit pageSize offset pageSize*(pageIndex-1)
limit 从哪一条开始  ,限定条数
select * from student limit 0,3
表示从第0条开始 查询3条数据

查询出年龄最大的学生的信息 -->年龄倒序限制在第一个
select * from student order by age DESC limit 0,1
7.多表查询

查询出每个学生的所属班级名

select s.name,c.class_name from student s , class c

结果每个学生都出现了多个班级名 这就是笛卡尔积现象
多表进行查询的过程不受到任何限制,最终的查询结果是这几张表查询的乘积

sql 92 语法:是两张表的class_num 相等
优点:解决了笛卡尔积现象,实现了连接查询
缺点:表的连接条件和查询条件放在了一起,没有分离,容易造成失误

select s.name , c.class_name from student s , class c where s.class_num=c.class_num and sex='男'

sql 99 语法:select xxx from 表名A join 表名B on 连接条件 where 查询条件

select s.name , c.class_name from student s join class c on s.class_num = c.class_num where sex = '男'

sql 99 连接方式

1.内联式 (inner join) --->查询表A B 的重合的字段
select s.name , c.class_num from student s inner join class c on s.class_num = c.class_num
2.左外联式(left join)---->A B 两张表中,左边的表是主表,查询结果偏向主表
select s.name , c.class_num from student s left join class c on s.class_num = c.class_num
3.右外联式(right join)---->A B 两张表中,右边的表是主表,查询结果偏向主表
select s.name , c.class_num from student s right join class c on s.class_num = c.class_num
4.多表进行连表查询: select xxx from 表A join 表B on 连接条件 join 表C on 连接条件
8.嵌套查询

一般在子查询中,程序线运行嵌套在最内层的语句,在运行外层;因此在写子查询语句时,可以先测试下内层的子查询语句是否输出了自己想要的结果,在一层一层的往外测试,增加子查询的正确率

1.找到所有选选择课程号为1001的同学的名称
select t1.name from
(select student.name , relationship.cno from student
inner join relationship on student.sno = relationship.sno)t1
where t1.cno='1001'
2.找到所有选择课程号为数学的同学的名称
select t2.name from
(select t1.name,scoure.gradeName from
(select student.name,relationship.cno from student
	inner join relationship on student.sno=relationship.sno)t1
	inner join scoure on t1.con=scoure.cno)t2
where gradeName='数学'
添加 insert

id 不需要插入数据
单行添加: insert into 表名 (列名1,列名2,列名3,···) values (值1,值2,值3,···)

insert into student (name,sex,age) values ('企鹅','男',66)

多行添加1: insert into 表名 set 列名1 = 值1,列名2 = 值2,列名3 = 值3

insert into student set name = '琪琪',sex = '女',age = 22

多行添加2:insert into 表名 (列名1,列名2,列名3,···) values (值1,值2,值3,···),(值1,值2,值3,···),(值1,值2,值3,···)···

insert into student (name,sex,age) values ('硕','男',25),('韩磊','男',23),('李梅','女',25)
修改 update

update 表名 set 列名1 = 值1,列名2 = 值2··· where 字段名 = 值
注意不能丢了where条件,否则整个表的信息都会

update student set name = 'ssh',sex='男',age=24 where id =1
删除 delete

delete from 表名 where 字段名 = 值

delete from student where id = 20

删除整张表
(1)delete from 表名
(2)truncate table 表名
delete可以跟where条件 truncate不可以
delete是一行一行删除数据,truncate是清空整个表,然后创建一个和原始表结构一样的新表

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1019893.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号