我想要实现的是:
CREATE TABLE foo (
bar1 int,
bar2 int,
type varchar(16),
FOREIGN KEY bar1 REFERENCES bar(id),
FOREIGN KEY bar2 REFERENCES bar(id)
);
ALTER TABLE foo ADD CONSTRAINT unique_link (bar1, bar2);
INSERT INTO foo (bar1, bar2, type) VALUES (1, 2, 'simple link')
--next line should fail, as {2, 1} as a set is already present, just in a different order
INSERT INTO foo (bar1, bar2, type) VALUES (2, 1, 'simple link')
有可能吗?也许有一些触发器,或者一些自动计算的 bar* 列的哈希值?谢谢您的帮助!
是的,您可以为此创建一个唯一索引: