I have a table called teams and a table called games. teams has id开发者_JAVA百科, 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
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);
First Read This:
FOREIGN KEY Constraints
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;
精彩评论