Ben*_*all 5 oracle ddl oracle11g
我试图将一个列添加到表中,但使用DEFAULT子句获得了惊人的效果.在包含现有行的表中,我添加了一个新列,如下所示:
alter table t add c char(1 char) default 'N' not null;
Run Code Online (Sandbox Code Playgroud)
当我随后向表中添加检查约束时,它失败了:
alter table t add constraint chk check(c in ('N', 'Y'));
Run Code Online (Sandbox Code Playgroud)
结果导致了
ERROR at line 1:
ORA-02293: cannot validate (T.CHK) - check constraint violated.
Run Code Online (Sandbox Code Playgroud)
其他信息:
谢谢.
我相信你所看到的是一个依赖于几个不同事物相互作用的错误
AL32UTF8),以便单个字符可能需要最多四个字节的存储空间.NOT NULL和具有DEFAULT甲骨文能做到这一点简单地通过更新数据字典,而不是实际存储的每一行中的默认值表. 当这两件事都成立时,看来返回的值的长度为4,并用CHR(0)字符填充.
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 64-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> create table foo( col1 number );
Table created.
SQL> insert into foo values( 1 );
1 row created.
SQL> commit;
Commit complete.
SQL> alter table foo add c char(1 char) default 'N' not null;
Table altered.
SQL> alter table foo add constraint chk_foo check( c in ('Y', 'N') );
alter table foo add constraint chk_foo check( c in ('Y', 'N') )
*
ERROR at line 1:
ORA-02293: cannot validate (SCOTT.CHK_FOO) - check constraint violated
SQL> select c, dump(c) from foo;
C DUMP(C)
---- ------------------------------
N Typ=1 Len=4: 78,0,0,0
Run Code Online (Sandbox Code Playgroud)
如果您实际强制将值存储在表中,您将获得没有CHR(0)填充的预期行为.因此,如果我在表中插入一个新行,它就会通过.
SQL> insert into foo(col1) values (2);
1 row created.
SQL> select c, dump(c) from foo;
C DUMP(C)
---- ------------------------------
N Typ=1 Len=4: 78,0,0,0
N Typ=1 Len=1: 78
Run Code Online (Sandbox Code Playgroud)
您还可以发出UPDATE更新未实际存储表的行中的值的行
SQL> update foo
2 set c = 'N'
3 where c != 'N';
1 row updated.
SQL> select c, dump(c) from foo;
C DUMP(C)
---- ------------------------------
N Typ=1 Len=1: 78
N Typ=1 Len=1: 78
Run Code Online (Sandbox Code Playgroud)
您标记了 oracle11g,但没有指定版本。
这适用于 Linux x86-64 上的 11.2.0.2。
SQL*Plus: Release 11.2.0.2.0 Production on Mon Mar 26 13:13:52 2012
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management and OLAP options
SQL> create table tt(a number);
Table created.
SQL> insert into tt values (1);
1 row created.
SQL> commit;
Commit complete.
SQL> alter table tt add c char(1 char) default 'N' not null;
Table altered.
SQL> alter table tt add constraint chk check(c in('N','Y'));
Table altered.
SQL> select * from tt;
A C
---------- -
1 N
SQL> column dump(c) format a30
SQL> select c, length(c),dump(c) from tt;
C LENGTH(C) DUMP(C)
- ---------- ------------------------------
N 1 Typ=96 Len=1: 78
Run Code Online (Sandbox Code Playgroud)
那么......也许您的版本有错误?
希望有帮助。