× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to load a CSV file into MySQL

Federico Vela
Oct 10, 2015
<p>First, create a table without a primary key and one varchar column for every column in your csv file. IN THE SAME ORDER.</p> <p> </p> <p>Once that is done, then the load can be simply made by statements such as:</p> <p> </p> <div style="background:0px 50% rgb(248, 248, 255); border:0px; padding:0px 5px"><span style="color:teal !important">LOAD </span><span style="color:teal !important">DATA </span>INFILE<span style="color:rgb(0, 111, 224) !important"> </span><span style="color:rgb(221, 17, 68) !important">'c:/tmp/theData.csv'</span><span style="color:rgb(0, 111, 224) !important"> </span></div> <div style="background:0px 50% rgb(248, 248, 255); border:0px; padding:0px 5px"><span style="color:teal !important">INTO </span><span style="color:teal !important">TABLE </span><span style="color:teal !important">theTableName</span></div> <div style="background:0px 50% rgb(248, 248, 255); border:0px; padding:0px 5px"><span style="color:teal !important">FIELDS </span><span style="color:teal !important">TERMINATED </span>BY<span style="color:rgb(0, 111, 224) !important"> </span><span style="color:rgb(221, 17, 68) !important">','</span><span style="color:rgb(0, 111, 224) !important"> </span></div> <div style="background:0px 50% rgb(248, 248, 255); border:0px; padding:0px 5px"><span style="color:teal !important">ENCLOSED </span>BY<span style="color:rgb(0, 111, 224) !important"> </span><span style="color:rgb(221, 17, 68) !important">'"'</span></div> <div style="background:0px 50% rgb(248, 248, 255); border:0px; padding:0px 5px"><span style="color:teal !important">LINES </span><span style="color:teal !important">TERMINATED </span>BY<span style="color:rgb(0, 111, 224) !important"> </span><span style="color:rgb(221, 17, 68) !important">'\n'</span></div> <div style="background:0px 50% rgb(248, 248, 255); border:0px; padding:0px 5px">IGNORE<span style="color:rgb(0, 111, 224) !important"> </span><span style="color:rgb(0, 153, 153) !important">1</span><span style="color:rgb(0, 111, 224) !important"> </span><span style="color:rgb(0, 45, 122) !important">ROWS</span><span style="color:rgb(51, 51, 51) !important">; -- in case the csv has column headers</span></div>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus