my problem is quite simple, but I canβt put words into it. I have a user table with identifiers for each of them.
I want to create a friendship table, but I do not want to have duplicate entries. This does not mean:
id_user1 | id_user2
---------|----------
2 | 3
3 | 2
Am I clear enough?
At the moment, I have this for creating a table:
CREATE TABLE User(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
surname VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE Friends(
id_user1 INT NOT NULL,
id_user2 INT NOT NULL,
PRIMARY KEY(id_user1, id_user2),
FOREIGN KEY (id_user1) REFERENCES User(id),
FOREIGN KEY (id_user2) REFERENCES User(id),
);
[EDIT 1:] Maybe it's best to save each entry twice? Both ways?
source
share