oracle ocp 051 14,【OCP】Oracle 11g OCP 1Z0-051 001

1.View the Exhibit and examine the structure of the SALES, CUSTOMERS, PRODUCTS, and TIMES tables.

The PROD_ID column is the foreign key in the SALES table, which references the PRODUCTS table.

Similarly, the CUST_ID and TIME_ID columns are also foreign keys in the SALES table referencing the CUSTOMERS and TIMES tables, respectively.

Evaluate the following CREATE TABLE command:

CREATE TABLE new_sales(prod_id, cust_id, order_date DEFAULT SYSDATE)

AS

SELECT prod_id, cust_id, time_id

FROM sales;

Which statement is true regarding the above command?

28108e62059ef9b47af3fe31d215cd21.png

A. The NEW_SALES table would not get created because the DEFAULT value cannot be specified in the column definition.

B. The NEW_SALES table would get created and all theNOT NULLconstraints defined on the specified columns would be passed to the new table.

C. The NEW_SALES table would not get created because the column names in the CREATE TABLE command and the SELECT clause do not match.

D. The NEW_SALES table would get created and all the FOREIGN KEY constraints defined on the specified columns would be passed to the new table.

答案:BNEW_SALES表可以建立,并且所有指定列上的已定义的非空约束会传递给新表。

考点:使用子查询创建表(create table as subquery)时,显示创建的非空约束(explicitly not null)可以传递到新表,其他约束不会传递。

关键字:NOT NULL

【解析实验】:

解锁sh用户

SYS@PROD>alter user sh identified by sh account unlock;

SYS@PROD>conn sh/sh

a279c19499dfb22ba5533cd4f2db43d1.png

SH@PROD>create table new_sales(prod_id, cust_id, order_date DEFAULT SYSDATE)

2  as select prod_id, cust_id, time_id from sales;

b296fed749273329b779c68ce3522904.png

表可以被创建,AC选项错

查看新表上约束

SH@PROD>desc new_sales;

b5a87eaeb316e66cc33e42cdf92423c2.png

SH@PROD>set linesize 100

SH@PROD>col table_name for a10

SH@PROD>col column_name for a15

SH@PROD>col constraint_name for a25

SH@PROD>col search_condition for a30

SH@PROD>select a.TABLE_NAME,b.COLUMN_NAME,a.CONSTRAINT_NAME,a.CONSTRAINT_TYPE,a.SEARCH_CONDITION

from user_constraints a,user_cons_columns b

where a.constraint_name=b.constraint_name and a.table_name='NEW_SALES';

f847f3d07e4630b9e5e4b43ce65afab3.png

查看源表约束

SH@PROD>select a.TABLE_NAME,b.COLUMN_NAME,a.CONSTRAINT_NAME,a.CONSTRAINT_TYPE,a.SEARCH_CONDITION

2 from user_constraints a,user_cons_columns b

3  where a.constraint_name=b.constraint_name and a.table_name='SALES';

85116931f1b235114a66223960c2fea8.png

新表可以创建,原表上对应列上的not null约束传递到了新表,但其他约束(PROD_ID列上外键约束FOREIGN KEY)没有传递到新表。

D选项错,B选项正确。

实验结束,清理测试表

SH@PROD>drop table new_sales purge;

【官方文档】

【SQL Language Reference  -  create table  -  as subquery】

Oracle Database automatically defines on columns in the new table any NOT NULL constraints that have a state of NOT DEFERRABLE and VALIDATE, and were explicitly created on the corresponding columns of the selected table if the subquery selects the column rather than an expression containing the column. If any rows violate the constraint, then the database does not create the table and returns an error.

NOT NULL constraints that were implicitly created by Oracle Database on columns of the selected table (for example, for primary keys) are not carried over to the new table.

In addition, primary keys, unique keys, foreign keys, check constraints, partitioning criteria, indexes, and column default values are not carried over to the new table.

84221686fdeffdeff2db6fff8d05053f.png

【练习实验】

创建带有不同约束的测试表t1,c1,c2,c3上分别带有主键、非空、唯一三种约束

SH@PROD>create table t1 (

2  c1 number constraint t1_c1_pk primary key,

3  c2 number constraint t1_c2_nn not null,

4  c3 number constraint t1_c3_uk unique);

Table created.

查看原始表t1上约束

SH@PROD>select a.TABLE_NAME,b.COLUMN_NAME,a.CONSTRAINT_NAME,a.CONSTRAINT_TYPE,a.SEARCH_CONDITION

2  from user_constraints a,user_cons_columns b

3   where a.constraint_name=b.constraint_name and a.table_name='T1';

TABLE_NAME COLUMN_NAME     CONSTRAINT_NAME           C SEARCH_CONDITION

---------- --------------- ------------------------- - ------------------------------

T1         C2              T1_C2_NN                  C "C2" IS NOT NULL

T1         C1              T1_C1_PK                  P

T1         C3              T1_C3_UK                  U

使用子查询创建创建测试表t2

SH@PROD>create table t2 as select * from t1;

Table created.

查看新表上约束

SH@PROD>select a.TABLE_NAME,b.COLUMN_NAME,a.CONSTRAINT_NAME,a.CONSTRAINT_TYPE,a.SEARCH_CONDITION

2  from user_constraints a,user_cons_columns b

3  where a.constraint_name=b.constraint_name and a.table_name='T2';

TABLE_NAME COLUMN_NAME     CONSTRAINT_NAME           C SEARCH_CONDITION

---------- --------------- ------------------------- - ------------------------------

T2         C2              SYS_C0011275              C "C2" IS NOT NULL

SH@PROD>desc t2;

Name                                                  Null?    Type

----------------------------------------------------- -------- ------------------------------------

C1                                                             NUMBER

C2                                                    NOT NULL NUMBER

C3                                                             NUMBER

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值