插入语句是';t在MYSQL中工作(外键)
您好,我遇到了这个错误,无法解决它,有人知道我应该如何解决这个问题,这样我的insert语句才能工作吗 INSERT语句与同一表的外键冲突 约束“fk_person_fatherID” 我假设它是因为外键在主键之外工作,在insert语句中,我在创建它们之前声明它们?我可能已经完全离开了,请帮帮我(: 这是我的插入声明插入语句是';t在MYSQL中工作(外键),mysql,sql,sql-server,Mysql,Sql,Sql Server,您好,我遇到了这个错误,无法解决它,有人知道我应该如何解决这个问题,这样我的insert语句才能工作吗 INSERT语句与同一表的外键冲突 约束“fk_person_fatherID” 我假设它是因为外键在主键之外工作,在insert语句中,我在创建它们之前声明它们?我可能已经完全离开了,请帮帮我(: 这是我的插入声明 INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID)
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID)
VALUES ('Abraham', 'Simpson', 'M', '1994-01-15', '2015-07-21',NULL,NULL),
('Mona', 'Simpson', 'F', '1946-09-22', NULL,NULL,NULL),
( 'Herb', 'Simpson', 'M', '1963-11-21',NULL,1,2),
( 'Homer', 'Simpson', 'M', '1965-05-19',NULL,1,2),
( 'Clancy', 'Bouvier', 'F', '1945-02-12',NULL,NULL,NULL),
( 'Jackie', 'Bouvier', 'M', '1945-12-01','2016-05-15',NULL,NULL),
( 'Marge', 'Simpson', 'F', '1966-05-18',NULL,6,5),
( 'Patty', 'Bouvier', 'F', '1964-01-08',NULL,6,5),
( 'Selma', 'Bouvier', 'F', '1969-03-01',NULL,6,5),
( 'Bart', 'Simpson', 'M', '1990-01-01',NULL,4,7),
( 'Lisa', 'Simpson', 'F', '1992-05-15',NULL,4,7),
( 'Maggie', 'Simpson', 'F', '1997-11-28',NULL,4,7),
( 'Ling', 'Bouvier', 'M', '2000-04-02',NULL,NULL,9)
我认为,错误信息非常清楚
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD,fatherID,motherID)
( 'Herb', 'Simpson', 'M', '1963-11-21',NULL,1,2)
( 'Homer', 'Simpson', 'M', '1965-05-19',NULL,1,2)
您试图将
FatherId
值设置为1,但我认为person
表中没有Id为1的记录我通过取出母亲和父亲Id的
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD)
VALUES ('Abraham', 'Simpson', 'M', '1994-01-15', '2015-07-21'),
('Mona', 'Simpson', 'F', '1946-09-22', NULL),
( 'Herb', 'Simpson', 'M', '1963-11-21',NULL),
( 'Homer', 'Simpson', 'M', '1965-05-19',NULL),
为了添加到母亲和父亲ID中,在添加输入语句后,我只需“编辑前200行”并手动输入ID,这是为什么会有问题?@raina77ow,但父ID列可以为空。这不会导致构造外键问题。这似乎是最有可能的解释+1
INSERT INTO person(persFName,persLname,persGender,persDOB,persDOD)
VALUES ('Abraham', 'Simpson', 'M', '1994-01-15', '2015-07-21'),
('Mona', 'Simpson', 'F', '1946-09-22', NULL),
( 'Herb', 'Simpson', 'M', '1963-11-21',NULL),
( 'Homer', 'Simpson', 'M', '1965-05-19',NULL),