SQL can I have a “conditionally unique” constraint on a table?

后端 未结 5 741
时光说笑
时光说笑 2020-12-17 18:03

I\'ve had this come up a couple times in my career, and none of my local peers seems to be able to answer it. Say I have a table that has a \"Description\" field which is a

5条回答
  •  不思量自难忘°
    2020-12-17 18:39

    Oracle does. A fully null key is not indexed by a Btree in index in Oracle, and Oracle uses Btree indexes to enforce unique constraints.

    Assuming one wished to version ID_COLUMN based on the ACTIVE_FLAG being set to 1:

    CREATE UNIQUE INDEX idx_versioning_id ON mytable 
      (CASE active_flag WHEN 0 THEN NULL ELSE active_flag END,
       CASE active_flag WHEN 0 THEN NULL ELSE id_column   END);
    

提交回复
热议问题