Representation of many-to-many self-relationship

0

I am creating an Entity-Relationship Model in which a Student can have many Students, thus characterizing many-to-many self-relationship.

However, I am in doubt as to how to implement this self-relationship at the time of the bank's creation. Should I create the Student table and a second table having two Student keys?

If it helps, I'm doing this relationship to identify which friends a Student has added into the system.

    
asked by anonymous 05.05.2018 / 23:07

1 answer

1

Yes, the way to represent a many-to-many relationship is to break it into an intermediary table. For example in MySQL we would do:

create table a(
  id int primary key auto_increment,
  text varchar(60)
);

create table b(
  id int primary key auto_increment,
  text varchar(60)
);

Now the relationship of the tables

create table a_b(
  id int primary key auto_increment,
  a_id int,
  b_id int
  FOREIGN KEY (a_id) REFERENCES a(id),
  FOREIGN KEY (b_id) REFERENCES b(id)
);
    
05.05.2018 / 23:28