How to Import Excel File Into a MySQL Database
-
Use the
LOAD DATA
Statement to Import Data -
Method 2: Use
Sqlizer.io
to Import Data -
Method 3: Import Data Using
phpMyAdmin
- Conclusions
This article will teach how to import an excel file into a MySQL database using Workbench, Command-Line Prompt, and phpMyAdmin. This tutorial will take you through a step-by-step guide of the top 3 easiest ways to import excel files into the MySQL databases.
Use the LOAD DATA
Statement to Import Data
Using the LOAD DATA
statement in MySQL Workbench, you must have an empty table in your MySQL database.
We have already created a table named tb_students
. This table has id
, firstname
, lastname
, gender
, and city
as column names.
We will convert our file type from .xlsx
to .csv
to import data into the MySQL database. To do this, open your excel file that has the data.
Click on File->Save As
. Make sure that you have selected CSV (Comma delimited)(*.csv)
and then press the SAVE
.
See the following screenshot.
You now have your data in the *.csv
file. We will use the command below to import data from the .csv
file into the MySQL database (using Workbench).
# MySQL Version 8.0.27
LOAD DATA INFILE
'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/top 5 easiest ways to import excel file into mysql database.csv'
INTO TABLE tb_students
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
In the above LOAD DATA
statement, the data from the given file will be collected and inserted into the mentioned table. The first row will be ignored (the column names) because we already have them while creating a table in MySQL Workbench.
In this example, the .csv
file’s fields are terminated by a comma. Yours may be terminated by a tab
or single space.
If so, replace the line FIELDS TERMINATED BY ','
with FIELDS TERMINATED BY '\t'
or FIELDS TERMINATED BY ' '
respectively. Let’s look at the *.csv
file’s data and then the MySQL data that are given below.
Data in .CSV
File:
Data in MySQL Table:
You can also use the LOAD DATA
statement from Command Line (CMD). We have created a new table named tb_students_cmd
to practice the command line’s LOAD DATA
statement.
Use the LOAD DATA
statement to import data via a command-line prompt.
Let’s read the id
from tb_students_cmd
to confirm that the data is imported. See the following screenshot.
If you see any error about security considerations while using the LOAD DATA
statement, then you may visit this website to see the possible solutions.
Method 2: Use Sqlizer.io
to Import Data
This method is useful if you don’t want to convert your file type from .xlsx
to .csv
. Go to sqlizer.io, follow the instructions as follows (or as per your needs).
Select your excel file and MySQL database type. Tell the sqlizer.io
by checking or unchecking My File has a Header Row
.
Check Use CHECK IF TABLE EXISTS
if you want to. If you want to use the active worksheet, check Use the active worksheet
.
If not, then tell the worksheet name. We are not using it for this tutorial and prefer to tell the worksheet name, students
.
Make sure to tick Convert the whole worksheet
to import the whole data. Otherwise, give it the cell range.
You can write your table name (give it a unique name) in which you want the data to be imported. Then, click on the Convert My File
button.
You will see the following screen. You can either download the queries or copy them to execute wherever you want.
We can see the following screenshot to confirm that the data is imported, and we can read that.
Method 3: Import Data Using phpMyAdmin
If you are using phpMyAdmin
, then you can import data using the following steps (see the given screenshots).
To practice, we have created a new table named tb_students_phpmyadmin
. It has five columns named id
, firstname
, lastname
, gender
, and city
.
Click on the Import
tab and select your *.csv
file. The Format
would be CSV
, but you can select according to requirements.
Provide Format-specific Options
and click on Go
at the bottom-right corner.
You can see that all the records are being imported inserted in the table named tb_students_phpmyadmin
as follows.
Let’s see whether the data is imported or not. And here it is! We have imported data.
Conclusions
Finally, we have concluded that you have to use different methods to import data into MySQL.
We saw two methods for importing data from the .csv
file to MySQL. It includes the LOAD DATA
statement (in workbench and command line) and using phpMyAdmin
.
We used sqlizer.io
to import data from excel files to MySQL, which is very easy to use also.
Related Article - MySQL Workbench
- How to View Tables in MySQL Workbench
- MySQL Workbench: Edit Read-Only Table Data
- How to Create New Database in MySQL Workbench