简体   繁体   中英

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. 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. However, from what I can see my syntax is correct naming the primary keys in my CREATE TABLES statements. I searched inside the all_cons_columns table for the player_info table and it showed the primary keys listed as well. 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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM