简体   繁体   中英

How to import CSV data to table with foreign keys

I have a data warehouse type model in MySQL so there's a facts table that is all foreign keys pretty much. I have all the data in CSV files and have imported all of it fine but I can't import to the facts table because "Cannot add or update a child row: a foreign key constraint fails". Most of the answers I've found get it solved with SET foreign_key_checks = 0 but I keep getting the same error, I thought it might be because I'm using the table import wizard instead of LOAD DATA INFILE but that gives me the secure-file-priv error and I've tried everything to solve that without success.

So is there a way to do this? Will it work if I create the tables in a different order? like set the foreign keys in the facts table after I import the data?

edit: I've tried to import the file anyway even though I'm getting errors just because and it imported two rows, I have no idea what this means, every row has the exact same columns filled

just leaving this here in case someone needs a quick fix like I did, you can create the tables without foreign keys, import all data (including to the facts table) and only then add the foreign keys to the table, works fine! just make sure the data in the columns of the fact table matchs with the data from the table it is referencing, meaning it won't let you import a 99 if the dimension referenced doesn't have a row with 99

您需要先加载维度表,然后在加载事实表时查找维度表以获取要插入事实表的相关键

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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