我想使用 Liquibase 向列添加唯一约束。当然,我想使用前提条件检查是否存在重复行。
我想出了这个:
<preConditions>
<sqlCheck expectedResult="0">
select count(*)
from person
having ( count(username) > 1 )
</sqlCheck>
</preConditions>
但是,这会在 MySQL 和其他数据库上产生 Empty set
。
我尝试使用 expectedResult=""
和 expectedResult="null"
但两者都不起作用。
最佳答案
你总是可以强制一个结果:
select
case when exists(
select username, count(*)
from person
group by username
having count(*) > 1 )
then 1
else 0
end
这也允许通过/拥有一个更正常的组
关于sql - 唯一约束的 Liquibase 前提条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14325050/