首页 > 代码库 > OCP-1Z0-051-题目解析-第13题

OCP-1Z0-051-题目解析-第13题

13. View the Exhibit and examine the structure of the PRODUCTS table.


You need to generate a report in the following format:
CATEGORIES
5MP Digital Photo Camera‘s category is Photo
Y Box‘s category is Electronics
Envoy Ambassador‘s category is Hardware
Which two queries would give the required output? (Choose two.)

A. SELECT prod_name q‘‘‘s category is ‘ prod_category CATEGORIES
FROM products;
B. SELECT prod_name q‘[‘s ]‘category is ‘ prod_category CATEGORIES
FROM products;
C. SELECT prod_name q‘\‘s\‘ ‘ category is ‘ prod_category CATEGORIES
FROM products;
D. SELECT prod_name q‘<‘s >‘ ‘category is ‘ prod_category CATEGORIES
FROM products;

注意:选项中字符串之间的连接应加连接符||
Answer: CD

题目解析:
sql中字符串的两边是需要加单引号的,表示字符串的开始和结束,如果我们想在sql中输出一个实际的单引号‘,那么需要连续输入四个单引号‘‘‘‘,其中中间的两个单引号表示一个单引号。
第一个单引号:表示字符串开始
第二个单引号:表示转义,后面接的单引号转义为真正的单引号
第三个单引号:表示实际输出的单引号
第四个单引号:表示字符串结束
如:
SQL> select ‘‘‘‘ name from dual;

N
-

SQL> select ‘Camera‘‘s category is Photo‘ name from dual;

NAME
--------------------------
Camera‘s category is Photo


另外Oracle还提供了一个Q-quote的表达式,来简化SQL或PLSQL中字符串的表示,其格式为q‘[Camera‘s category is Photo]‘,输出为方括号中的原始字符串格式,其中方括号可以更换为其他的任意一对特殊符号,但必须是成对出现的,不可为q‘[Camera‘s category is Photo|‘。
如:
SQL> select q‘[Camera‘s category is Photo]‘ name from dual;

NAME
--------------------------
Camera‘s category is Photo

SQL> select q‘|Camera‘s category is ‘Photo‘|‘ name from dual;

NAME
----------------------------
Camera‘s category is ‘Photo‘

A:错误, is后面应该是两个单引号,正确的为
SELECT prod_name || q‘‘‘s category is ‘‘|| prod_category CATEGORIES
FROM products;
B:错误,category 前面少了一个‘,正确的为
SELECT prod_name || q‘[‘s ]‘||‘category is ‘||prod_category CATEGORIES FROM products;