在OCP SQL部分有这样一道题:
You need to add a NOT NULL constraint to the QUANTITY column in the PO_DETAIL table. Which statement should you use to complete this task?正确 A. ALTER TABLE po_detail MODIFY (quantity NOT NULL) D. ALTER TABLE po_detail ADD CONSTRAINT quantity_nn NOT NULL(quantity);
NOT NULL ConstraintsA NOT NULL constraint prohibits a column from containing nulls. The NULL keywordby itself does not actually define an integrity constraint, but you can specify it toexplicitly permit a column to contain nulls. You must define NOT NULL and NULLusing inline specification.If you specify neither NOT NULL nor NULL, then the defaultis NULL.
从not null约束的定义我们知道,如果我们要给一个列增加一个not null约束,只能通过modify的方式。那通过add行不行呢?比如alter table po_detail add constraint quantity_nn check(quantity is not null);我们可以通过这种办法达到"not null"的效果。那么check is not null既然能达到“not null”的效果,为什么Oracle要提出not null约束呢,两者的区别在哪里?
SQL> create table t(nn number not null,cnn number check(cnn is not null));Table created.
SQL> select constraint_name,constraint_type,search_condition from user_constraints where table_name = 'T';
CONSTRAINT_NAME CONST SEARCH_CONDITIO
--------------- ----- ---------------
SYS_C005420 C "NN" IS NOT NULLSYS_C005421 C cnn is not null
通过上面的例子可以发现,二者的CONSTRAINT_TYPE都是C(CHECK约束),而且,二者的检查条件也几乎完全相同,唯一的区别是,Oracle为NOT NULL约束生成检查条件时自动给列名加上了引号。 Oracle把NOT NULL约束作为约束的一种进行描述,而且为NOT NULL约束提供了专门的语法,难道这一切仅仅是为了方便考虑吗? 下面通过一个例子来看看NOT NULL和CHECK的不同之处。
SQL> create table t1 as select nn,cnn,rowid r from t where 1=0;
Table created.
SQL> desc t1
Name Null? Type
----------------------------------------- -------- ----------------------------
NN NOT NULL NUMBER
CNN NUMBER
R ROWID
SQL> select constraint_name,constraint_type from user_constraints where table_name = 'T1';
CONSTRAINT_NAME CONST
--------------- -----
SYS_C005422 C
SQL> alter table t1 add constraint cons_c_nn check(cnn is not null);
Table altered.
SQL> insert into t1 select nn,cnn,rowid from t;
402550 rows created.
SQL> commit;
Commit complete.
SQL> create index ind_t1_nn on t1(nn);
Index created.
SQL> exec dbms_stats.gather_table_stats(user,'T1',cascade=>true);
PL/SQL procedure successfully completed.
SQL> set autotrace on;
SQL> select count(*) from t1;
COUNT(*)
----------
402550
Execution Plan
----------------------------------------------------------
Plan hash value: 4079031386
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 204 (4)| 00:00:03 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| IND_T1_NN | 402K| 204 (4)| 00:00:03 |
---------------------------------------------------------------------------
由于具有NOT NULL约束的列上面存在索引,Oracle认为通过全索引扫描可以得到正确的答案而且速度比全表扫描快,因此执行计划使用了快速全索引扫描。
SQL> drop index ind_t1_nn;
Index dropped.
SQL> create index ind_t1_cnn on t1(cnn);
Index created.
SQL> exec dbms_stats.gather_table_stats(user,'T1',cascade=>true);
PL/SQL procedure successfully completed.
SQL> set autot on;
SQL> select count(*) from t1;
COUNT(*)
----------
402550
Execution Plan
----------------------------------------------------------
Plan hash value: 3724264953
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 326 (4)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T1 | 402K| 326 (4)| 00:00:04 |
-------------------------------------------------------------------
SQL> select /*+ index_ffs(t1 ind_t1_cnn) */ count(*) from t1;
COUNT(*)
----------
402550
Execution Plan
----------------------------------------------------------
Plan hash value: 3724264953
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 326 (4)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T1 | 402K| 326 (4)| 00:00:04 |
-------------------------------------------------------------------
虽然我们知道,根据CHECK约束的条件,COL_CHECK列上不会存在为空的记录,Oracle使用COL_CHECK上的索引同样可以得到正确的结果,但是Oracle这一次没有使用索引。而且,即使给出了提示,Oracle仍然没有使用索引。这应该是NOT NULL和CHECK的最大区别,NOT NULL成为了列的一种属性,而CHECK是列的数据的限制条件。 Oracle在确定执行计划,考虑是否使用索引的时候,只是检查了列的NOT NULL属性,而没有去检查CHECK约束中的具体约束条件。