How to run MYSQL query in Command prompt

How to run Mysql Query in Command Line (start->run->cmd) :
We can also run a mysql queries using Mysql Command line and also windows commant prompt.In this post will help for running a queries in windows command prompt.
-u : Specify mysql database user name
-p : Prompt for password
-e : Execute sql query

Create a Database using Command prompt :
Will create Database using command line is simple, just follow below code
Syntax:
mysql -u root -p -e “CREATE QUERY”
Example:
mysql -u root -p -e “create database employee”
here employee is DB name

Select Query :
Syntax:
C:\>mysql -u[Username] -p[Password] DBNAME -e “YOUR QUERY”
Ex:
mysql -u root -p Employee -e “select * from employee_master where empID=1”
Type above query in command prompt and enter, once you run above syntax in cmd prompt it will as
Enter password:
Again press enter, then you will get result in a table

Get resultset in a file :
We can get result in a file also.
Syntax:
C:\>mysql -u[Username] -p[Password] DBNAME -e “YOUR QUERY” > FILENAME
Ex:
mysql -u root -p Employee -e “select * from employee_master where empID=1” > myfilename
Once run above query from cmd prompt, will get a result file from C: drive.

Get reusultset in XML format :
C:\>mysql -X -u root -e “USE DB;select * from YOURTABLE”
Ex:
C:\>mysql -X -u root -e “USE Employee;select * from employee_master”;
once run above query in command line, will get recordset as XML format

Get reusultset in html format :
Syntax :
C:\>mysql –html -u root -e “USE DB;select * from YOURTABLE”
Ex:
C:\>mysql –html -u root -e “USE Employee;select * from employee_master”;

Create Table in Command prompt :
Syntax:
c:\>mysql -u root -p -e “USE DB;CREATE QUERY”;
Ex:
C:\>mysql -u root -p -e “USE employee;CREATE table employee_detail(id INT(11),name VARCHAR(100))”;

Leave a Reply