MySQL是否有任何关键约束来处理唯一配对?

时间:2016-12-20 00:16:57

标签: mysql unique-constraint

给定一个记录表,每个记录都有一个唯一的ID,我想要另一个表,它可以在第一个表中存储唯一的记录对。

第二个表显然需要一个复合唯一键,但它似乎也需要一个额外的约束:其中一个键总是小于另一个键。这样,第二个表永远不会将第一个表中的任何记录与自身配对,也可以避免为相同的两个记录创建两个单独的配对,只是因为键出现了相反的方式。

鉴于第二个表“配对”,包含以下值:

 key1 key2
  1    2
  1    3
  2    3

我想要以下行为:

INSERT pairing SET key1=2, key2=1;
=> Duplicate key error

INSERT pairing SET key1=2, key2=2;
=> Invalid key error


INSERT pairing SET key1=4, key2=3;
[could give:]
=> Invalid key error

[but in fact ideally it would insert a record with:]
=> key1  key2
    3     4

并且在最后一个案例中,也欢迎同样灵巧的SELECT查询:

SELECT * FROM pairing WHERE key1=4 AND key2=3;
=> key1  key2
    3     4

但是,我不会惊讶地发现MySQL不提供这种功能!

尽管如此,这似乎是一个常见的要求,所以我想知道是否有人能告诉我任何技术来获得我想要的结果,尽可能少依赖外部编程。

1 个答案:

答案 0 :(得分:2)

不幸的是CHECK Constraints在mysql中还没有,所以唯一的解决方案似乎是给我们一个触发器。

CREATE TRIGGER pairing_check
BEFORE INSERT ON pairing
FOR EACH ROW
BEGIN
    IF NEW.key1 = NEW.key2 THEN
        SIGNAL 'Two keys should be different'set @
    END IF 
    /* We can't try an insert and then capture the exception because
"A stored function or trigger cannot modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger." */
    IF (SELECT COUNT(*) FROM paring where key1=NEW.key1 and key2=NEW.key2) == 1 THEN
        SET @tmp = NEW.key1;
        NEW.key1 = NEW.key2;
        NEW.key2 = @tmp;
    END IF;
END

即便如此,由于mysql triggers have lots of restrictions on them,你很难满足全部要求。