3

我想使用 Liquibase 向列添加唯一约束。当然,我想使用前提条件检查是否存在重复行。

我想出了这个:

<preConditions>
    <sqlCheck expectedResult="0">
        select count(*)
        from person
        having ( count(username) > 1 )
    </sqlCheck>
</preConditions>

然而,这会Empty set在 MySQL 和可能的其他数据库上产生。

我尝试使用expectedResult=""expectedResult="null"但两者都不起作用。

4

1 回答 1

7

你总是可以强制一个结果:

select 
  case when exists(
    select username, count(*)
    from person
    group by username
    having count(*) > 1 )
    then 1
    else 0
  end

这也允许通过/拥有一个更正常的组

于 2013-01-14T19:29:46.913 回答