我想为列创建不区分大小写的唯一约束。但我无法找出确切的语法。
我尝试使用下面的 ex 查询,但出现语法错误。
alter table test_table_user add constraint user_name_unique unique (lower(user_name))
Cloud Spanner 要求您创建唯一索引而不是唯一约束。
GoogleSQL
不支持在表达式上创建索引,只支持在列上创建索引,但是您可以创建一个计算列,然后在该列上定义一个唯一索引。所以在你的例子中,解决方案是:
alter table test_table
add column user_name_lower string(max) as (lower(user_name));
create unique index idx_test_table_user_name_unique
on test_table user_name_lower;