从Oracle9i开始,Oracle答应对于视图(view)进行主键、唯一键、外键约束的声名。NOT NULL约束可以从基表继续,所以不答应显示声明。
由于视图约束仅仅是声明而已,所以其状态只能是DISABLE NOVALIDATE。
以下是一个简单范例说明:
SQL> CREATE VIEW d10_emp
2 ( empno, ename, job,deptno,
3 CONSTRAINT pk_d10
4 PRIMARY KEY (ename)
5 RELY DISABLE NOVALIDATE
6 ) AS
7 SELECT empno, ename, job ,deptno
8 FROM emp
9 WHERE deptno = 10;
View created.
SQL> select * from d10_emp;
EMPNO ENAME JOB DEPTNO
---------- ---------- --------- ----------
7782 CLARK MANAGER 10
7839 KING PRESIDENT 10
7934 MILLER CLERK 10
SQL> select CONSTRAINT_NAME,TABLE_NAME,CONSTRAINT_TYPE from user_constraints
2 where table_name='D10_EMP';
CONSTRAINT_NAME TABLE_NAME C
------------------------------ ------------------------------ -
PK_D10 D10_EMP P
由于此约束仅仅为"约束声明",所以不具有实际约束力:
SQL> insert into d10_emp values(7777,'EYGLE','MANAGER',10);
1 row created.
SQL> insert into d10_emp values(8888,'EYGLE','MANAGER',10);
1 row created.
SQL> select * from d10_emp;
EMPNO ENAME JOB DEPTNO
---------- ---------- --------- ----------
7782 CLARK MANAGER 10
7839 KING PRESIDENT 10
7934 MILLER CLERK 10
7777 EYGLE MANAGER 10
8888 EYGLE MANAGER 10
-----
原文地址:
http://www.eygle.com/archives/2006/02/oracle9i_view_constraints_declarative.Html