Skip to content
Advertisement

Can I use two values of a foreign key in on table?

I have a ‘users’ table, and there is also a table ‘alerts’. And in the ‘alert’ table, I want to use ‘users.id’ two times representing two different users of a ‘users’ table as a foreign keys in ‘alert’ table.

Is it possible? if not then what will be the alternate solution? Kindly help me. Thanks in advance 🙂

Advertisement

Answer

You would express this as:

create table alerts (
    . . . ,
    user_id_from int,
    user_id_to int,
    foreign key (alerts_user_id_from) references users(user_id),
    foreign key (alerts_user_id_to) references users(user_id)
);

I just made up the column names to give you an example of how it would work.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement