If you have connected to a MySQL database using the Command Line on Windows or Terminal on macOS/Linux and want to run queries against a particular schema/database, you can make use of the "USE" statement.
The syntax for the USE statement in MySQL is as follows:
Using the USE statement, you can switch to a particular database before running queries against it.
For example, to switch to the "myDb" database, you can use the following command:
mysql> USE myDb;
Database changed
If you do not provide the correct database name, or the database does not exist you will get ERROR 1049 (42000),
The database name cannot be provided on a new line, if you just make use of the USE statement, you will get an error.
If you do not want to switch to a particular database, you can also prefix the table name in your query with the database name.
For example:Select * from myDb.employee where name='Sam';

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!