MySQL uniqueness in several fields

I am developing an application that should bring people together. Each person can correspond only to one other person. So, in the table below, I'm trying to make the values ​​from user1 and user2 unique in both fields:

CREATE TABLE `match_table` (
    `user1` int(11) NOT NULL,
    `user2` int(11) NOT NULL,
UNIQUE KEY `user2` (`user2`),
UNIQUE KEY `user1` (`user1`))

So, for example, the following INSERT statement should ignore lines 2 and 4. Or, at a minimum, I need to be able to mark those lines that need to be ignored. Note that line 5 is ok because lines 2 and 4 were ignored.

INSERT IGNORE INTO match_table (user1, user2)
VALUES
    (1,2),
    (2,3),
    (4,5),
    (6,4),
    (3,6)

Is there any index that can do this? .. Otherwise, there are some UPDATES that I could do after insertion that could mark those that I want to ignore?

+3
2

, , , :

INSERT IGNORE INTO match_table (user1, user2)
VALUES
(1,2),
(2,1),
(2,3),
(3,2),
(4,5),
(5,4),
(6,4),
(4,6),
(3,6),
(6,3)

, . :

SELECT * FROM match_table WHERE user1 < user2
1, 2
3, 6
4, 5

, , .

+3

, UNIQUE INDEX ('user1', 'user2')

0

Source: https://habr.com/ru/post/1756081/


All Articles