管理数据完整性一 学习目标1.实现数据完整性约束2.管理完整性约束3.从数据字典中获取约束信息二 保证数据完整性的方法1.应用程序代码控制2.触发器控制3.声明完整性约束三 约束的类型 (见图)1.not null (不能为空)2.unique (值必须唯一)3.primary key (not null + unique)4.foreign key (该表值必须在外键表中存在)5.check (自己加的条件)6.ref (不熟)注:Constraints不但可以建立在Table上,也可以建立在View上。四 约束状态1.disable novalidate 既不会约束新增数据也不会验证已有数据,等同于disable2.disable validate 约束新增数据但不会验证已有数据,启用后禁止DML3.enable novalidate 约束新增数据但不会验证已有数据4.enable validate 约束新增数据并验证已有数据,等同于enable下面举例说明:
- SQL> create table dept2 as select * from scott.dept;
-
- Table created
-
- SQL> select * from dept2 order by deptno;
-
- DEPTNO DNAME LOC
- ------ -------------- -------------
- 10 ACCOUNTING NEW YORK
- 20 RESEARCH DALLAS
- 30 SALES CHICAGO
- 40 OPERATIONS BOSTON
-
- SQL> alter table dept2 add constraint dept2_u1 unique(deptno);
-
- Table altered
-
- SQL> select index_name,table_name,uniqueness from dba_indexes where index_name = "DEPT2_U1";
-
- INDEX_NAME TABLE_NAME UNIQUENESS
- ------------------------------ ------------------------------ ----------
- DEPT2_U1 DEPT2 UNIQUE
-
- SQL> select constraint_name,status,validated from dba_constraints where constraint_name = "DEPT2_U1";
-
- CONSTRAINT_NAME STATUS VALIDATED
- ------------------------------ -------- -------------
- DEPT2_U1 ENABLED VALIDATED
-
- SQL> insert into dept2(deptno) values(10);
-
- insert into dept2(deptno) values(10)
-
- ORA-00001: unique constraint (SYSTEM.DEPT2_U1) violated
-
- SQL> select * from dept2 order by deptno;
-
- DEPTNO DNAME LOC
- ------ -------------- -------------
- 10 ACCOUNTING NEW YORK
- 20 RESEARCH DALLAS
- 30 SALES CHICAGO
- 40 OPERATIONS BOSTON
-
- SQL> alter table dept2 modify constraint dept2_u1 disable novalidate;
-
- Table altered
-
- SQL> select index_name,table_name,uniqueness from dba_indexes where index_name = "DEPT2_U1";
-
- INDEX_NAME TABLE_NAME UNIQUENESS
-
- ------------------------------ ------------------------------ ----------
-
- (disable自动移除索引)
-
-
- SQL> select constraint_name,status,validated from dba_constraints where constraint_name = "DEPT2_U1";
-
- CONSTRAINT_NAME STATUS VALIDATED
- ------------------------------ -------- -------------
- DEPT2_U1 DISABLED NOT VALIDATED
-
- SQL> insert into dept2(deptno) values(10);
-
- 1 row inserted
-
- SQL> select * from dept2 order by deptno;
-
- DEPTNO DNAME LOC
- ------ -------------- -------------
- 10 ACCOUNTING NEW YORK
- 10
- 20 RESEARCH DALLAS
- 30 SALES CHICAGO
- 40 OPERATIONS BOSTON
-
- SQL> alter table dept2 modify constraint dept2_u1 enable novalidate;
-
- alter table dept2 modify constraint dept2_u1 enable novalidate
-
- ORA-02299: cannot validate (SYSTEM.DEPT2_U1) - duplicate keys found
- (因为enable会去创建唯一性索引,而已有数据deptno存在重复数据10,所以这里不能enable)
-
- SQL> delete from dept2 where deptno=10 and dname is null;
-
- 1 row deleted
-
- SQL> alter table dept2 modify constraint dept2_u1 enable novalidate;
-
- Table altered
-
- SQL> select index_name,table_name,uniqueness from dba_indexes where index_name = "DEPT2_U1";
-
- INDEX_NAME TABLE_NAME UNIQUENESS
- ------------------------------ ------------------------------ ----------
-
- DEPT2_U1 DEPT2 UNIQUE
-
- (enable会自动创建唯一性索引)
-
-
- SQL> select constraint_name,status,validated from dba_constraints where constraint_name = "DEPT2_U1";
-
- CONSTRAINT_NAME STATUS VALIDATED
- ------------------------------ -------- -------------
- DEPT2_U1 ENABLED NOT VALIDATED
-
- SQL> insert into dept2(deptno) values(10);
-
- insert into dept2(deptno) values(10)
-
- ORA-00001: unique constraint (SYSTEM.DEPT2_U1) violated
-
- SQL> alter table dept2 modify constraint dept2_u1 disable validate;
-
- Table altered
-
- SQL> select index_name,table_name,uniqueness from dba_indexes where index_name = "DEPT2_U1";
-
- INDEX_NAME TABLE_NAME UNIQUENESS
- ------------------------------ ------------------------------ ----------
-
- SQL> select constraint_name,status,validated from dba_constraints where constraint_name = "DEPT2_U1";
-
- CONSTRAINT_NAME STATUS VALIDATED
- ------------------------------ -------- -------------
- DEPT2_U1 DISABLED VALIDATED
-
- SQL> insert into dept2(deptno) values(10);
-
- insert into dept2(deptno) values(10)
-
-
- ORA-25128: No insert/update/delete on table with constraint (SYSTEM.DEPT2_U1) disabled and validated
-
- (disable validate后禁止DML)
-
-
- SQL> select * from dept2 order by deptno;
-
- DEPTNO DNAME LOC
- ------ -------------- -------------
- 10 ACCOUNTING NEW YORK
- 20 RESEARCH DALLAS
- 30 SALES CHICAGO
- 40 OPERATIONS BOSTON
-
- SQL> alter table dept2 modify constraint dept2_u1 enable validate;
-
- Table altered
-
- SQL> select index_name,table_name,uniqueness from dba_indexes where index_name = "DEPT2_U1";
-
- INDEX_NAME TABLE_NAME UNIQUENESS
- ------------------------------ ------------------------------ ----------
- DEPT2_U1 DEPT2 UNIQUE
-
- SQL> select constraint_name,status,validated from dba_constraints where constraint_name = "DEPT2_U1";
-
- CONSTRAINT_NAME STATUS VALIDATED
- ------------------------------ -------- -------------
- DEPT2_U1 ENABLED VALIDATED
-
- SQL> insert into dept2(deptno) values(10);
-
- insert into dept2(deptno) values(10)
-
- ORA-00001: unique constraint (SYSTEM.DEPT2_U1) violated
-
- SQL> select * from dept2 order by deptno;
-
- DEPTNO DNAME LOC
- ------ -------------- -------------
- 10 ACCOUNTING NEW YORK
- 20 RESEARCH DALLAS
- 30 SALES CHICAGO
- 40 OPERATIONS BOSTON
Google地图是如何用Oracle进行图片的存储的熟悉掌握Oracle SQL语言相关资讯 Oracle约束
- Oracle约束的分类 (03/17/2015 19:28:41)
- Oracle约束Constraint对于CBO优化 (05/05/2014 19:49:38)
- Oracle数据完整性约束:主键、外键 (10/27/2013 16:56:40)
| - Oracle外键约束(Foreign Key)的 (12/06/2014 14:41:09)
- Oracle数据库中由于没有做任何约束 (04/01/2014 17:27:56)
- Oracle数据库的字段约束创建和维护 (10/02/2013 19:31:45)
|
本文评论 查看全部评论 (0)