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|
A) 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.
A1) IMPORT COMMAND EXAMPLE
D:\http\>mysql -u root -p test < d:\http\test.sql Enter password: D:\http\>
Yep, it’s that simple, just run the command
mysql -u USER -p DATABASE < PATH/FILE.SQL.
A2) WINDOWS “UNKNOWN COMMAND”
D:\http\>mysql -u root -p test < d:\http\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.
START > SEARCH FOR “SYSTEM ENVIRONMENT”
SYSTEM VARIABLES > PATH
ADD MYSQL TO THE PATH
A3) COMMAND-LINE EXPORT
D:\http\>mysqldump -u root -p test > d:\http\test.sql Enter password: D:\http\>
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.
B) MYSQL WORKBENCH
If you are uncomfortable with the command line or have to manage multiple databases, Oracle also offers a free management tool called MySQL Workbench.
B1) 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”.
B2) HOW TO IMPORT WITH MYSQL WORKBENCH
ADD MYSQL CONNECTION
Launch MySQL Workbench. Add a connection to your database, then click on it.
SERVER > DATA IMPORT
After connecting to your database, Server > Data Import.
SELECT FILE > SELECT DATABASE > START IMPORT
Lastly, just choose the SQL file to import, and where to import it into.
B3) EXPORT DATABASE WITH WORKBENCH
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.
C1) 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.
C2) HOW TO IMPORT WITH PHPMYADMIN
You can probably figure this one on your own… Select a database > Import > Choose File > Go.
C3) UPLOAD FILE SIZE LIMIT
upload_max_filesize = 100M post_max_size = 100M
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
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!