简体   繁体   English

Mysql alter语句

[英]Mysql alter statement

this is my alter statement trying to connect two tables: 这是我尝试连接两个表的alter语句:

ALTER TABLE my_contacts 
  CHANGE `profession` `profession_id`  INT NOT NULL, 
  ADD CONSTRAINT professions_profession_id_fk 
  FOREIGN KEY (profession_id) REFERENCES professions (profession_id)

and I have the following error: 我有以下错误:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`contacts`.<result 2 when explaining filename '#sql-1ca_73'>, CONSTRAINT `professions_profession_id_fk` FOREIGN KEY (`profession_id`) REFERENCES `professions` (`profession_id`))

can anyone guide me on what this is? 任何人都可以指导我这是什么? I am actually learning how to write sql myself since I have used tools all this while. 我实际上是在学习如何编写sql,因为我一直使用工具。

This is a relationship conflict. 这是一种关系冲突。 An easier to solve this is 更容易解决这个问题

  • Remove the relationship between the fields 删除字段之间的关系
  • Alter the tables 改变表格
  • While adding the relationship again, fix the errors you receive. 在再次添加关系时,修复您收到的错误。

Your foreign key constraint is failing. 您的外键约束失败。

The foreign key you are trying to create enforces every profession_id in my_contacts to be present in your professions table. 您尝试创建的外键强制my_contacts中的每个profession_id都出现在您的职业表中。 Right now, this is not the case. 现在,情况并非如此。 You should lookup all records in my_contacts where the profession_id is not present in professions and fix those first. 您应该在my_contacts中查找职业中不存在profession_id的所有记录,并首先修复这些记录。

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

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