开发者

MySQL Multiple foreign keys pointing to same table

开发者 https://www.devze.com 2023-02-06 17:52 出处:网络
I have a table called teamsand 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

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消