4

I've been asked to create an excel file saved as a .csv file to import into a mysql db. How do you format the excel file if the data goes to multiple tables in the db?

2
  • I don't think that's possible, at least with the CSV file format. Excel .xls(x) files allows multiple spreadsheets so you can use that. Commented Apr 26, 2013 at 18:00
  • You don't have to format the Excel file into multiple sheets. You can selectively extract parts of the CSV file and put them into different tables, and link these tables together with a foreign key. This article explores how you can do that: blog.terresquall.com/2022/05/… Commented May 10, 2022 at 1:48

2 Answers 2

4

You need to create separate csv file for each table & then you can use command something like below:

LOAD DATA LOCAL INFILE './csv_data/employees.csv' 
INTO TABLE employees 
FIELDS TERMINATED BY ',' 
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES 
(first_name,last_name,email_id );

This sample code is under presumption that csv file is comma separated , escape charater is double quote & first line contain header.

Sign up to request clarification or add additional context in comments.

Comments

2

You need a CSV file for each table, or you need to import all of your data into an intermediate table and then split that out somehow using either a series of queries, a stored procedure, or a TRIGGER on the import table.

This is because the LOAD DATA INFILE system is limited to one table at a time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.