2. 1、constraint类型介绍
SQL> select distinct constraint_type from dba_constraints;
CONSTRAINT_TYPE
-
V
R
U
P
?
C
O
已选择 7 行。
SQL>
类型说明:
V——(Check Option on a view)
R——(Reference Froeign Key 参照外键约束
U——(Unique)唯一性约束
P——(Primary Key)unique and not null,主键约束
?——
C——(Check)present in a static list of values permitted for the column,检查约束
O——(Read Only on a view)
描述:
Type.Code Type .Description Acts.On.Level
C Check.on.a.table Column
O Read.Only.on.a.view Object
P Primary.Key Object
R Referential.Foreign.Key Column
U Unique.Key Column
V Check.Option.on.a.view Object
常用:
1、primary key unique and not null,可以是多个 column 的联合。composite pk 只能被定
义为 table constraint
2、foreign key parent table 中的 primary key 中的 values 必须包含 child table 中所有的
values. share column 的 parent-child 关系
3、unique
4、check present in a static list of values permitted for the column.
5、not null 只能被定义在 column constraint。而且是一种内联约束,只能写在列的后面。
Zianed Version 1.1 2
3. 五种完整性约束:
Check、Unique、Primary Key、Foreign Key 类型可以通过 SELECT * FROM user_constraints
来查询;
NOT NULL 类型可以通过 DESC table_name 来进行查看,在 constraint_type 显示的是 C;
完整性约束是一种规则,不占用任何数据库空间。完整性约束始终存在数据字典中,在执行
SQL 或 PL/SQL 期间使用。
可以指明约束是启用、还是禁用;当约束启用时,增强了数据的完整性;否则,并不检查数
据完整性。
2、constraint的创建和管理
1)增加约束
LINARY@testjoe.us.oracle.com>ALTER TABLE emp ADD CONSTRAINT emp_chk_sal
CHECK(sal>=800);
2)删除约束
LINARY@testjoe.us.oracle.com>ALTER TABLE emp DROP CONSTRAINT emp_chk_sal;
Constraint 的四种状态(Constraint States):
The current status of an integrity constraint can be changed to any of the following 4 options using
the CREATE TABLE or ALTER TABLE statement.
- ENABLE(验证新增数据) ensure that all incoming data conforms to the constraint
- DISABLE(不验证新增数据) allow incoming data, regardless of whether it conforms to the
constraint
- VALIDATE(验证已存在数据) ensure that existing data conforms to the constraint
- NOVALIDATE(不验证已存在数据) existing data does not have to conform to the constraint
In addition:
ENABLE VALIDATE is the same as ENABLE. The constraint is checked and is guaranteed to
hold for all rows.
ENABLE NOVALIDATE means that the constraint is checked, but it does not have to be true
for all rows. This allows existing rows to violate the constraint, while ensuring that all new or
modified rows are valid.In an ALTER TABLE statement, ENABLE NOVALIDATE resumes
constraint checking on disabled constraints without first validating all data in the table.
DISABLE NOVALIDATE is the same as DISABLE. The constraint is not checked and is not
necessarily true.
DISABLE VALIDATE disables the constraint, drops the index on the constraint, and disallows
any modification of the constrained columns.
For a UNIQUE constraint, the DISABLE VALIDATE state enables you to load data efficiently
from a nonpartitioned table into a partitioned table using the EXCHANGE PARTITION clause of
the ALTER TABLE statement.
Zianed Version 1.1 3
4. Transitions between these states are governed by the following rules:
ENABLE implies VALIDATE, unless NOVALIDATE is specified.DISABLE implies
NOVALIDATE, unless VALIDATE is specified.VALIDATE and NOVALIDATE do not have any
default implications for the ENABLE and DISABLE states.
When a unique or primary key moves from the DISABLE state to the ENABLE state, if there
is no existing index, a unique index is automatically created. Similarly, when a unique or primary
key moves from ENABLE to DISABLE and it is enabled with a unique index, the unique index is
dropped.When any constraint is moved from the NOVALIDATE state to the VALIDATE state, all
data must be checked (this can be very slow). However, moving from VALIDATE to
NOVALIDATE simply forgets that the data was ever checked.Moving a single constraint from the
ENABLE NOVALIDATE state to the ENABLE VALIDATE state does not block reads, writes, or
other DDL statements. It can be done in parallel.
第一种ENABLED、VALIDATED
不检查存在的值,只检查新增加的值。
LINARY@testjoe.us.oracle.com>ALTER TABLE emp ADD CONSTRAINT emp_chk_sal
CHECK(sal>=800);
表已更改。
已用时间: 00: 00: 01.00
LINARY@testjoe.us.oracle.com>exec print_table('select * from user_constraints');
OWNER : LINARY
CONSTRAINT_NAME : EMP_CHK_SAL
CONSTRAINT_TYPE :C
TABLE_NAME : EMP
SEARCH_CONDITION : sal>=800
R_OWNER :
R_CONSTRAINT_NAME :
DELETE_RULE :
STATUS : ENABLED
DEFERRABLE : NOT DEFERRABLE
DEFERRED : IMMEDIATE
VALIDATED : VALIDATED
GENERATED : USER NAME
BAD :
RELY :
LAST_CHANGE : 18-10 月-2009 17:09:04
INDEX_OWNER :
INDEX_NAME :
INVALID :
VIEW_RELATED :
-----------------
Zianed Version 1.1 4