2
Hello, I want to do the following: Create in Mysql the n-to-n relationship where men have friendships with women and women also have friendships with men.
Example:
John has friendship with Fernanda, but, Fernanda has no friendship with John rather with Carlos, but John continues to have friendship with Fernanda.
I created a n-to-n model where men befriend women, but couldn’t make women befriend men and vice versa.
See my SQL script below:
create database pessoas;
use pessoas;
create table homens (id int auto_increment, nome varchar(10), primary key (id));
create table mulheres (id int auto_increment, nome varchar(10), primary key (id));
create table amizades (id int auto_increment, homem_id int, mulher_id int,
primary key(id),
foreign key(homem_id) references homens(id),
foreign key(mulher_id) references mulheres(id));
insert into homens values
(default, 'Joao'),
(default, 'Flavio'),
(default, 'Carlos');
insert into mulheres values
(default,'Ana'),
(default,'Fernanda'),
(default,'Julia');
insert into amizades values
(default, 1, 2),
(default, 2, 1),
(default, 3, 3);
/* join com 3 tabelas, aparecer o id e nome do homem e depois o id e nome da mulher de quem o homem é amigo */
select homens.id, homens.nome, mulheres.id, mulheres.nome from homens join amizades on homens.id = amizades.homem_id
join mulheres on amizades.mulher_id = mulheres.id order by homens.id;
I want an explicit relationship in the database so I know who has friendship with whom and not just simulate that relationship by changing the order of the columns in the SELECT
, understood? What better way to do this?
Make a table only PERSON (ID, NAME, SEX) other FRIENDSHIP (ID_DE,ID_PARA) and by TRIGGER test if the sexes are different
– Motta
More I want to know regarding n-to-n something explicit and not testing the sexes.
– Bruno
just did not see the need for two tables, the relationship would be maintained.
– Motta
@Motta Valeu ai
– Bruno