Oracle11g温习-第十四章:约束( constraint )

2013年4月27日 星期六

10:48

 

1、约束的功能

 通过一些强制性商业规则,保证数据的完整性、一致性

2、约束的类别

    1 )  not null    不允许为空

    2 )  check       数值类要求       约束该列的值必须满足某种要求

    3 )  unique    唯一

    4 )  primary key (  not null + unique )      非空  唯一  只能有一个主键  唯一标示一行记录

    5 )  foreign key (  reference )              外键      

 【当进行dml操作的时候会检查当前约束是延迟还是非延迟的

  如果是可延迟的约束只有提交的时候才会检查约束】

 

3、约束的状态

 1 )  enable  validate(  默认 )  :【约束会立刻生效,并且 新 旧数据都要符合约束】

SCOTT @ prod > alter table emp1            enable validate constraint  pk_emp1;                                                                                  

alter table emp1

*

ERROR at line 1:

ORA-02437: cannot validate (  SCOTT.PK_EMP1 ) - primary key violated

-----新旧记录都必须符合约束

 2 )  enable novalidate :【约束会立刻生效,只检查新的数据,已经存在的数据可以不符合约束( 注意对primary key 和unique,必须先建立非唯一性索引 )】  

    SCOTT @ prod > create table emp1 as select * from emp;                                                                                    

 

Table created.  

 

SCOTT @ prod >  select * from emp1;                                                                                                       

 

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO

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

      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30

      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30

      7566 JONES      MANAGER         7839 02-APR-81       2975                    20

      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30

      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30

      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10

      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20

      7839 KING       PRESIDENT            17-NOV-81       5000                    10

      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30

      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20

      7900 JAMES      CLERK           7698 03-DEC-81        950                    30

      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20

      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10   

SCOTT @ prod >  update emp1 set empno=7788 where ename='KING';                                                                           

1 row updated.

SCOTT @ prod >  select * from emp1 WHERE EMPNO=7788;                                                                                       

 

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO

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

      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20

      7788 KING       PRESIDENT            17-NOV-81       5000                    10

 

 

 添加primary key ,到enable novalidate

 

SCOTT @ prod > alter table emp1 add constraint     pk_emp1 primary key (  empno ) enable novalidate;           新数据校验  旧的数据不校验  

                                                              

  pk_emp1 primary key (  empno ) enable novalidate

  *

ERROR at line 2:

ORA-02437: cannot validate (  SCOTT.PK_EMP1 ) - primary key violated

 ——原因:在建立primary key 它需要建立unique index

 

——在empno 上建立索引非唯一性索引 )

SCOTT @ prod >  create index       emp1_empno_ind     on    emp1(  empno )        tablespace      indexes;                                                             

 

Index created.

 

SCOTT @ prod >  alter table emp1 add constraint                                                                                           

   pk_emp1 primary key (  empno ) using     index     enable    novalidate ;                                                              

 

Table altered.

 

——新的记录必须符合约束,旧的不检查

 

SCOTT @ prod >  update emp1 set empno=7788 where ename='FORD';                                                                            

update emp1 set empno=7788 where ename='FORD'

*

ERROR at line 1:

ORA-00001: unique constraint (  SCOTT.PK_EMP1 ) violated

 

SCOTT @ prod > desc  user_constraints ;

SCOTT @ prod > select  constraint_name,constraint_type,status ,validated   from   user_constraints where

Constraint_name=’’;        【检查约束状态】  

 

3 )  disable validate:  约束失效,但不能做DML操作

4 )  disable novalidate:约束失效,不检查任何数据(  等同于禁用约束 )

4、修正约束将违背约束的记录插入到exceptions表里 )

 1 ) SCOTT @ prod >  @?/rdbms/admin/utlexpt1.sql                                                                                                

SQL> Rem

SQL> Rem $Header: utlexpt1.sql 24-jun-99.07:59:18 echong Exp $

11:47:09 SQL> Rem

11:47:09 SQL> Rem utlexpt1.sql

11:47:09 SQL> Rem

11:47:09 SQL> Rem  Copyright (  c ) Oracle Corporation 1998, 1999. All Rights Reserved.

11:47:09 SQL> Rem

11:47:09 SQL> Rem    NAME

11:47:09 SQL> Rem        utlexpt1.sql - <one-line expansion of the name>

11:47:09 SQL> Rem

11:47:09 SQL> Rem    DESCRIPTION

11:47:09 SQL> Rem        <short description of component this file declares/defines>

11:47:09 SQL> Rem

11:47:09 SQL> Rem    NOTES

11:47:09 SQL> Rem        <other useful comments, qualifications, etc.>

11:47:09 SQL> Rem

11:47:09 SQL> Rem    MODIFIED   (  MM/DD/YY )

11:47:09 SQL> Rem    echong      06/24/99 - rename

11:47:09 SQL> Rem    echong      06/05/98 - exceptions table with urowid type

11:47:09 SQL> Rem    echong      06/05/98 - Created

11:47:09 SQL> Rem

11:47:09 SQL>

11:47:09 SQL> create table exceptions(  row_id urowid,

11:47:09   2                          owner varchar2(  30 ),

11:47:09   3                          table_name varchar2(  30 ),

11:47:09   4                          constraint varchar2(  30 ) );

 

Table created.                       EXCEPTIONS

 

SCOTT @ prod > select * from tab;                                                                                                         

 

TNAME                          TABTYPE  CLUSTERID

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

DEPT                           TABLE

EMP                            TABLE

BONUS                          TABLE

SALGRADE                       TABLE

ADMIN_EXT_EMPLOYEES            TABLE

EMPLOYEES                      TABLE

TEST                           TABLE

EMP1                           TABLE

EXCEPTIONS                     TABLE

9 rows selected.

 

SCOTT @ prod > alter table emp1          enable validate constraint   pk_emp1    exceptions     into     exceptions;      【把违背约束的数据插入expections  】                                             

alter table emp1

*

ERROR at line 1:

ORA-02437: cannot validate (  SCOTT.PK_EMP1 ) - primary key violated

---------把违背约束的记录插入到exceptions 表里

 

SCOTT @ prod > select * from exceptions; 

 

ROW_ID                         OWNER                TABLE_NAME                     CONSTRAINT

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

AAAM0/AAEAAAAGcAAI             SCOTT                EMP1                           PK_EMP1

AAAM0/AAEAAAAGcAAH             SCOTT                EMP1                           PK_EMP1

 

SCOTT @ prod > delete from emp1            where rowid in (  select row_id from exceptions );                                                                          

 

2 rows deleted.

 

SCOTT @ prod >alter table emp1        enable    validate     constraint     pk_emp1;                                                                                      

 

Table altered.

5、延迟性约束deferrable(  约束只在提交事务时检查 )   并且回滚事务

 

SCOTT @ prod > alter table dept1      add constraint pk_dept1 primary key (  deptno );                                                                            

Table altered.

SCOTT @ prod > alter table emp1    add constraint fk_emp1 foreign key(deptno) references dept1(deptno) deferrable;                                          

 

Table altered.

 

——deferrable 可延迟(默认是immediate ,非 deferr 延迟)

 

SCOTT @ prod >set constraint fk_emp1 deferred;                                                                                          

Constraint set.

 

---------将约束fk_emp1 进入延迟状态数据可以正常插入但是提交会报错

 

SCOTT @ prod > select * from emp1;                                                                                                       

 

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO    

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

      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30

      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30

      7566 JONES      MANAGER         7839 02-APR-81       2975                    20

      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30

      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30

      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10

      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30

      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20

      7900 JAMES      CLERK           7698 03-DEC-81        950                    30

      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20

      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

 

12 rows selected.

SCOTT @ prod > select * from dept1;                                                                                                      

 

    DEPTNO DNAME          LOC

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

        10 ACCOUNTING     NEW YORK

        20 RESEARCH       DALLAS

        30 SALES          CHICAGO

        40 OPERATIONS     BOSTON

 

SCOTT @ prod > update emp1 set deptno=50 where empno=7900;                                                                               

 

1 row updated.

 

SCOTT @ prod > commit;                                                                                                                   

commit

*

ERROR at line 1:

ORA-02091: transaction rolled back

ORA-02291: integrity constraint (  SCOTT.FK_EMP1 ) violated - parent key not found

 

 

SCOTT @ prod > select * from emp1;                                                                                                       

 

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO

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

      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30

      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30

      7566 JONES      MANAGER         7839 02-APR-81       2975                    20

      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30

      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30

      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10

      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30

      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20

      7900 JAMES      CLERK           7698 03-DEC-81        950                    30

      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20

      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

 

12 rows selected.

——【插入记录只在提交(  commit ) 时检查

 Alter  table  emp  modify  (  empno  varchar2(6)      not null  )     非空约束

 Alter  table  emp  add constraint  constraint_name  unique (  empno )   唯一约束

 Alter  table  emp  add constraint  constraint_name  check  (  sal >5000 );  

 Dba_column_contents    ;          dba_constraints;

 添加主键 或是 唯一约束的时候 必须添加 索引

 

posted @ 2017-09-15 14:51  Oracle-fans  阅读(512)  评论(0编辑  收藏  举报