问题
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?
回答1:
Yes - you can ;-)
Just define all three foreign keys to refer to the id column in User.
回答2:
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)
回答3:
Alter Table call
ADD FOREIGN KEY (Sourceid) references Source(Id),
FOREIGN KEY (DesId) references Destination(Id)
来源:https://stackoverflow.com/questions/2453731/multiple-foreign-keys-in-one-table-to-one-other-table-in-mysql