首页 文章

Oracle中不区分大小写的主键

提问于
浏览
3

我们的数据的语义不区分大小写,因此我们将oracle会话配置为不区分大小写:

alter session set NLS_COMP=LINGUISTIC;
alter session set NLS_SORT=BINARY_AI;

然后,为了利用索引,我们还希望主键也不区分大小写:

create table SCHEMA_PROPERTY (
  NAME  nvarchar2(64)   not null,
  VALUE nvarchar2(1024),
  constraint SP_PK primary key (nlssort(NAME))
)

但是,这会遇到“ORA-00904 :: invalid identifier”,所以我假设不可能在PK定义中使用nlssort()函数 .

下一次尝试是将不区分大小写的唯一索引与主键相关联:

create table SCHEMA_PROPERTY (
  NAME  nvarchar2(64) primary key using index (
      create unique index SP_UQ on SCHEMA_PROPERTY(nlssort(NAME))),
  VALUE nvarchar2(1024)
);

但这也失败了:

Error: ORA-14196: Specified index cannot be used to enforce the constraint.
14196. 00000 -  "Specified index cannot be used to enforce the constraint."
*Cause:    The index specified to enforce the constraint is unsuitable
           for the purpose.
*Action:   Specify a suitable index or allow one to be built automatically.

我是否应该断定Oracle不支持PK约束的不区分大小写的语义?这在MSSQL中工作正常,它在处理排序规则时有一个更简单的方法 .

当然,我们可以创建一个唯一的索引而不是主键,但我想首先确保不支持正常的方法 .

我们的oracle版本是11.2.0.1 .

1 回答

  • 4

    在11.2上,您可以使用虚拟列来实现此目的:

    CREATE TABLE SCHEMA_PROPERTY (
       REAL_NAME  nvarchar2(64) not null,
       NAME       generated always as (lower(real_name)) primary key,
       VALUE nvarchar2(1024)
    );
    

相关问题