08、SQL--数据处理(2)、事务回滚(含习题)

这篇博客介绍了SQL中的事务管理,包括事务的步骤和回滚操作,并提供了在system用户下访问scott用户employees表的示例。此外,还包含两组测验题,涉及更改员工信息、删除特定员工及创建、修改、删除表的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

四、数据库事务:

 

 

 

1、事务:

 

 

(1)步、

delete from employees

where employee_id=206

 

(2)步、

select count(*) from employees

 

(3)步、

commit ;

 

(4)步、

delete from employees

where employee_id=205

 

(5)步、设置一个savepoint A:

savepoint A ;

 

(6)步、查询个数:

select count(*) from employees ;

 

(7)、rollback to savepoint A ;

 

 

 

 

2、在system用户登录后访问scott用户的employees表:

System用户:select * from scott.employees for update ;点回车后没有反应。

因为scott用户对employees表锁定了,只有在scott用户用commit ; 释放锁。

 

 

 

 

 

五、测验题一:

 

55. 更改 108 员工的信息: 使其工资变为所在部门中的最高工资, job 变为公司中平均工资最低的 job

       1). 搭建骨架      

 update employees

       set salary = (



       ), job_id = (



       ) where employee_id = 108;

      

       2). 所在部门中的最高工资        

select max(salary)

       from employees

       where department_id = (

              select department_id

              from employees

              where employee_id = 108

       )

      

       3). 公司中平均工资最低的 job      

 select job_id

       from employees

       group by job_id

       having avg(salary) =  (

              select min(avg(salary))

              from employees

              group by job_id

       )

      

       4). 填充(总合):      

update employees e set salary = (

              select max(salary)

              from employees

              where department_id = e.department_id

       ), job_id = (

              select job_id

              from employees

              group by job_id

              having avg(salary) =  (

                     select min(avg(salary))

                     from employees

                     group by job_id

              )

       ) where employee_id = 108;

      

56. 删除 108 号员工所在部门中工资最低的那个员工.

 

       1). 查询 108 员工所在的部门 id      

 select department_id

       from employees

       where employee_id = 108;

      

       2). 查询 1) 部门中的最低工资:
      

select min(salary)

       from employees

       where department_id = (

              select department_id

              from employees

              where employee_id = 108

       )

      

       3). 删除 1) 部门中工资为 2) 的员工信息:

 delete from employees e

              where department_id = (

                       select department_id

                       from employees e

                       where employee_id = 108

              ) and salary = (

                       select min(salary)

                       from employees

                       where department_id = e.department_id

              )     

 

六、测验题二:

 

1、运行以下脚本创建表my_employees

Create table my_employee (  id         number(3),

first_name varchar2(10),

                            Last_name  varchar2(10),

                            User_id    varchar2(10),

                            Salary     number(5));

 

2、显示表my_employees的结构

DESC my_employees;

 

3、向表中插入下列数据

ID

FIRST_NAME

LAST_NAME

USERID

SALARY

1

patel

Ralph

Rpatel

895

2

Dancs

Betty

Bdancs

860

3

Biri

Ben

Bbiri

1100

4

Newman

Chad

Cnewman

750

5

Ropeburn

Audrey

Aropebur

1550

   

    INSERT INTO my_employee

    VALUES(1,’patel’,’Palph’,’Rpatel’895);

 

4、提交

COMMIT;

 

5、将3号员工的last_name修改为“drelxer”

UPDATE my_employees

SET last_name = ‘drelxer’

WHERE id = 3;

 

6、将所有工资少于900的员工的工资修改为1000
UPDATE my_employees

SET salary = 1000

WHERE salary< 900

 

7、检查所作的修正

SELECT * FROM my_employees

WHERE salary < 900

 

8、提交

COMMIT;

 

9、删除所有数据

DELETE FROM my_employees;

 

10、检查所作的修正

SELECT * FROM my_employees;

 

11、回滚

ROLLBACK;

 

12、清空表my_employees

TRUNCATE TABLE my_employees

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值