OCP-1Z0-051-V9.02-44题

44. View the Exhibit and examine the structure of the ORD table.
Evaluate the following SQL statements that are executed in a user session in the specified order:
CREATE SEQUENCE ord_seq;
SELECT ord_seq.nextval
FROM dual;
INSERT INTO ord
VALUES (ord_seq.CURRVAL, '25-jan-2007',101);
UPDATE ord
SET  ord_no= ord_seq.NEXTVAL
WHERE cust_id =101;
What would be the outcome of the above statements?
A. All the statements would execute successfully and the ORD_NO column would contain the value 2 for
the CUST_ID 101.
B. The CREATE SEQUENCE command would not execute because the minimum value and maximum
value for the sequence have not been specified. 
C. The CREATE SEQUENCE command would not execute because the starting value of the sequence
and the increment value have not been specified. 
D. All the statements would execute successfully and the ORD_NO column would have the value 20 for
the CUST_ID 101 because the default CACHE value is 20. 
Answer: A
答案解析:
使用NEXTVAL和CURRVAL的规则
可以在下列上下文中使用NEXTVAL和CURRVAL:
• 不是子查询一部分的SELECT 语句的SELECT 列表
• INSERT 语句中子查询的SELECT 列表
• INSERT 语句的VALUES 子句
• UPDATE 语句的SET 子句
 

CREATE SEQUENCE时可以省略所有参数,默认起始值为1,步长为1,无上限

此处提一下CACHE这个关键字:如果建立序列时不指定CACHENOCACHE,默认值为20
即一次性从序列里取20个数放入内存,如果内存崩溃,则这20个数就会丢失,再取值时从第21个数开始取值,CACHE设置的最小值为2
 
实验验证:
1、创建一个序列,什么选项都不用跟。
sh@TESTDB> CREATE SEQUENCE ord_seq;
 
Sequence created.
 2、使用序列。应用select列表, INSERT 语句的VALUES 子句,UPDATE 语句的SET 子句都可以成功执行。排除BC,而最后经过UPDATE后ORD_NO列值为2,而不是20.
sh@TESTDB> SELECT ord_seq.nextval  FROM dual;
 
   NEXTVAL
----------
         1
 
sh@TESTDB> INSERT INTO ord  VALUES (ord_seq.CURRVAL, '25-jan-2007',101);
 
1 row created.
 
sh@TESTDB> UPDATE ord SET  ord_no= ord_seq.NEXTVAL WHERE cust_id =101;
 
1 row updated.

原文地址:https://www.cnblogs.com/hzcya1995/p/13316937.html