INSERT INTO ... SELECT在同一个表中生成外键错误

时间:2013-07-24 16:14:19

标签: mysql

我在使用以下插入查询时遇到问题。

INSERT INTO CM_LABEL_CALENDAR (
  label_id,
  label_name,
  order_seq,
  meal_id,
  hyperlink
)
SELECT
  label_id,
  label_name,
  order_seq,
  (meal_id + 315),
  hyperlink
FROM
  CM_LABEL_CALENDAR
WHERE
  (meal_id BETWEEN '1466' AND '1521');

当我尝试执行它时,我收到以下错误:

  

查找错误 - MySQL数据库错误:无法添加或更新子行:外键约束失败(TEST_PBMSCM_LABEL_CALENDAR,CONSTRAINT CM_LABEL_CALENDAR_ibfk_1 FOREIGN KEY(meal_id )参考CM_MEAL_CALENDARmeal_id))

我试过寻找答案但找不到答案。

1 个答案:

答案 0 :(得分:0)

CM_LABEL_CALENDAR(meal_id)CM_MEAL_CALENDAR(meal_id)

之间存在外键约束

您收到此错误是因为您尝试在meal_id列中插入CM_MEAL_CALENDAR表中不存在的值。