How To Create A 'Two-Sided' Unique Index On Two Fields?

后端 未结 4 1044
醉话见心
醉话见心 2020-12-11 18:50

How can I efficiently create a unique index on two fields in a table like this: create table t (a integer, b integer);

where any unique combination of two different

4条回答
  •  渐次进展
    2020-12-11 19:14

    In Oracle you could use a function-based index like this:

    create unique index mytab_idx on mytab (least(a,b), greatest(a,b));
    

    I don't know mySQL, but maybe something similar is possible? For example, you could add 2 new columns leastab and greatestab to the table, with a trigger to maintain them with the values of least(a,b) and greatest(a,b) respectively, and then create a unique index on (leastab, greatestab).

提交回复
热议问题