我们数据的语义不区分大小写,因此我们将 oracle session 配置为不区分大小写:
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::无效标识符”,因此我认为不可能在 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 中运行良好,MSSQL 在处理排序规则方面具有更简单的方法。
当然,我们可以创建唯一索引而不是主键,但我想首先确保不支持执行此操作的正常方法。
我们的oracle版本是11.2.0.1。
最佳答案
在 11.2 上,您可以使用虚拟列来实现此目的:
CREATE TABLE SCHEMA_PROPERTY (
REAL_NAME nvarchar2(64) not null,
NAME generated always as (lower(real_name)) primary key,
VALUE nvarchar2(1024)
);
关于oracle - Oracle 中不区分大小写的主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7984289/