Terry
Terry

Reputation: 5262

Multiple foreign keys in one table to one other table in mysql

I got two tables in my database: user and call. User exists of 3 fields: id, name, number and call : id, 'source', 'destination', 'referred', date.

I need to monitor calls in my app. The 3 ' ' fields above are actually userid numbers.

Now I'm wondering, can I make those 3 field foreign key elements of the id-field in table user?

Upvotes: 2

Views: 6386

Answers (3)

user3989300
user3989300

Reputation: 1

Alter Table call

ADD FOREIGN KEY (Sourceid) references Source(Id),

FOREIGN KEY (DesId) references Destination(Id)

Upvotes: 0

Hueso Azul
Hueso Azul

Reputation: 76

Something alike should do the work:

ALTER TABLE call 
ADD CONSTRAINT fk_call_source_user FOREIGN KEY (source) 
REFERENCES user (id)

ALTER TABLE call 
ADD CONSTRAINT fk_call_destination_user FOREIGN KEY (destination) 
REFERENCES user (id) 

ALTER TABLE call 
ADD CONSTRAINT fk_call_referred_user FOREIGN KEY (referred) 
REFERENCES user (id)

Upvotes: 3

Stefan Gehrig
Stefan Gehrig

Reputation: 83622

Yes - you can ;-)

Just define all three foreign keys to refer to the id column in User.

Upvotes: 3

Related Questions