MySQL - How to Import CSV Data into MySQL Database

in Education2 years ago

In this post, we take a look at How to Import CSV Data into MySQL Database. Previously we looked at how to import the JSON data into MySQL database. I have also covered the setup of the MySQL and the MySQL workbench in the past and I may cover it again in near future.

Here are some of the options that you can use to import & export data into the MySQL database.

  • Import CSV data into the MySQL database using the MySQL Shell
  • Import CSV data into the MySQL database using MySQL Workbench

Here in the below video I am going to show you how to import the CSV data into the MySQL database using the MySQL Workbench.



First thing we would be needing is the sample file. And that sample file for the CSV should have some of the data that you would be needing there. We just have to test out whether the CSV file uploads or not.

Another thing is that you are MySQL Shell and the Workbench that you would be needing for the usage of the importing of the data. And once you use that it would take you ahead with the next steps that we are about to check out. So I assume you have some directory ready there with the CSV file in it.

First method making use of the MySQL shell. I assume you created a database and also the table and then you use the command like this.

LOAD DATA INFILE 'Path/Sample.csv'
INTO TABLE Sample
FIELDS TERMINATED BY ','
ENCLOSED BY '"' LINES
TERMINATED BY '\n'
(column1,column2,column3,column4);

As you can see we have table named Sample. And we are uploading the data into the 4 columns of the table. And if things work out you would be seeing the CSV data through the specific path folder through which we imported into the table.

You can check out the command in the image.

sqlshell.png

Now in case of the Workbench follow the steps below.

  1. Open the MySQL Workbench on desktop.
  2. Choose your database where you want to upload CSV data.
  3. Move to the table in which the database would be uploading.
  4. Right click on the table name and then select Table Data Import Wizard.
  5. Point out the path to the CSV file in the import wizard.
  6. Next few steps make sure to choose the columns and primary key and name, encoding etc.
  7. Click finish and check the results.

As you can see this would get you some good results with the workbench and there is no need to type command either. Because mysql shell is a good choice for the devops and the DBA people and they can do this lot better and also they can have much better approach with the flow or set of the process for the output of their choice.

My approach is to use what works for you. Some people make use of the terminal a lot and then they should make use of the mysql database with the terminal usage there in the case of the GUI based something. You can make use of the workbench app and it can be pretty cool there to try out. I have learned that importing the CSV data into the mysql database.

I hope this tutorial helps. You may now try out more of such tutorials in my channel devnami. You can click on the above video and check my channel. You can then explore channel for other tutorials. I post a lot of content specific to the cloud and the databases and the data science. So you should like, share and subscribe and hopefully you would love this channels content.

I'll see you later in next week and hopefully you would find the value in the tutorial.

Sort:  

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

Congratulations @devpress! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You received more than 55000 upvotes.
Your next target is to reach 60000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

Our Hive Power Delegations to the November PUM Winners
Feedback from the December Hive Power Up Day
Hive Power Up Month Challenge - November 2023 Winners List