I want to import CSV data into mysql database.
Additionally, I dont want to create a table first and then import. That means, if my csv file name is test.csv, and its contents are like
customer_ID,name
10000000,name1
10000001,name2
I should be execute
mysql> use database {database_name}
mysql> {some_command} test.csv
And I should be able to create a table with name test
and it having headers customer_ID
and name
and it should be populated according to contents of test.csv. Specifically some_command
is the magic bullet I am looking for.
Any idea how this can be done in MySQL. While I searched I did not find a way to import CSV data without creating a table first.
Best Answer
There is no way to do this with MySQL own tools. If you look closer it makes sense that you have to create your table first, you have to specify datatypes for the fields of your csv file, possible indexes and so on. MySQL canĀ“t predict which datatype you want to use, so here is some manual work to do.
On the other hand you can use external scripts to archive what you want, a quick and dirty example would be:
Above "script" lefts plenty of room for optimization, it should only give you a basic idea how to archive what you need with common Linux tools.