Search code examples
mysqldatabaseforeign-keysprimary-key

2 foreign keys referencing same table


Can I have two foreign keys in the same table that are referencing an other table named profil(same one) ?

my table is MailSent, it contains : primary key (Id), date, foreignkey1 (profil_sender), foreignkey2 (profil_receiver)


Solution

  • Add foreign keys (profil_sender_id, profil_receiver_id) to an existing table (MailSent), follow the following steps:

    ALTER TABLE MailSent ADD CONSTRAINT fk_profile_sender_id FOREIGN KEY (profil_sender_id) REFERENCES TABLE-NAME(id);
    
    ALTER TABLE MailSent ADD CONSTRAINT fk_profil_receiver_id FOREIGN KEY (profil_receiver_id) REFERENCES TABLE-NAME(id);