Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 建立正确的订单导入数据以防止FK冲突_Mysql_Database - Fatal编程技术网

Mysql 建立正确的订单导入数据以防止FK冲突

Mysql 建立正确的订单导入数据以防止FK冲突,mysql,database,Mysql,Database,所有数据均为CSV格式 假设我有两个表:客户和他们的地址。当然,我在addresses表中设置了一个名为customer\u id的FK,它引用了customer\u id,这是父表customers中的主键 当它们为空时,我可以在表单中用数据填充它们,因为我从客户那里选择ID并将其传递给子表。但是我现在在phpmyadmin,我想将它们和它们的数据一起导入 我在考虑不同的情况 1) Creating the structure of the tables but still empty and

所有数据均为CSV格式 假设我有两个表:客户和他们的地址。当然,我在addresses表中设置了一个名为customer\u id的FK,它引用了customer\u id,这是父表customers中的主键

当它们为空时,我可以在表单中用数据填充它们,因为我从客户那里选择ID并将其传递给子表。但是我现在在phpmyadmin,我想将它们和它们的数据一起导入

我在考虑不同的情况

1) Creating the structure of the tables but still empty and then

- import the customers table first
- import addresses table afterwards
- and then implement the FK

but I fear I will get the : cannot add or delete child row foreign key constraint fails

2) Create the structure  of the tables empty and then

- do it the other way around, starting first with addresses, then customers

3) Importing both tables with their data (in csv) and then try to link them through the FK

有经验的人知道如何面对这种情况吗?

好吧,第一条路就是要走的路。您担心FK约束失败…当然,这可能发生。但是你的数据被破坏了,这就是外键保护你的原因。这就是它们的用途。所以我不明白你为什么害怕它

您的第二个场景…当然,您可以这样做,但是您也可以省略外键,您不能确定每个子项都有一个实际的父项。当您尝试在之后添加外键时,外键约束也会失败


您的第三个场景…当您的数据被正确规范化时,之后就没有办法链接它们了

无论如何,我总是实现FK并在需要时将其标准化到第6级,但让我担心的是,为什么你说我的数据已损坏。我确实收到了无法添加或删除子行的消息,但这并不意味着数据已损坏。原因是,如果已经存在数据,在FK可以链接到它们之前,所有索引都已经存在于父表中。Corrupt可能是错误的单词,请原谅我的英语…但是外键可以确保每个子表都有一个父表。当您收到该错误消息时,子级没有父级。这就是我要说的。