Chinaunix首页 | 论坛 | 博客
  • 博客访问: 723684
  • 博文数量: 94
  • 博客积分: 1937
  • 博客等级: 上尉
  • 技术积分: 1618
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-04 18:46
个人简介

专注数据库开发管理

文章分类

全部博文(94)

文章存档

2015年(1)

2014年(2)

2013年(19)

2012年(32)

2011年(10)

2010年(30)

分类: Oracle

2012-10-27 09:54:14

在Oracle数据库中,关于约束的状态有下面两个参数:
initially (initially immediate 或 initially deferred)
deferrable(deferrable 或 not deferrable)
第1个参数,指定默认情况下,约束的验证时刻(在事务每条子句结束时,还是在整个事务结束时)。
第2个参数,指定了在事务中,是否可以改变上一条参数的设置。
如果不指定上述参数,默认设置是 initially immediate not deferrable。
注意:如果约束是not deferrable,那么它只能是initially immediate,而不能是initially deferred。

 

测试①,initially immediate:
SQL> create table nlist (
2 nid number
3 );

Table created

SQL> alter table nlist add constraint pk_nlist primary key (nid) initially immediate;

Table altered

SQL> insert into nlist values (1);

1 row inserted

SQL> insert into nlist values (1);

insert into nlist values (1)

ORA-00001: 违反唯一约束条件 (TEST.PK_NLIST)

测试②,initially deferred:
SQL> create table nlist (
2 nid number
3 );

Table created

SQL> alter table nlist add constraint pk_nlist primary key (nid) initially deferred;

Table altered

SQL> insert into nlist values (1);

1 row inserted

SQL> insert into nlist values (1);

1 row inserted

SQL> commit;

commit

ORA-02091: 事务处理已回退
ORA-00001: 违反唯一约束条件 (TEST.PK_NLIST)

测试③,initially immediate deferrable:
SQL> create table nlist (
2 nid number
3 );

Table created

SQL> alter table nlist add constraint pk_nlist primary key (nid) initially immediate deferrable;

Table altered

SQL> set constraint pk_nlist deferred;

Constraints set

SQL> insert into nlist values (1);

1 row inserted

SQL> insert into nlist values (1);

1 row inserted

SQL> commit;

commit

ORA-02091: 事务处理已回退
ORA-00001: 违反唯一约束条件 (TEST.PK_NLIST)

测试④,initially deferred deferrable:
SQL> create table nlist (
2 nid number
3 );

Table created

SQL> alter table nlist add constraint pk_nlist primary key (nid) initially deferred deferrable;

Table altered

SQL> set constraint pk_nlist immediate;

Constraints set

SQL> insert into nlist values (1);

1 row inserted

SQL> insert into nlist values (1);

insert into nlist values (1)

ORA-00001: 违反唯一约束条件 (TEST.PK_NLIST)

测试⑤:
SQL> create table nlist (
2 nid number
3 );

Table created

SQL> alter table nlist add constraint pk_nlist primary key (nid) initially deferred no deferrable;

alter table nlist add constraint pk_nlist primary key (nid) initially deferred no deferrable

ORA-01735: 无效的 ALTER TABLE 选项

阅读(847) | 评论(0) | 转发(0) |
0

上一篇:sqlplus基本命令总结

下一篇:监听手动注册

给主人留下些什么吧!~~