Friday, April 1, 2016

How to import an SQL file using the command line in MySQL

When I was working with MySql command prompt, I was wondering how to import an available sql file into the available database. 

First you need to open the MySql Command prompt. And select the database. Here "smartcart" is the database. Now I select that database using "use" command as below:

mysql> use smartcart;
Database changed

Now you need know the path of the sql file. Here "items.sql" is the file exported from the other mysql database which I want to import to this "smartcart" database. For that we can use source command.

mysql> source f:/items.sql;
Query OK, 0 rows affected (0.07 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.76 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Now you can check that the new table of the sql file is added to the database

mysql> show tables;
+---------------------+
| Tables_in_smartcart |
+---------------------+
| items               |
| users               |
+---------------------+
2 rows in set (0.00 sec)

mysql> select * from items;
Empty set (0.06 sec)

That's it. Happy coding.

No comments: