insert into (select ...) values('&jobid')的疑问?

ocp_toad 2009-03-29 04:13:08
Q: 111 Examine the structure of the EMPLOYEES table:

EMPLOYEE_ID NUMBER NOT NULL
EMP_NAME VARCHAR2(30)
JOB_ID VARCHAR2(20)
SAL NUMBER
MGR_ID NUMBER
DEPARTMENT_ID NUMBER

You want to create a SQL script file that contains an INSERT statement. When the script is run, the
INSERT statement should insert a row with the specified values into the EMPLOYEES table. The
INSERT statement should pass values to the table columns as specified below:

EMPLOYEE_ID: Next value from the sequence EMP_ID_SEQ
EMP_NAME and JOB_ID: As specified by the user during run time, through
substitution variables
SAL: 2000
MGR_ID: No value
DEPARTMENT_ID: Supplied by the user during run time through
substitution variable. The INSERT statement should
fail if the user supplies a value other than 20 or 50.
Which INSERT statement meets the above requirements?

A. INSERT INTO employees
VALUES (emp_id_seq.NEXTVAL, '&ename', '&jobid', 2000, NULL, &did);
B. INSERT INTO employees
VALUES (emp_id_seq.NEXTVAL, '&ename', '&jobid',
2000, NULL, &did IN (20,50));
C. INSERT INTO (SELECT *
FROM employees
WHERE department_id IN (20,50))
VALUES (emp_id_seq.NEXTVAL, '&ename', '&jobid', 2000, NULL, &did);
D. INSERT INTO (SELECT *
FROM employees
WHERE department_id IN (20,50)
WITH CHECK OPTION)
VALUES (emp_id_seq.NEXTVAL, '&ename', '&jobid', 2000, NULL, &did);
E. INSERT INTO (SELECT *
FROM employees
WHERE (department_id = 20 AND
department_id = 50)
WITH CHECK OPTION )
VALUES (emp_id_seq.NEXTVAL, '&ename', '&jobid', 2000, NULL, &did);

Answer: D

问下,这个答案为什么选D呢?
insert into (select ...)?这也可以啊?还有value('&jobid')这是啥语法?
...全文
488 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
limaowa 2009-03-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 vc555 的回复:]
只有D的语法正确且满足需求。
这里注意with check option的用法。

insert into (select ...)等价于insert into t(col1,col2)
[/Quote]

说的好
jdsnhan 2009-03-30
  • 打赏
  • 举报
回复
&jobid 中 &是引用变量
EMP_NAME and JOB_ID: As specified by the user during run time, through
题目的要求是在执行的时候由用户输入,所以,需要使用变量。
changeking 2009-03-29
  • 打赏
  • 举报
回复
The INSERT statement should fail if the user supplies a value other than 20 or 50. 如果没加WITH CHECK OPTION,那么不是20和50的数也会被插入,加勒WITH CHECK OPTION就不会允许对这个子查询不包含的记录做任何改变
cheng_fengming 2009-03-29
  • 打赏
  • 举报
回复
学习了!
dawugui 2009-03-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 ocp_toad 的帖子:]
问下,这个答案为什么选D呢?
insert into (select ...)?这也可以啊?还有value('&jobid')这是啥语法?[/Quote]

这是插入数据的基本语法.

使用 INSERT 添加行
INSERT 语句可给表添加一个或多个新行。INSERT 语句在简单的情况下有如下形式:

INSERT [INTO] table_or_view [(column_list)] data_values

此语句将使 data_values 作为一行或者多行插入已命名的表或视图中。column_list 是由逗号分隔的列名列表,用来指定为其提供数据的列。如果没有指定 column_list,表或者视图中的所有列都将接收数据。

如果 column_list 没有为表或视图中的所有列命名,将在列表中没有命名的任何列中插入一个 NULL 值(或者在默认情况下为这些列定义的默认值)。在列的列表中没有指定的所有列都必须允许 null 值或者指定的默认值。

由于 Microsoft® SQL Server™ 为以下类型的列生成值,INSERT 语句将不为这些类型的列指定值:

具有 IDENTITY 属性的列,该属性为列生成值。


有默认值的列,该列用 NEWID 函数生成一个唯一的 GUID 值。


计算列。
这些是虚拟列,被定义为 CREATE TABLE 语句中从另外一列或多列计算的表达式,例如:

CREATE TABLE TestTable
(ColA INT PRIMARY KEY,
ColB INT NOT NULL,
ColC AS (ColA + ColB) * 2)

所提供的数据值必须与列的列表匹配。数据值的数目必须与列数相同,每个数据值的数据类型、精度和小数位数也必须与相应的列匹配。有两种方法指定数据值:

用 VALUES 子句为一行指定数据值:
INSERT INTO MyTable (PriKey, Description)
VALUES (123, 'A description of part 123.')

用 SELECT 子查询为一行或多行指定数据值。
INSERT INTO MyTable (PriKey, Description)
SELECT ForeignKey, Description
FROM SomeView

vc555 2009-03-29
  • 打赏
  • 举报
回复
只有D的语法正确且满足需求。
这里注意with check option的用法。

insert into (select ...)等价于insert into t(col1,col2)

2,668

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 认证与考试
社区管理员
  • 认证与考试社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧