我有一张mySql表:
create table associations
(email1 varchar(30),
email2 varchar(30),
primary key(email1, email2));现在,在这个表中,如果我按如下方式插入两行:
insert into associations values('a@abc.com','b@abc.com');
insert into associations values('b@abc.com','a@abc.com');这些在数据库中是可以接受的。我想做的是,它们不应该被数据库接受,因为,我只对键的组合感兴趣,而不是对它们的 order感兴趣。
mysql中是否有任何内置的功能/关键字/hack(或者其他数据库)允许我这样做?
发布于 2014-05-05 06:28:16
这对我有用,我想出了一个扳机:
mysql> create trigger insert_check_associations before insert on associations
-> for each row
-> begin
-> if new.email1>new.email2 then
-> set @a = new.email2;
-> set new.email2 = new.email1;
-> set new.email1 = @a;
-> end if;
-> end;//查询确定,0行受影响(0.07秒)
此触发器按递增顺序交换两个值。
发布于 2014-04-29 21:47:10
在支持check约束的RDBMS中,可以强制两个条目按特定顺序显示。*这确保您所描述的类型的复制不会发生,因为两个订单中的一个将被拒绝。不幸的是,MySQL没有提供检查约束,尽管对this question的公认答案提出了一种使用触发器实现类似结果的方法。
*这是假设您对列数据类型有一个总的顺序,在本例中是这样的。
https://stackoverflow.com/questions/23375765
复制相似问题