OCP-1Z0-051 第161题 insert子句中的子查询

一、原题
 View the exhibit and examine the description for the SALES and CHANNELS tables.

You issued the following SQL statement to insert a row in the SALES table:
INSERT INTO sales
VALUES
  (23,
   2300,
   SYSDATE,
   (SELECT channel_id FROM channels WHERE channel_desc = 'Direct Sales'),
   12,
   1,
   500);
Which statement is true regarding the execution of the above statement?
A. The statement will execute and the new row will be inserted in the SALES table.
B. The statement will fail because subquery cannot be used in the VALUES clause.
C. The statement will fail because the VALUES clause is not required with subquery.
D. The statement will fail because subquery in the VALUES clause is not enclosed with in single quotation marks

答案:A

二、题目翻译
查看SALES and CHANNELS表的结构
执行下面的SQL语句给SALES表插入一行:
关于执行上面的语句哪句话是正确的?
A.语句执行成功,并且SALES表会插入一个新行。
B.语句失败,因为子查询不能用于VALUES子句中。
C.语句失败,因为VALUES子句不需要使用子查询。
D.语句失败,因为VALUES子句中的子查询没有使用单引号引起来。

三、题目解析
A选项,答案是正确的,如果子查询中只返回一行,则这个答案正确,如果子查询中返回多行,就会报错,但相比其它答案,只有这个还算比较正确。
B选项不正确,values子句中,可以使用子查询,详见下面。
C选项不正确,需不需要使用子查询,是业务需求决定的。
D选项不正确,子查询使用括号,而不是单引号。

四、测试

SQL>  create table emp3 as select * from emp where 1=0;

Table created.

-- values中的子查询如果返回一行,则能插入成功:
SQL>  insert into emp3(empno,ename,deptno) values(1234,'aaa',(select deptno from emp where ename='SCOTT'));

1 row created.

-- values中的子查询如果返回多行,则插入失败:
SQL> insert into emp3(empno,ename,deptno) values(1234,'aaa',(select deptno from emp where deptno=10));
insert into emp3(empno,ename,deptno) values(1234,'aaa',(select deptno from emp where deptno=10))
                                                        *
ERROR at line 1:
ORA-01427: single-row subquery returns more than one row

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值