how to set unique constraint over multiple column when any one can be null in sqlite

前端 未结 3 1317
情歌与酒
情歌与酒 2020-12-18 11:48

How do I set unique constraint over multiple column when any one can be null in sqlite?

e.g. I have made unique(\"col1\",\"col2\",\"col3\") and tried with inse

3条回答
  •  一生所求
    2020-12-18 12:44

    Starting with version 3.9.0 (2015-10-14) you can use indexes on expressions (https://www.sqlite.org/expridx.html) and use for example the COALESCE function to transform null values into some form of fallback value:

    CREATE UNIQUE INDEX IX_Unique ON Table1 (
        COALESCE(col1, ""), 
        COALESCE(col2, ""), 
        COALESCE(col3, "")
    );
    

提交回复
热议问题