简体   繁体   English

将数据添加到表时出现 MySQL 错误 1452

[英]MySQL Error 1452 when adding data to tables

I get the following error when trying to import data via MySQL Workbench:尝试通过 MySQL Workbench 导入数据时出现以下错误:

Preparing...
Importing DataInputs.sql...
Finished executing script
ERROR 1452 (23000) at line 6: Cannot add or update a child row: a foreign key constraint fails (`nsldatabase`.`player`, CONSTRAINT `playsFor` FOREIGN KEY (`player_ID`) REFERENCES `team` (`team_ID`))
Operation failed with exitcode 1

I am creating the database via two different SQL scripts.我正在通过两个不同的 SQL 脚本创建数据库。 One creates the tables, and another imports the data.一个创建表,另一个导入数据。 I thought initially that some foreign key wasn't define properly, but it seems like they are all setup properly.我最初认为某些外键没有正确定义,但似乎它们都设置正确。 Any help would be great.任何帮助都会很棒。 See both files below:请参阅以下两个文件:

This is the NSLStructure.sql file:这是 NLSStructure.sql 文件:

DROP DATABASE IF EXISTS NSLdatabase;

CREATE SCHEMA IF NOT EXISTS NSLdatabase;
USE NSLdatabase;



CREATE TABLE player (
  player_ID int NOT NULL,
  player_Name text NOT NULL,
  player_Position text NOT NULL,
  player_Skills int NOT NULL,
  team_ID int NOT NULL,
  CONSTRAINT playerPK PRIMARY KEY (player_ID)
);

CREATE TABLE history (
  history_ID int NOT NULL,
  player_ID int NOT NULL,
  history_Desc text NOT NULL,
  history_sDate text NOT NULL,
  history_eDate text NOT NULL,
  CONSTRAINT recordPK PRIMARY KEY (history_ID, player_ID)
);

CREATE TABLE field (
  field_ID int NOT NULL,
  field_Name text NOT NULL,
  field_Location text NOT NULL,
  CONSTRAINT fieldPK PRIMARY KEY (field_ID)
);

CREATE TABLE team (
  team_ID int NOT NULL,
  team_Name text NOT NULL,
  team_City text NOT NULL,
  field_ID int NOT NULL,
  captain_ID int NOT NULL,
  team_coach text NOT NULL,
  CONSTRAINT teamPK PRIMARY KEY (team_ID)
);

CREATE TABLE matches (
  matches_ID int NOT NULL,
  matches_Date text NOT NULL,
  matches_Score text NOT NULL,
  matches_Winner text NOT NULL,
  field_ID int NOT NULL,
  teamHost_ID int NOT NULL,
  teamGuest_ID int NOT NULL,
  CONSTRAINT matchesPK PRIMARY KEY (matches_ID)
);

CREATE TABLE goal (
  goal_ID int NOT NULL,
  goal_Time int NOT NULL,
  matches_ID int NOT NULL,
  player_ID int NOT NULL,
  CONSTRAINT goalPK PRIMARY KEY (goal_ID, matches_ID, player_ID)
);

ALTER TABLE team
  ADD CONSTRAINT capitanRel FOREIGN KEY (captain_ID)
  REFERENCES player (player_ID);

ALTER TABLE team
  ADD CONSTRAINT playedOnField FOREIGN KEY (field_ID)
  REFERENCES field (field_ID);

ALTER TABLE player
  ADD CONSTRAINT playsFor FOREIGN KEY (player_ID)
  REFERENCES team (team_ID);

ALTER TABLE history
  ADD CONSTRAINT hasRel FOREIGN KEY (player_ID)
  REFERENCES player (player_ID);

ALTER TABLE goal
  ADD CONSTRAINT scoredBy FOREIGN KEY (player_ID)
  REFERENCES player (player_ID);

ALTER TABLE goal
  ADD CONSTRAINT scoredIn FOREIGN KEY (matches_ID)
  REFERENCES matches (matches_ID);

ALTER TABLE matches
  ADD CONSTRAINT playedOn FOREIGN KEY (field_ID)
  REFERENCES field (field_ID);

ALTER TABLE matches
  ADD CONSTRAINT playedHost FOREIGN KEY (teamHost_ID)
  REFERENCES team (team_ID);

ALTER TABLE matches
  ADD CONSTRAINT playedGuest FOREIGN KEY (teamGuest_ID)
  REFERENCES team (team_ID);

This is the DataInputs.sql file:这是 DataInputs.sql 文件:

USE NSLdatabase;

INSERT INTO player
  VALUES
  (1, "Paxton Pomykal", "Midfielder", 7.5, 1);

INSERT INTO history
  VALUES
  (1, 3, "US 20 Squad", "11/27/2003", "12/19/2003");

INSERT INTO field
  VALUES
  (1, "Dicks Sporting Goods Park", "Commerce City, CO");

INSERT INTO team
  VALUES
  (2, "Colorado Rapids", "Denver, CO", 1, 3, "Robin Fraser");

INSERT INTO matches
  VALUES
  (1, "01/08/2019", "0-1", 2, 1, 1, 2);

INSERT INTO goal
  VALUES
  (1, 32, 1, 19);

You are not inserting the data in the correct sequence.您没有以正确的顺序插入数据。 Parent rows need to be created before children rows, so the foreign key constraints are not violated.父行需要在子行之前创建,因此不会违反外键约束。

So, typically, you need to create rows in team rows before creating rows in player .因此,通常,您需要先在team行中创建行,然后再在player创建行。 There may be other dependencies that you need to look into, based on the same logic.基于相同的逻辑,您可能需要查看其他依赖项。

An alternative approach would be to add the foreign key constraints after the data is inserted.另一种方法是在插入数据添加外键约束。 However, I would not necessarily recommend that: if your data has invalid relationships, you will not be able to create the foreign keys.但是,我不一定建议:如果您的数据具有无效关系,您将无法创建外键。 It is safer to proceed the other way around, since offending rows are immediately signaled.反过来进行会更安全,因为会立即发出违规行的信号。

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

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