简体   繁体   English

添加外键约束时遇到问题(错误 ORA-02270:此列列表没有匹配的唯一键或主键)

[英]Trouble adding Foreign Key Constraint (error ORA-02270: no matching unique or primary key for this column-list)

I am learning how to create a small and simple database for class with Primary Key and Foreign Key restraints.我正在学习如何使用主键和外键限制为 class 创建一个小而简单的数据库。 I am getting ORA-02770 when attempting to run my ALTER TABLE statement, which as I understand is notifying me that the column I am referencing in the outside table is not listed as a primary key constraint.我在尝试运行我的 ALTER TABLE 语句时收到 ORA-02770,据我所知,这是通知我在外部表中引用的列未列为主键约束。 However, from what I can see my syntax is correct naming the primary keys in my CREATE TABLES statements.但是,据我所知,我的语法是正确命名 CREATE TABLES 语句中的主键的。 I searched inside the all_cons_columns table for the player_info table and it showed the primary keys listed as well.我在 all_cons_columns 表中搜索了 player_info 表,它也显示了列出的主键。 Could I get some guidance?我能得到一些指导吗? Listed below is my current script:下面列出的是我当前的脚本:

CREATE TABLE Player_Game
( school varchar2(30),
  player_number number(2,0),
  game_number number(1,0), 

  CONSTRAINT playergame_pk PRIMARY KEY (school, player_number,game_number)
);

CREATE TABLE School 
( school varchar2(30), 
  city varchar2(30), 
  coach varchar2(30), 
  team_name varchar2(30),
  win_record number (2,0), 
  loss_record number (2,0), 

  CONSTRAINT school_pk PRIMARY KEY (school)
);

CREATE TABLE Game 
( school varchar2(30),
  game_number number(1,0), 
  game_date DATE, 
  game_score varchar2(15), 

  CONSTRAINT game_pk PRIMARY KEY (school, game_number)
);

CREATE TABLE player_info
( school varchar2(30), 
  player_number number(2,0), 
  player_name varchar2(25), 

 CONSTRAINT playerinfo_pk PRIMARY KEY (school, player_number)
);

CREATE TABLE city 
( city varchar2(30),
  population number(5,0),

 CONSTRAINT city_pk PRIMARY KEY (city)
);

/*Here is the failing alter command */
ALTER TABLE Player_Game
ADD CONSTRAINT playergame_fk FOREIGN KEY (school) REFERENCES game(school); 

You have incorrect column list in playergame_fk in the alter table statement.您在 alter table 语句的playergame_fk中有不正确的列列表。 The column list of the foreign key must exactly match with the column list of the primary key it is referencing to .外键的列列表必须与它所引用的主键的列列表完全匹配。

Primary Key column list is school, game_number , therefore, your foreign key must have the same columns:主键列列表是school, game_number ,因此,您的外键必须具有相同的列:

ALTER TABLE Player_Game
ADD CONSTRAINT playergame_fk FOREIGN KEY (school, game_number) 
  REFERENCES game(school, game_number);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ORA-02270:此列列表没有匹配的唯一键或主键 - ORA-02270: no matching unique or primary key for this column-list ORA-02270:此列列表没有匹配的唯一键或主键 - ORA-02270: no matching unique or primary key for this column-list 如何添加外键倍数“ ORA-02270:此列列表没有匹配的唯一键或主键” - How to add multiples of foreign key “ORA-02270: no matching unique or primary key for this column-list” 需要协助SQL错误(ORA-02270):此列列表错误没有匹配的唯一键或主键 - Need assistance SQL error (ORA-02270) : no matching unique or primary key for this column-list error Oracle 错误 ORA-02270: 此列列表没有匹配的唯一键或主键 - Oracle error ORA-02270: no matching unique or primary key for this column-list 错误ORA-02270:此列列表没有匹配的唯一键或主键 - error ORA-02270: no matching unique or primary key for this column-list SQL错误:ORA-02270:此列列表没有匹配的唯一键或主键 - SQL Error: ORA-02270: no matching unique or primary key for this column-list 如何在sql中修复“ORA-02270:此列列表中没有匹配的唯一或主键”错误 - how to fix “ORA-02270: no matching unique or primary key for this column-list ” error in sql 数据库SQL错误:ORA-02270:此列列表没有匹配的唯一键或主键 - Database SQL Error: ORA-02270: no matching unique or primary key for this column-list (错误) ORA-02270: 此列列表没有匹配的唯一键或主键 - (Error) ORA-02270: no matching unique or primary key for this column-list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM