2. View the Exhibit and examine the description of the CUSTOMERS table.
You want to add a constraint on the CUST_FIRST_NAME column of the CUSTOMERS table so that the value inserted in the column does not have numbers. Which SQL statement would you use to accomplish the task?
A. ALTER TABLE CUSTOMERS ADD CONSTRAINT cust_f_name
CHECK(REGEXP_LIKE(cust_first_name,'^AZ')) NOVALIDATE
B. ALTER TABLE CUSTOMERS ADD CONSTRAINT cust_f_name
CHECK(REGEXP_LIKE(cust_first_name,'^[09]')) NOVALIDATE
C. ALTER TABLE CUSTOMERS ADD CONSTRAINT cust_f_name
CHECK(REGEXP_LIKE(cust_first_name,'[[:alpha:]]'))NOVALIDATE
D. ALTER TABLE CUSTOMERS ADD CONSTRAINT cust_f_name
CHECK(REGEXP_LIKE(cust_first_name,'[[:digit:]]'))NOVALIDATE
Answer: C
题目要求是对表加一个约束,使其cust_first_name列中的值不能有纯数字,答案为C。
通过我的实验发现对于答案C来说约束的状态无论是novalidate和validate都可以达到需求,这里就不理解了,如果novalidate是不进行新值的校验,而validate则是对新值进行校验,既然是对于同样的约束条件,不同的约束状态怎么可能都达到同一校验结果呢?
操作步骤如下:
1.创建表t和t2
create table t(name varchar2(10));
create table t2(name varchar2(10));
2.分别对表t和t2添加不同状态的约束
alter table t add constraint name_c check(regexp_like(name,'[[:alpha:]]')) novalidate;
alter table t2 add constraint name_c2 check(regexp_like(name,'[[:alpha:]]'));
3.检查每个约束的状态
select constraint_name,status,validated from user_constraints
where table_name in ('T','T2');
CONSTRAINT_NAME STATUS VALIDATED
------------------------------ -------------- ----------------------
NAME_C ENABLED VALIDATED
NAME_C2 ENABLED NOT VALIDATED
4.验证约束是否生效
SQL> insert into t values('a');
已创建 1 行。
SQL> insert into t values('1');
insert into t values('1')
*
第 1 行出现错误:
ORA-02290: 违反检查约束条件 (SCOTT.NAME_C)
SQL> insert into t2 values('b');
已创建 1 行。
SQL> insert into t2 values('1');
insert into t2 values('1')
*
第 1 行出现错误:
ORA-02290: 违反检查约束条件 (SCOTT.NAME_C2)
结论:从以上操作步骤可以看到无论约束的状态为validated和novalidated都可以达到我们所要的需求,就是这块不是很理解,请各位指教,非常感谢!
|
|