2013-01-14 85 views
3

我想使用Liquibase爲列添加唯一約束條件。當然,我想檢查是否存在使用前提條件的重複行。Liquibase唯一約束的前提條件

我想出了這一點:

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

然而,這產生Empty set對MySQL和可能其他數據庫。

我試過使用expectedResult=""expectedResult="null"但都不起作用。

回答

5

你總是可以迫使結果:

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

這也讓更多的普通組由/有

+0

感謝,適用於MySQL的。這是否適用於其他數據庫,如MSSQL/Oracle/HSQL? – siebz0r

+0

這個查詢將適用於所有數據庫(我是這樣編寫的,因爲你沒有用特定的數據庫標記你的問題) – Bohemian

+0

超級!真的很感激它;-) – siebz0r