Java面向对象数据库基本使用说明

系列文章目录

第一章 JAVA面向对象学习入门之数据库的使用


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


一,何为面向对象?

前言

Java学习需要不断的积累故此创建博客


`

一、数据库是什么?

1.Java存储数据区 与增删改查

二、使用步骤

1.如何创建数据库?

1.创建数据库
create database 数据库名;
设置字符集和排序规则
create database 数据库名
default character set 字符集 collate 排序规则;
2.删除数据库
drop database if exists testdb;
3.创建数据库
create database testdb*
default character set utf8mb4 collate utf8mb4_general_ci;
4. 使用数据库
use testdb;

2.如何创建表?

代码如下(示例):
创建学生表,内含id,stu_name学生名字,学生年龄,学生性别,学生地址

CREATE TABLE student
(
  	id int primary key auto_increment,
  	stu_name varchar(20),
	stu_age INT,
	stu_gender varchar(2),
	stu_address varchar(25)
);

–创建学生表,内含id,stu_name学生名字,学生年龄,学生性别,学生地址

3.增删改查基本使用方法

1.添加数据
insert [into] 表名[字段名1[, 字段名2]] value[s](值1, 值2),(值3, 值4);
1.1只添加一条数据注意不能添加null值
insert into 表名 set 字段名1=值[,字段名2=值];
2.删除数据
2.1物理删除
使用delete字段
2.2显示删除
在表中创建新的列del 用0和1作为区别 实现0显示 1不显示
3.修改数据
UPDATE <表名> SET 字段1=值1, 字段2=值2, … WHERE …;
例如:
UPDATE students SET score=score+10 WHERE score<80;
(更新分数小于80同学的数据)
SELECT * FROM students;
(查看更新后的数据)

三、查询

1.排序

select * from student where stu_age>18 AND stu_name LIKE’%三’;
– 排序,order by+条件
select * from student order by stu_age;
– 排序 倒序 desc 从大到小
select * from student order by stu_age desc;

2.模糊查询

like 字段显示 张% 查找张1 张12…
%四% 查找与李四花 %前可以放任意字数

四、索引函数

drop table if exists person;
create table person
(
p_id int primary key auto_increment, – 主键索引
p_name varchar(20)unique,-- 唯一索引
p_age int,
p_gender varchar(1),
p_address varchar(20),
p_description text,
index ix_address(p_address),-- 普通索引
fulltext index ix_desc(p_description), – 全文索引
index ix_address_gender(p_address,p_gender) – 组合索引(可以用多个)
);
– 先存储数据在进行判断 所以会导致ID有时候不对
– 删除索引(所有的索引都用index来删除)
drop index ix_name on person;
alter table person drop index ix_name;

– 创建唯一索引
– 创建唯一索引
– create +(家其他索引前缀,如:unique)+ index +索引名(自定) on 表名(字段名)
create unique index ix_name on person(p_name);
– 创建普通索引
create unique index ix_name on person(p_name);
create index ix_name on person(p_name);
alter table person add index person9(p-address);

– 给student 表 student_address加上普通索引
create index ix_address on student(student_address);
– 删除上述索引
alter index ix_address on student;
– 唯一索引可以为空

– 查询过程
explain select student_address from person;

– 索引失效
– 使用了or也会失效
explain select p_name from person where p_name=‘张三’ or p_age>12;
– 模糊查询是,%放在前面也会失效
explain select p_name from person where p_name like ‘%张’;
explain select p_name from person where p_name like ‘%三’;

– is not null 做空键查询时可能也会失效 ==null 失效
explain select *from person where p_name is not null;
– 列放到函数中可能会失效
explain select *from person where substr(p_name,1,2)=‘三’;
– 使用组合索引是,查询必须第一列(最左前缀原则,吧最重要列,放到最左)
explain select * from person where p_gender=‘男’;

– substr(字段,a,b) a从1开始,第一个字 b:截取长度 lenght
– 函数:截取下标a,后面a个字符
explain select substr(p_name,1,3)from person;

– 组合索引
explain select * from person where p_gender=‘男’;

1.or 2.is not null 3.%在前面 4.函数 5.组合索引 都会失效

五、表连接

– inner join 不会产生空数据
select *from type INNER JOIN animal on animal_type.type.id;

– 右表接在左表上去,可以有空数据
select *from type LEFT JOIN animal on animal_type.type.id;
注意:进行分组查询时,可以添加分组列和聚合函数,不要添加无关列

having 条件

对分组后的结果进行筛选

group by 列 having 条件

where和having的区别

  • where是分组前进行筛选
  • having是分组前进行筛选

where --> group by --> having

六、聚合函数

将一些数值类型的列进行统计,如:总和、平均值、最大值、最小值、总数

  • 总和 sum(列)
  • 平均值 avg(列)
  • 最大值 max(列)
  • 最小值 min(列)
  • 总行数 count(*)
-- 聚合函数
select sum(stu_age) 总年龄,avg(stu_age) 平均年龄,max(stu_age) 最大年龄,
min(stu_age) 最小年龄,count(*) 总数
from student;

七、分组查询

将表的数据按某些列分组,进行统计

如: 不同类型商品的销售量、男女学生的总数、不同地区的学生平均年龄、每个月份商品的销售量

group by 列

-- 男女学生的总数
select stu_gender 性别,count(*) 总数 from student
group by stu_gender;
-- 人数超过5人的地区
select stu_address 地区,count(*) 人数 from student
group by stu_address having count(*) >= 5;
-- 男人超过5人的地区
select stu_address 地区,count(*) 人数 from student
where stu_gender = '男' group by stu_address having count(*) >= 5;
-- 姓李的平均年龄超过24的地区
select stu_address 地区,avg(stu_age) 平均年龄 from student where stu_name like '李%'
group by stu_address having avg(stu_age) > 24;

八、连接查询

将多张表的数据在一个结果中显示

1.内连接

查询两张表中相关的数据(都存在的数据)

语法

select 列..... from 表1 inner join 表2
on 表1.主键 = 表2.外键
-- 内连接,显示考试学生的姓名和分数
select s.stu_id,s.stu_name,c.s_course,c.s_score from student s inner join score c
on s.stu_id = c.stu_id;

select s.stu_id,s.stu_name,c.s_course,c.s_score from student s , score c
where s.stu_id = c.stu_id;

2.外连接

2.1 左外连接

显示所有左表的数据,加上右表相关的数据,不相关的补空值

左表 left join 右表
on 关系

-- 查询没考试的学生
select s.stu_id,s.stu_name,c.s_course,c.s_score from student s left join score c
on s.stu_id = c.stu_id where c.s_score is null;

2.2 右外连接

显示所有右表的数据,加上左表相关的数据,不相关的补空值

right join

九、联合查询

将不同的表合并到一起查询出来

union

select 列... from 表1
union
select 列... from 表2

union 如果表1和表2有完全相同的数据,会自动合并

union all 不会合并相同的数据

select stu_name 姓名,stu_age 年龄,stu_gender 性别 from student
union all
select t_name,t_age,t_gender from teacher;

十、子查询

可以将查询语句嵌入到另一个查询中

先执行子查询,再执行外部查询

把子查询的结果当做表,进行查询

select * from
(
	select stu_name 姓名,stu_age 年龄,stu_gender 性别 from student 
	union all
	select t_name,t_age,t_gender from teacher
) person 
where person.性别 = '男';

-- 查找比李三大的学生
select * from student where stu_age >
(select stu_age from student where stu_name = '李三');
-- 查找李三的同乡
select * from student where stu_address =
(select stu_address from student where stu_name = '李三');

IN 关键字

如果子查询返回多条数据,需要使用in进行比较

-- 查找考过试的学生信息(子查询)
select * from student where stu_id in 
(select stu_id from score);

EXISTS 关键字

用于判断数据是否存在 返回true和false

-- EXISTS 学生表的学号是否在成绩表的学号中存在
select * from student where exists
(select * from score where score.stu_id = student.stu_id);

ALL 关键字

和比较运算符配合使用,必须比所有返回值都大(或都小)

-- 查找比所有南京学生的数学成绩高的武汉学生
select s.stu_name,c.s_score,s.stu_address from student s inner join score c
on s.stu_id = c.stu_id where s.stu_address = '武汉' and c.s_course='数学'
and c.s_score >
all(select c.s_score from student s inner join score c
on s.stu_id = c.stu_id where s.stu_address = '南京' and c.s_course='数学');

ANY 关键字

和比较运算符配合使用,任意一个比较成功都可以

-- 查找和任意一个南京学生的年龄相同的武汉学生
select * from student where stu_address = '武汉' and stu_age = any(
select stu_age from student where stu_address ='南京');

总结

数据库基本方法使用
增删改查

  • 29
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值