USE - MySQL


Overview

The USE command in MySQL is used to switch to a specific database within the current MySQL session. It allows you to access tables, views, and other database objects within the selected database.

Syntax

USE <database_name>;

Options/Flags

None.

Examples

Simple example:

USE my_database;

Selecting a database with a special character:

USE `my-database`;

Common Issues

  • Database not found error: Ensure that the specified database exists and that you have the necessary permissions to access it.
  • Syntax error: Double-check the syntax and ensure there are no typos or missing semicolons.
  • Access denied: Verify that you have the USAGE privilege on the database you are trying to switch to.

Integration

The USE command can be used in conjunction with other MySQL commands to perform various tasks, such as:

  • Creating a table:
USE my_database;
CREATE TABLE users (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255), PRIMARY KEY (id));
  • Inserting data:
USE my_database;
INSERT INTO users (name) VALUES ('John Doe');
  • Selecting data:
USE my_database;
SELECT * FROM users;