目录
入门操作
建立数据库
create database sqltest
建立表
create table students(
student_id number(5) constraint id_pk primary key,
moniter_id number(5),
name varchar2(10) not null,
sex varchar2(6) constraint sexx_chk check(sex in('男','女')),
dob date,
specialty varchar2(10)
);
查看表结构
describe student
增加数据
insert into students (student_id,moniter_id,name,dob,sex,specialty) values(10139,10101,'王二','20-12 月-1989','男',计算机');
insert into studentss values(02222,null,'tom','男','07-5月-1999','计算机');
查询表
select* from studentss;
删除表
drop table studentss;
条件删改查
select name,age from student where id=1
select name from teacher where id=(select teacher_id from student where name='licy')
update student set name='licy' where id=1
delete from student where id=2
添加列
alter table student add birthday datetime
修改列(可以重命名列名)
alter table student change age myage int
修改列(不可以重命名列名)
alter table student modify birthday date
删除列
alter table drop myage
删除数据库
drop database myfirstdatabase
中级操作
建立表
teacher表
student
1、查询教师平均工资
select avg(wage) from teacher;
2、统计学生人数
select count(*) from student;
3、找出全体学生中最大和最小的出生日期
select max(birthday),min(birthday) from student;
4、求全体教师工资方差
select variance(wage) from teacher
5、按系部号对teacher表进行分组
select department_id from teacher group by department_id;
6、查询每一个系部教师工资最大值
select department_id,max(wage) from teacher group by department_id;
7、求每一个系部对教师人数
select department_id,count(*) from teacher group by department_id;
8、求每一个系部工资在2000元以上的教师工资平均值
select department_id,avg(wage) from teacher where wage>2000 group by department_id;
9、求平均工资高于3500顶系部,显示系部号和平均工资
select department_id,avg(wage) from teacher group by department_id having avg(wage)>3500
10、在工资低于3000元的教师中检索平均工资高于2000的系部,显示系部号和平均工资
select department_id,avg(wage) from teacher where wage<3000 group by department_id having avg(wage)>1700
11、在工资低于3200元的教师中检索平均工资高于2000的系部,显示系部号和平均工资,并将显示结构按平均工资降序进行排列
select department_id,avg(wage) from teacher where wage<3200 group by department_id having avg(wage)>2000 order by 2 desc;
12、检索student表,显示学生专业这一列
select distinct specialty from student
13、nvl2函数使用,计算教师月总收入,并用函数处理bonus出现null的情况(没建这些字段)
nvl2(1,2,3) 若参数1为空,则执行3,反则执行2
select name as “姓名”,nvl2(bonus,bonus+wage,wage) as “月总收入”,department_id as “部门”
14、查询工资低于任何一个部门平均工资的教师信息
select * from teacher where wage <all(select avg(wage) from teacher group by department_id)
15、利用子查询,在teacher表中检索在各自部门中工资最低的教师
select * from teacher where (department_id,wage) in (select department_id,min(wage) from teacher group by department_id);
事务
1、事务的定义
一个最小的不可再分的工作单元,通常一个事务对应一个完整的业务(例如银行账户转账业务,该业务就是一个最小的工作单元)
2、例子入门
银行转账懂吧,大儿给小儿转1000块=>大儿3000,小儿也3000
执行操作语句如下
update account set balance=balance+1000 where id=1
update account set balance=balance-1000 where id=2
如果第一条语句执行成功后,第二条语句执行异常,那么小儿是3000,大儿还是4000,岂不是很危险
这就产生了事务:以上两台DML语句必须同时成功或者同时失败
3、事务初体验
回滚
大儿给小儿汇款5000
第一句语句执行后,第二句语句是有问题的,因为大儿最多4000元,这时候判断资产为负,这整个流程就是有问题的,需要撤销此操作,因此需要事务
提交
给大儿给小儿汇款4000
资产判断无问题情况下,事务通过,提交
springboot初识事务
1、不使用事务
我是要删除最后一条评论 ,执行删除语句,很简单的大家应该都会
执行结果,抛异常,可是该评论被删掉了(数据库最终执行了删除),显然不符合我们正常业务需求
我们希望当这边执行出错时,不要改变数据库底层数据
2、添加事务
删除最后一条评论,这次我们使用事务注解(下图这注解意思是当出现异常这方法就回滚)
出现异常,但是该评论并没有被删除
@Transactional常用参数
readOnly | 该属性用于设置当前事务是否为只读事务,设置为true表示只读,false则表示可读写,默认值为false。例如:@Transactional(readOnly=true) |
rollbackFor | 该属性用于设置需要进行回滚的异常类数组,当方法中抛出指定异常数组中的异常时,则进行事务回滚。例如: 指定单一异常类:@Transactional(rollbackFor=RuntimeException.class) 指定多个异常类:@Transactional(rollbackFor={RuntimeException.class, Exception.class}) |
rollbackForClassName | 该属性用于设置需要进行回滚的异常类名称数组,当方法中抛出指定异常名称数组中的异常时,则进行事务回滚。例如: 指定单一异常类名称:@Transactional(rollbackForClassName="RuntimeException") 指定多个异常类名称:@Transactional(rollbackForClassName={"RuntimeException","Exception"}) |
noRollbackFor | 该属性用于设置不需要进行回滚的异常类数组,当方法中抛出指定异常数组中的异常时,不进行事务回滚。例如: 指定单一异常类:@Transactional(noRollbackFor=RuntimeException.class) 指定多个异常类:@Transactional(noRollbackFor={RuntimeException.class, Exception.class}) |
noRollbackForClassName | 该属性用于设置不需要进行回滚的异常类名称数组,当方法中抛出指定异常名称数组中的异常时,不进行事务回滚。例如: 指定单一异常类名称:@Transactional(noRollbackForClassName="RuntimeException") 指定多个异常类名称:@Transactional(noRollbackForClassName={"RuntimeException","Exception"}) |
@Transactional 注解只能被奏效在 public 方法上