简体   繁体   中英

Unique constraint on two columns regardless of order - Oracle

disclaimer: I have read the similar questions, it is different.

We have to create a flight table with 'DEPART' and 'ARRIVE' entries (that are 3 long chars (flight from LON to LAX) and distance, and other entries etc. The main constraint is, that the DEPART and ARRIVE pairs should be unique regardless of their order (direction does not matter). What I know I can do but we are not allowed to (so do not suggest):

  • check DEPART > ARRIVE and then check UNIQUE
  • Make the DEPART and ARRIVE primary keys (neither together nor alone, though it would not solve it)
  • Reference them from other tables, where they are keys.

So how can I check if a pair exist in the table or not? I gotta say it is not for an industrial task, it is for a university subject. We use Oracle SQLDeveloper for the task. Thank you in advance! Regards.

Oracle supports indexes on expressions, so:

create index unq_t_from_to on t(least(from, to), greatest(from, to))

(Of course, from and to are keywords in SQL so they are not very good for column names.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM