对于具有char语义的列,Oracle是否支持默认值?

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)

其他信息:

  1. 因为我明确地设置了单位(即char(1 char)而不是char(1)),所以我不认为nls_length_semanatics的值是相关的.
  2. 将列添加为char(1个字符)后,新添加的"N"实际上是"N",我不确定额外的空格是什么.
  3. 将列添加为char(1个字节)按预期工作;
  4. 添加没有"default"N'not null"的列,然后将所有现有行更新为"N",然后将列更改为"not null"也可以按预期工作.
  5. NLS_CHARACTERSET是AL32UTF8,但我不认为这也是相关的.
  6. 数据库是Oracle 11g; 11.2.0.1.0.

谢谢.

Jus*_*ave 5

我相信你所看到的是一个依赖于几个不同事物相互作用的错误

  • 首先,数据库字符集必须是可变宽度字符集(即AL32UTF8),以便单个字符可能需要最多四个字节的存储空间.
  • 其次,必须使用字符长度语义声明该列
  • 第三,在11.1开始,Oracle添加了优化,这样,如果你添加一列被声明表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)

  • 另请参阅上面的示例和评论.这显然是11.2.0.1.0中的一个错误,并在11.2.0.2.0中修复. (3认同)

Mar*_*bak 2

您标记了 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)

那么......也许您的版本有错误?

希望有帮助。

  • @Mark J. Bobak - 你的数据库字符集是什么?我敢打赌,这只发生在具有可变宽度字符集(即“AL32UTF8”)的数据库中。 (3认同)