简体   繁体   English

两列的唯一约束,与顺序无关-Oracle

[英]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): 我们必须使用“ DEPART”和“ ARRIVE”条目(这是3个长字符(从LON到LAX的飞行)和距离)以及其他条目创建一个飞行表。主要约束是,DEPART和ARRIVE对应为唯一的,与顺序无关(方向无关紧要)。我知道我可以做,但是我们不允许这样做(所以不建议这样做):

  • check DEPART > ARRIVE and then check UNIQUE 检查DEPART> ARRIVE,然后检查UNIQUE
  • Make the DEPART and ARRIVE primary keys (neither together nor alone, though it would not solve it) 设置DEPART和ARRIVE主键(虽然不能解决,但不能同时使用或单独使用)
  • 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. 我们使用Oracle SQLDeveloper来完成任务。 Thank you in advance! 先感谢您! Regards. 问候。

Oracle supports indexes on expressions, so: Oracle支持表达式索引,因此:

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.) (当然,SQL中的fromto是关键字,因此它们对于列名不是很好。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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