【问题标题】:create unique index with function in hsqldb / liquibase在 hsqldb / liquibase 中使用函数创建唯一索引
【发布时间】:2018-05-27 10:20:55
【问题描述】:

有没有办法在 hsqldb 中使用函数索引?

我尝试了这 4 个:

<column name="LOWER(name)"/>
<column name="LCASE(name)"/>
<column name="LOWER(name)" computed="true"/>
<column name="LCASE(name)" computed="true"/>

在我的 createIndex 变更集中:

<changeSet author="dgt" id="unique-postgres" dbms="hsqldb">
    <createIndex indexName="lower_case_index" tableName="users" unique="true">
        <column name="LOWER(name)" computed="true"/>
    </createIndex>

在文档中,我注意到 hsqldb 得到:LOWER 和 LCASE 内置函数,但其​​中任何一个都不适合我。

每次我遇到错误时:

原因:liquibase.exception.DatabaseException:意外令牌:( 必需:) [失败的 SQL:创建唯一索引 PUBLIC.lower_case_index ON PUBLIC.users(LOWER(name))]

我知道一个解决方案,我可以将列类型从 VARCHAR 更改为 VARCHAR_IGNORECASE,但对我来说不是这种情况,因为我需要一个解决方案来同时处理 db:hsqldb 和 postgres。

我理想的解决方案应该是这样的:

<changeSet author="dgt" id="users-unique-index-postgres" dbms="hsqldb">
    <createIndex indexName="name_index" tableName="users" unique="true">
        <column name="LOWER(name)" computed="true"/>
    </createIndex>
</changeSet>
<changeSet author="dgt" id="users-unique-index-hsqldb" dbms="postgresql">
    <createIndex indexName="name_index" tableName="users" unique="true">
        <column name="lower(name)" computed="true"/>
    </createIndex>
</changeSet>

但它不起作用。

【问题讨论】:

    标签: postgresql hsqldb liquibase


    【解决方案1】:

    HSQLDB 根本不支持基于函数的索引,所以你需要寻找不同的解决方案。你可以例如将该列定义为varchar_ignorecase 而不是varchar,然后在该列上创建一个“正常”唯一索引。

    您可以使用属性保留单个表定义。

    可能是这样的:

    <changeSet author="dgt" id="create-users-table">
      <property name="users_name_type" value="varchar" dbms="postgresql"/>
      <property name="users_name_type" value="varchar_ignorecase" dbms="hsqldb"/>
    
      <createTable tableName="users">
        <column name="name" type="${users_name_type}">
          <constraints nullable="false"/>
        </column>
      </createTable>
    </changeSet>
    
    
    <changeSet author="dgt" id="users-unique-index-postgres" dbms="postgresql">
      <createIndex indexName="name_index" tableName="users" unique="true">
        <column name="lower(name)" computed="true"/>
      </createIndex>
    </changeSet>
    
    <changeSet author="dgt" id="users-unique-index-hsqldb" dbms="hsqldb">
      <createIndex indexName="name_index" tableName="users" unique="true">
        <column name="name"/>
      </createIndex>
    </changeSet>
    

    【讨论】:

    • 检查这个:liquibase.jira.com/browse/CORE-2179 和另一个:liquibase.jira.com/browse/CORE-2107。您确定不支持创建基于函数的索引吗?
    • @degath:啊,谢谢,我不知道。但它不会改变 HSQLDB 的方法
    • 所以看起来我可以像我关于 postgres 的示例中那样做,但仍然需要为 hsqldb 使用 varchar ignorecase?
    • @degath: 正确(或者您提交 HSQLDB 补丁以支持基于函数的索引)
    • “HSQLDB 根本不支持基于函数的索引,所以你需要找到不同的解决方案” @degath 和其他读者我知道这是老问题和答案。 HSQLDB 2.0 有一个比这更好的选项,它被称为生成的列,您可以对其进行索引。我相信生成的列在 2010 - 2012 年左右受支持。在 create table 或 alter 命令中或多或少 &lt;column_name&gt; GENERATED ALWAYS AS (UPPER(name)) 然后你可以简单地索引列正常。
    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多