gpt4 book ai didi

sql - Oracle SQL唯一约束A到B,B到A

转载 作者:行者123 更新时间:2023-12-04 13:33:06 25 4
gpt4 key购买 nike

我的 table 有点像

从到远
------ ---- ----------
A B 100
B C 100
Z A 120

我需要向表中添加一个不允许插入的约束B A 100C B 100A Z 120
我有一个PRIMARY KEY (from, to)CONSTRAINT const_u UNIQUE (to, from),但无法正常工作。

编辑:
我也无法强行按字母顺序排列正确顺序,因为我无权访问插入逻辑
编辑#2:您可以添加B A 50吗? -不,你不能。从A到B或从B到A应该只有一个唯一的距离,但不能同时有两个唯一的距离。

最佳答案

create unique index on the_table (least(from,to), greatest(from,to));

如果表(A,B,100)中已经存在,将阻止添加(B,A,100)。

关于sql - Oracle SQL唯一约束A到B,B到A,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15731227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com