Jonathan Jonathan - 3 months ago 8
SQL Question

Create mysql table directly from CSV file using the CSV Storage engine?

I just learned that Mysql has a Native CSV storage engine which stores data in a Comma Separated Value file per table.

Is it possible to create a table directly from a uploaded CSV file, something like:


where users.csv is uploaded by the user?


This is not possible. To create table you need a table schema. What you have is a data file. Schema cannot be created with it. What you can do is check if your file has header row. In that case you can create table using that header row but manually.

However there is a way to generate create table statement by a batch file as described by John Swapceinski in the comment section of MySQL manual.

Posted by John Swapceinski on September 5 2011 5:33am.
Create a table using the .csv file's header:

# pass in the file name as an argument: ./mktable filename.csv
echo "create table $1 ( "
head -1 $1 | sed -e 's/,/ varchar(255),\n/g'
echo " varchar(255) );"