我正在创建一个简单的数据库并且我一直遇到此错误,ORA-02270:此列列表没有匹配的唯一或主键。我一直试图解决这个问题超过一天,我不明白我做错了什么。我查看过有关此错误的其他帖子,但不了解我在这里做错了什么。
创建没有任何问题的表是TEAMS,PLAYERS和GAMES表。当我尝试创建TEAM_GAME_STATS或PLAYER_GAME_STATS表时,我收到此错误。我注意到如果我从这两个表中删除外键约束,那么创建它们就没有问题;但我认为这可能会导致数据出现问题?如果有人能查看我的代码并告诉我可能做错了什么或者指出我正确的方向,我将不胜感激。我已经将表简化为主键/外键,因此没有过多的代码。
CREATE TABLE teams (
team VARCHAR2(50),
CONSTRAINT teams_pk PRIMARY KEY (team)
);
CREATE TABLE players (
player_id NUMBER,
team VARCHAR2(50),
CONSTRAINT players_pk PRIMARY KEY (player_id),
CONSTRAINT players_fk_team FOREIGN KEY (team)
REFERENCES teams (team)
);
CREATE TABLE games (
game_number NUMBER,
player_id NUMBER,
team VARCHAR2(50),
CONSTRAINT games_pk PRIMARY KEY (game_number, player_id, team),
CONSTRAINT games_fk_player_id FOREIGN KEY (player_id)
REFERENCES players (player_id),
CONSTRAINT games_fk_team FOREIGN KEY (team)
REFERENCES teams (team)
);
CREATE TABLE team_game_stats (
game_number NUMBER,
team VARCHAR2(50),
CONSTRAINT team_game_stats_pk PRIMARY KEY (game_number, team),
CONSTRAINT team_game_stats_fk_game_number FOREIGN KEY (game_number)
REFERENCES games (game_number),
CONSTRAINT team_game_stats_fk_team FOREIGN KEY (team)
REFERENCES games (team),
);
CREATE TABLE player_game_stats (
game_number NUMBER,
player_id NUMBER,
CONSTRAINT player_game_stats_pk PRIMARY KEY (game_number, player_id),
CONSTRAINT player_game_stats_fk_game_number FOREIGN KEY (game_number)
REFERENCES games (game_number),
CONSTRAINT player_game_stats_fk_player_id FOREIGN KEY (player_id)
REFERENCES games (player_id),
);
CREATE INDEX players_team_idx
ON players (team);
CREATE INDEX games_player_id_idx
ON games (player_id);
CREATE INDEX games_team_idx
ON games (team);
CREATE INDEX team_game_stats_game_number_idx
ON team_game_stats (game_number);
CREATE INDEX team_game_stats_team_idx
ON team_game_stats (team);
CREATE INDEX player_game_stats_game_number_idx
ON player_game_stats (game_number);
CREATE INDEX player_game_stats_player_id_idx
ON player_game_stats (player_id);
答案 0 :(得分:1)
外键是对另一个表的主键的引用。
Oracle SQL: Receiving 'no matching unique or primary key' error and don't know why
更改
CONSTRAINT games_pk PRIMARY KEY (game_number, player_id, team)
到
CONSTRAINT games_pk PRIMARY KEY (game_number, team)
同时更改
CONSTRAINT team_game_stats_fk_game_number FOREIGN KEY (game_number)
REFERENCES games (game_number),
CONSTRAINT team_game_stats_fk_team FOREIGN KEY (team)
REFERENCES games (team),
是
CONSTRAINT team_game_stats_game_fk FOREIGN KEY (game_number, team)
REFERENCES games (game_number, team)
并执行与player_game_stats类似的操作。
您可能需要摆弄每张桌子的钥匙。重要的是表中的外键应该包含与它引用的表的主键相同的列。