How to Run MySQL Queries From the Command Line
In this tutorial, we will learn how to run MySQL queries from the command line.
To run MySQL from the command line, we make use of the following command:
mysql -u userName -p databaseName < fileName.sql
However, we need to start by opening the command prompt using Windows + R key before beginning. We would have the following window:
Here we type cmd
and hit OK
, which opens up our command prompt. Next, we need to reach the bin directory of MySQL Server through the command line for which the snapshot is as follows:
Once we enter the MySQL command-line client, we initialize our work by creating a database to work with. Let us create a students
database. This operation can be done with the following command in the client:
Now, we use the USE
command to instruct the command line to change the current database to students
. As soon as we have the database ready, our next step is to create a table inside the database. Lets create a table student_details
with stu_id
, stu_firstName
, and stu_lastName
as the columns. Moreover, we make stu_id
the primary key. This operation can be done as follows:
Now let us try to insert rows with student details in this table. We can do this as follows:
Once we have inserted the rows in the table, we can use the following command to view our entries in the command line:
Similarly, we can perform each of the operations in the MySQL command-line client and use it as a workbench.
Thus, we have successfully executed MySQL queries in the command line.