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')这是啥语法?
------最佳解决方案--------------------------------------------------------
只有D的语法正确且满足需求。
这里注意with check option的用法。
insert into (select ...)等价于insert into t(col1,col2)
------其他解决方案--------------------------------------------------------
这是插入数据的基本语法.
使用 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 语句将不为这些类型的列指定值: