Welcome to a tutorial on how to import SQL files. So you have downloaded an SQL file from the Internet, and don’t know how to import it?
There are 3 common ways to import an SQL file into the database:
Let us walk through these methods step-by-step in this guide – Read on!
TABLE OF CONTENTS
|Command Line||MySQL Workbench||phpMyAdmin|
|Useful Bits & Links||The End|
COMMAND LINE IMPORT
This first method is just as mentioned in the introduction. We will use the command line to import SQL files, without the hassle of installing any extra tools.
IMPORT COMMAND EXAMPLE
D:\http\test>mysql -u root -p test < d:\http\test\test.sql Enter password: D:\http\test>
Yep, it’s that simple, just run the command
mysql -u USER -p DATABASE < PATH/FILE.SQL.
WINDOWS USERS – FIXING THE “UNKNOWN COMMAND” ERROR
D:\http\test>mysql -u root -p test < d:\http\test\test.sql 'mysql' is not recognized as an internal or external command, operable program or batch file.
For the Windows users, if you get the “MySQL is not recognized” error, you will need to add the Mysql folder into the system path.
STEP 1. START > SEARCH FOR “PATH” > CONTROL PANEL
STEP 2. ENVIRONMENTAL VARIABLES
STEP 3. PATH > EDIT
STEP 4. NEW > ADD PATH > OK
EXTRA – COMMAND LINE EXPORT
D:\http\test>mysqldump -u root -p test > d:\http\test\test.sql Enter password: D:\http\test>
This is a small extra for you guys who are curious – We can also export a database using
mysqldump -u USER -p DATABASE > PATH/FILE.SQL.
If you are uncomfortable with the command line or have to manage multiple databases, Oracle also offers a free management tool called MySQL Workbench.
DOWNLOAD & INSTALLATION
You can get the latest version of MySQL Workbench from the official MySQL website. Installation is very straightforward. Pretty much just launch the installer and hit “next”.
HOW TO IMPORT
After installation, launch the Workbench.
STEP 1. ADD MYSQL CONNECTION
STEP 2. ENTER YOUR DATABASE SETTINGS > CLICK OK
STEP 3. DATA IMPORT > SELECT FILE > SELECT DATABASE > START
Guess this one does not need more explanation. Simply choose “Data Export” from the side menu, and choose the database (or tables) that you want to export.
This final method is another tool that is made in PHP – If you have installed the XAMPP server, then you already have it.
DOWNLOAD & INSTALLATION
If you have installed the XAMPP server, simply access
http://localhost/phpmyadmin. If not, you can also get the latest version of phpMyAdmin from their official website. There is no installation required, just unzip it into your “public HTML” folder.
HOW TO IMPORT
STEP 1. SELECT DATABASE (OR CREATE A NEW ONE) > IMPORT
STEP 2. CHOOSE FILE > GO
UPLOAD FILE SIZE LIMIT
Please do take note that phpMyAdmin is ultimately still limited by the upload limit set in the
php.ini file – You will want to avoid uploading large SQL files, or at least zip it before uploading. Alternatively, you can change the limits in
upload_max_filesize = 100M post_max_size = 100M
USEFUL BITS & LINKS
That’s all for this project, and here is a small section on some extras and links that may be useful to you.
WHICH IS THE BEST METHOD?
Well, everyone has their own preferences – Some prefer to not install anything and just push SQL files in the command line, while a few like to use the Workbench to manage multiple databases at the same time. All the 3 methods work fine, just test it out yourself and stick with the one you are most comfortable with.
P.S. CHECK BEFORE YOU IMPORT
Before you go trigger happy with importing SQL files, you might want to check what is inside it first. They are essentially text files containing queries, so you can open it with any text editor – Just eyeball the queries, make sure that you are not overriding/deleting any of your critical existing database tables or works.
It will be a painful experience to lose all your work or even data. Plus, yes, it is an irreversible process… So always remember to backup before doing anything of a large scale.
LINKS & REFERENCES
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you in your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!