Computer >> Computer tutorials >  >> Programming >> MySQL

Run SQL file in MySQL database from terminal?


To run SQL file in database, you need to use below syntax:

mysql -u yourUserName -p yourDatabaseName < yourFileName.sql

To understand the above syntax, let us open command prompt using windows+R shortcut key.

The snapshot is as follows:

Run SQL file in MySQL database from terminal?

After pressing OK button, you will get a command prompt. The snapshot is as follows:

Run SQL file in MySQL database from terminal?

Now reach the bin directory, the snapshot is as follows:

Run SQL file in MySQL database from terminal?

Here is my file ‘mydb.sql’ which is located in bin directory. The snapshot and content of SQL file is as follows:

Run SQL file in MySQL database from terminal?

Now you can type the above syntax which I have discussed to run SQL file. The snapshot of command is as follows:

Run SQL file in MySQL database from terminal?

Now you can check the table ‘Instructor’ is created in database test or not.

Run SQL file in MySQL database from terminal?

Now switch to database test using USE command. The query is as follows:

mysql>Use test

Now check the table ‘Instructor’ is present in test database. The query is as follows:

Run SQL file in MySQL database from terminal?

Yes, we have Instructor table. Now we need to check the record is present or not which is inserted in the table. The query is as follows:

mysql> select *from instructor;

The following is the output:

+----+-------+
| id | Name  |
+----+-------+
|  1 | John  |
|  2 | Larry |
|  3 | Sam   |
+----+-------+
3 rows in set (0.00 sec)