添加外键约束失败

时间:2018-07-25 08:30:20

标签: mysql

我正在尝试在表中添加约束,并且不断得到以下内容

  

11:11:55 ALTER TABLE adggtnz.lng01_rpt_animrec添加约束'fk20_lng01_rpt_animrec_reg04_rpt_animreg'外键('animalid')   参考'reg04_rpt_animreg'('animalid')

     

错误代码:1452。无法   添加或更新子行:外键约束失败   ('adggtnz'。'#sql-74e_2219446',CONSTRAINT   'fk20_lng01_rpt_animrec_reg04_rpt_animreg'外键('animalid')   参考'reg04_rpt_animreg'('animalid'))0.077秒

我已使用以下查询添加约束

ALTER TABLE adggtnz.lng01_rpt_animrec 
ADD CONSTRAINT 'fk20_lng01_rpt_animrec_reg04_rpt_animreg' 
FOREIGN KEY ('animalid') REFERENCES 'reg04_rpt_animreg' ('animalid');

如何成功运行以下查询并避免出现错误 lng01_rpt_animrec的示例:
列名

animalid,milkdate

'0065ae2f-62b2-45dd-b0c4-e6479e57e4c8', NULL
'0065ae2f-62b2-45dd-b0c4-e6479e57e4c8', '2018-05-29'
'0065ae2f-62b2-45dd-b0c4-e6479e57e4c8', NULL
'0065ae2f-62b2-45dd-b0c4-e6479e57e4c8', '2017-09-22'
'02db813b-c2a4-4ce1-802e-51f48e1d93ba', '2018-05-07'
'02db813b-c2a4-4ce1-802e-51f48e1d93ba', '2018-06-29'
'04f2d070-6336-4426-8e13-733872ee4bb3', '2018-01-02'
'0555e8ce-ba91-4a00-8a1d-133ff849ef87', '2017-10-02'
'05ab7837-005d-4494-8c32-f290cc83c1af', '2018-06-16'
'05ab7837-005d-4494-8c32-f290cc83c1af', NULL

reg04_animreg的示例:
列:

hh_id,regdate,animalid

'0', '2017-02-13', 'TZN000362738061'
'0', '2017-02-13', 'TZN000362738062'
'0', '2017-02-13', 'TZN000362738074'
'00', '2018-01-20', 'TZN000404007294'
'00000', '2017-06-14', 'TZN000404001178'
'00000', '2017-06-14', 'TZN000404001179'
'000000', '2017-07-21', 'TZN000403255840'
'0000000000_DUP1', '2017-05-31', 'TZN000365694884'
'0000000000_DUP2', '2017-06-06', 'TZN123456782234'
'0099887764', '2017-03-19', '03b704e2-bcdd-46e0-85e8-f38f21f16507'

0 个答案:

没有答案