我看到找不到父键。在我的例子中,FK_PLAYERTEAM和FK_PLAYERPOSITION。我注意到,无论我看到这个错误,都是操作错误的顺序。在输入或引用Player表中的这些值之前,未输入position或team id的值。在我的情况下,似乎一旦有14名球员进入,同样数量的球队,我再也无法进入任何球员了。
那么我在这里有一对一而不是一对一的关系吗?请检查我的工作!我是新人,为我的暑期课程做这件事。
/*CITY TABLE*/
DROP TABLE CITY;
CREATE TABLE CITY (
ID int NOT NULL PRIMARY KEY,
NAME varchar2(255) NOT NULL,
STATE varchar2(255) NOT NULL,
COUNTRY varchar2(100) default 'USA'
);
/*SEASON TABLE*/
DROP TABLE SEASON;
CREATE TABLE SEASON (
ID int NOT NULL PRIMARY KEY,
YEAR varchar2(4) NOT NULL
);
/*POSITION TABLE*/
DROP TABLE POSITION;
CREATE TABLE POSITION (
ID int NOT NULL PRIMARY KEY,
NAME varchar2(255) NOT NULL,
NAME_ABR varchar2(3) NOT NULL,
CATEGORY varchar2(255) NOT NULL
);
/*TEAM TABLE*/
DROP TABLE TEAM;
CREATE TABLE TEAM (
ID int NOT NULL PRIMARY KEY,
NAME varchar2(255) DEFAULT 'FREE AGENT',
SHORT_NAME varchar2(3) NOT NULL,
COACH_NAME varchar2(255) NULL,
CITY_ID int NULL,
CONSTRAINT FK_TEAMCITY FOREIGN KEY (ID) REFERENCES CITY(ID)
);
*/PLAYER TABLE*/
DROP TABLE PLAYER;
CREATE TABLE PLAYER (
ID int NOT NULL PRIMARY KEY,
FIRSTNAME varchar2(255) NOT NULL,
LASTNAME varchar2(255) NOT NULL,
DATE_OF_BIRTH varchar2(255) NOT NULL,
PLAYER_NUM int NOT NULL,
COUNTRY varchar2(3) DEFAULT 'USA',
TEAM_ID int NULL,
CONSTRAINT FK_PLAYERTEAM
FOREIGN KEY (ID) REFERENCES TEAM(ID),
POSITION_ID int NULL,
CONSTRAINT FK_PLAYERPOSITION
FOREIGN KEY (ID) REFERENCES POSITION(ID)
);
/*MATCH TABLE*/
DROP TABLE MATCH;
CREATE TABLE MATCH (
ID int NOT NULL PRIMARY KEY,
GAME_DAY date NOT NULL,
GAME_TIME timestamp NOT NULL,
WIN_TEAM int NULL,
CONSTRAINT FK_WIN_TEAM
FOREIGN KEY (WIN_TEAM) REFERENCES TEAM(ID),
HOME_TEAM int NOT NULL,
CONSTRAINT FK_HOME_TEAM
FOREIGN KEY (HOME_TEAM) REFERENCES TEAM(ID),
AWAY_TEAM int NOT NULL,
CONSTRAINT FK_AWAY_TEAM
FOREIGN KEY (AWAY_TEAM) REFERENCES TEAM(ID),
SEASON_ID int NOT NULL,
CONSTRAINT FK_SEASON_ID
FOREIGN KEY (SEASON_ID) REFERENCES SEASON(ID)
);
/*GOALS TABLE*/
DROP TABLE GOALS;
CREATE TABLE GOALS (
ID int NOT NULL,
MINUTE int NOT NULL,
MATCH_ID int NOT NULL,
CONSTRAINT FK_GOALS_MATCH
FOREIGN KEY (MATCH_ID) REFERENCES MATCH(ID),
PLAYER_ID int NOT NULL,
CONSTRAINT FK_GOALS_PLAYER
FOREIGN KEY (PLAYER_ID) REFERENCES PLAYER(ID)
);
我在这里输入的值是我的订单。
/*CITY POPULATE*/
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('1','Springfield','MA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('2','Toledo','OH','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('3','New Orleans','LA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('4','Erie','PA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('5','Dallas','TX','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('6','Denver','CO','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('7','Reno','NV','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('8','Scranton','PA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('9','Virginia Beach','VA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('10','Green Bay','WI','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('11','Atlanta','GA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('12','Columbus','OH','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('13','Orlando','FL','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('14','Austin','TX','USA');
/*SEASON POPULATE*/
INSERT INTO SEASON (ID,YEAR) VALUES (1,2013);
INSERT INTO SEASON (ID,YEAR) VALUES (2,2014);
INSERT INTO SEASON (ID,YEAR) VALUES (3,2015);
INSERT INTO SEASON (ID,YEAR) VALUES (4,2016);
INSERT INTO SEASON (ID,YEAR) VALUES (5,2017);
INSERT INTO SEASON (ID,YEAR) VALUES (6,2018);
/*POSITION POPULATE*/
INSERT INTO POSITION
VALUES ('1','GoalKeeper','GK','GoalKeeper');
INSERT INTO POSITION
VALUES ('2','Sweeper','SW','Defender');
INSERT INTO POSITION
VALUES ('3','Centerback','CB','Defender');
INSERT INTO POSITION
VALUES ('4','Leftback','LB','Defender');
INSERT INTO POSITION
VALUES ('5','Rightback','RB','Defender');
INSERT INTO POSITION
VALUES ('6','Left-Wingback','LWB','Defender');
INSERT INTO POSITION
VALUES ('7','Right-Wingback','RWB','Defender');
INSERT INTO POSITION
VALUES ('8','Defending Midfielder','DM','Midfielder');
INSERT INTO POSITION
VALUES ('9','Central Midfielder','CM','Midfielder');
INSERT INTO POSITION
VALUES ('10','Attacking Midfielder','AM','Midfielder');
INSERT INTO POSITION
VALUES ('11','Left Winger','LW','Midfielder');
INSERT INTO POSITION
VALUES ('12','Right Winger','RW','Midfielder');
INSERT INTO POSITION
VALUES ('13','Center Forward','CF','Forward');
INSERT INTO POSITION
VALUES ('14','Withdrawn Forward','WF','Forward');
INSERT INTO POSITION
VALUES ('15','Striker','S','Forward');
/*TEAM POPULATE*/
INSERT INTO TEAM
VALUES ('1','Springfield FC','SFC','Dolan Mcguire','1');
INSERT INTO TEAM
VALUES ('2','Toleda Stars FC','TS','Wing Warren','2');
INSERT INTO TEAM
VALUES ('3','Louisiana AF','LAF','Zane Valentine','3');
INSERT INTO TEAM
VALUES ('4','Erie FC','EAF','Brenda Rios','4');
INSERT INTO TEAM
VALUES ('5','FC Dallas','FCD','Mark Horne','5');
INSERT INTO TEAM
VALUES ('6','Colorado Rapids','CR','Brian Cameron','6');
INSERT INTO TEAM
VALUES ('7','Reno 1868 FC','RFC','Russel Klabough','7');
INSERT INTO TEAM
VALUES ('8','Scranton Football','SF','Mark York','8');
INSERT INTO TEAM
VALUES ('9','Beach FC','BFC','Zidane Zidan','9');
INSERT INTO TEAM
VALUES ('10','Green Bay Phoenix','GBP','Shad Ramos','10');
INSERT INTO TEAM
VALUES ('11','Atlanta United FC','AFC','Gernado Martino','11');
INSERT INTO TEAM
VALUES ('12','Columbus Crew SC','CCS','Gregg Berhalter','12');
INSERT INTO TEAM
VALUES ('13','Orlando City SC','OSC','Jason Kreis','13');
INSERT INTO TEAM
VALUES ('14','Austin Aztex','AAX','Adrian Health','14');
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (1,'Ashton','Hewitt','05/07/1991',31,'USA',10,13);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (2,'Fuller','Farrell','08/12/1987',45,'USA',3,2);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (3,'Allistair','Mccullough','05/20/1993',20,'USA',5,14);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (4,'Connor','Caldwell','03/28/1996',66,'USA',14,5);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (5,'Igor','Britt','09/27/1992',45,'USA',12,11);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (6,'Cyrus','Barron','10/20/1991',41,'USA',7,1);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (7,'Quamar','Melendez','09/28/1986',20,'USA',7,15);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (8,'Dustin','Sandoval','07/05/1987',34,'USA',11,9);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (9,'Addison','Santana','12/07/1994',17,'USA',8,10);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (10,'Christian','Murphy','11/06/1986',73,'USA',5,15);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (11,'Isaiah','Reeves','03/23/1990',59,'USA',1,8);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (12,'Nero','Luna','03/14/1996',31,'USA',10,4);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (13,'Deacon','Maddox','05/10/1989',54,'USA',4,5);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (14,'Macaulay','Velez','07/13/1994',33,'USA',7,15);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (15,'Jamal','Cox','02/03/1990',44,'USA',12,11);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (16,'Axel','Dominguez','01/13/1996',15,'USA',3,5);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (17,'Jakeem','Barlow','03/09/1994',61,'USA',12,6);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (18,'Mufutau','Hodges','09/09/1987',69,'USA',1,9);
但我最终得到了第14行:
ORA-02291: integrity constraint (SQL_DFIJAZERMYRXHWLVJVYDVODHP.FK_PLAYERTEAM) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721
ORA-02291: integrity constraint (SQL_DFIJAZERMYRXHWLVJVYDVODHP.FK_PLAYERPOSITION) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721
ORA-02291: integrity constraint (SQL_DFIJAZERMYRXHWLVJVYDVODHP.FK_PLAYERPOSITION) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721
ORA-02291: integrity constraint (SQL_DFIJAZERMYRXHWLVJVYDVODHP.FK_PLAYERPOSITION) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721
答案 0 :(得分:0)
您正在应用错误的外键约束,请尝试以下操作:
CREATE TABLE TEAM (
ID int NOT NULL PRIMARY KEY,
NAME varchar2(255) DEFAULT 'FREE AGENT',
SHORT_NAME varchar2(3) NOT NULL,
COACH_NAME varchar2(255) NULL,
CITY_ID int NULL,
CONSTRAINT FK_TEAMCITY FOREIGN KEY (CITY_ID) REFERENCES CITY(ID)
);