Reputation: 47
I have a table called teams and a table called games. teams has id, name, ..... the games has id, hteam_id, vteam_id, loc, .... I want the hteam_id and vteam_id to each be foreign key into the teams table. How do you do it
Upvotes: 4
Views: 6624
Reputation: 99911
You can add the two foreign key using this:
alter table game add foreign key game_hteam_id(hteam_id) references teams(id)
, add foreign key game_vteam_id(vteam_id) references teams(id);
Upvotes: 5
Reputation: 42093
First Read This:
Example:
CREATE TABLE parent (id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
Upvotes: 2