有你在真好 的个人博客
数据库笔试面试7——下面选项中哪几个更改永久保存到数据库
阅读:2274 添加日期:2021/3/27 23:26:16 原文链接:https://www.toutiao.com/item/6414220285322461697/

数据库笔试面试7——下面选项中哪几个更改永久保存到数据库

阅读下面的PL/SQL程序块:

BEGIN

INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(35000,'WANG','FRED');

SAVEPOINT SAVE_A;

INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(40000,'WOO','DAVID');

SAVEPOINT SAVE_B;

INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(50000,'LDD','FRIK');

SAVEPOINT SAVE_C;

INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(45000,'LHR','DAVID');

INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(25000,'LEE','BERT');

ROLLBACK TO SAVEPOINT SAVE_C;

INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(32000,'CHUNG','MIKE');

ROLLBACK TO SAVEPOINT SAVE_B;

COMMIT;

END;

运行上面的程序,下面选项中哪几个更改永久保存到数据库()(多选题)

A、INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(50000,'LDD','FRIK');

B、INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(32000,'CHUNG','MIKE');

C、INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(35000,'WANG','FRED');

D、INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(40000,'WOO','DAVID');

答案:C、D。

保存点(SAVEPOINT)是事务处理过程中的一个标志,与回滚命令(ROLLBACK)结合使用。其主要用途是允许用户将某一段处理进行回滚而不必回滚整个事务。

1)执行SAVEPOINT SAVE_A的时候创建了一个保存点SAVE_A;

2)执行SAVEPOINT SAVE_B的时候创建了一个保存点SAVE_B;

3)执行SAVEPOINT SAVE_C的时候创建了一个保存点SAVE_C;

4)在执行ROLLBACK TO SAVEPOINT SAVE_C后,SAVEPOINT SAVE_C到当前语句之间所有的操作都被回滚;也就是说回滚到了3)的状态;

5)在执行ROLLBACK TO SAVEPOINT SAVE_B后,SAVEPOINT SAVE_B到当前语句之间所有的操作都被回滚;也就是说回滚到了2)的状态;

6)在执行COMMIT后,只有SAVEPOINT SAVE_B之前的操作会被提交从而永久保存到数据库。

使用程序验证以上的分析,如下所示:

SYS@lhrdb> create table EMPLOYEE

2 (

3 first_name VARCHAR2(20),

4 last_name VARCHAR2(25),

5 salary NUMBER(8,2)

6 );

Table created.

SYS@lhrdb> BEGIN

2 INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(35000,'WANG','FRED');

3 SAVEPOINT SAVE_A;

4

5 INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(40000,'WOO','DAVID');

6 SAVEPOINT SAVE_B;

7

8 INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(50000,'LDD','FRIK');

9 SAVEPOINT SAVE_C;

10

11 INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(45000,'LHR','DAVID');

12 INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(25000,'LEE','BERT');

13 ROLLBACK TO SAVEPOINT SAVE_C;

14

15 INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(32000,'CHUNG','MIKE');

16 ROLLBACK TO SAVEPOINT SAVE_B;

17

18 COMMIT;

19 END;

20 /

PL/SQL procedure successfully completed.

SYS@lhrdb> SELECT * FROM EMPLOYEE;

FIRST_NAME LAST_NAME SALARY

-------------------- ------------------------- ----------

FRED WANG 35000

DAVID WOO 40000

综上分析,题目中的程序块在执行完毕后,只有SAVEPOINT SAVE_B之前的操作会被提交从而永久保存到数据库,显然,C和D选项正确。

所以,本题的答案为C、D。

ICP备案号:苏ICP备14035786号-1 苏公网安备 32050502001014号