How to constrain a database table so only one row can have a particular value in a column?
问题 Using Oracle, if a column value can be 'YES' or 'NO' is it possible to constrain a table so that only one row can have a 'YES' value? I would rather redesign the table structure but this is not possible. [UDPATE] Sadly, null values are not allowed in this table. 回答1: Use a function-based index: create unique index only_one_yes on mytable (case when col='YES' then 'YES' end); Oracle only indexes keys that are not completely null, and the CASE expression here ensures that all the 'NO' values