VMware ESXi and vSphere Cluster Management
How to Create and Select a Database in MySQL
Learn how to create, verify, and select a MySQL database with CREATE DATABASE, SHOW DATABASES, and USE, including permissions and safe repeated execution.
MySQL is a relational database management system that uses SQL, the language for defining, querying, and managing relational data. Before creating tables, you usually create a database to contain them.
What is a MySQL database?
A database is a named logical container for related tables and other database objects. For example, an application for tracking products might use a database named inventory containing tables such as products and suppliers.
Creating a database creates the container only. It does not create tables, columns, rows, or application data. You create those objects separately after selecting the database.
Prerequisites
You need access to a MySQL server and an SQL interface, such as the MySQL command-line client. From a terminal, an illustrative connection command is:
mysql -u username -p
After entering the password, you can issue SQL statements at the MySQL prompt. The examples in this lesson use testdb as the database name.
CREATE DATABASE syntax
The CREATE DATABASE statement creates a new database. Its basic syntax is:
CREATE DATABASE database_name;
database_name is an identifier: a name assigned to a database or another SQL object. Replace it with the name you choose.
The semicolon terminates the SQL statement in the MySQL command-line client and in many SQL editors. Other interfaces may provide a run button or use a different statement delimiter, but the semicolon is the normal convention for these examples.
Choosing a database name
You choose the database name when you write the CREATE DATABASE statement. Good names make the database's purpose clear and are easy to use in scripts.
- Prefer descriptive, lowercase names such as
inventory,customer_portal, ortestdb. - Use underscores when separating words is helpful.
- Avoid spaces and confusing punctuation.
- Avoid reserved words, which have special meanings in SQL.
Names containing spaces, special characters, or reserved words may require quoted identifiers. For beginner-friendly scripts, choosing a simple name made from letters, numbers, and underscores is usually clearer and less error-prone.
Create a database: example
Run this statement to create a database named testdb:
CREATE DATABASE testdb;
If the operation succeeds, the MySQL client typically reports that the query was successful and includes execution timing. The exact formatting varies by client and version, but a successful response indicates that MySQL accepted the statement and created the database.
If a database named testdb already exists, the statement normally returns a database-already-exists error instead of replacing it.
Verify the database with SHOW DATABASES
The SHOW DATABASES statement lists databases visible to the current MySQL account:
SHOW DATABASES;
Read the result and look for a row named testdb. The output may also contain system databases, including information_schema. information_schema is a system database that contains metadata about databases, tables, columns, and other objects.
The list is based on what the connected account can see. It may not contain every database present on the server.
Select the database with USE
The USE statement makes a database active for the current connection:
USE testdb;
The active database is the database MySQL uses for table operations that do not specify a database name. After running USE testdb;, you can create or query tables without writing testdb. before every table name.
For example:
USE testdb;
CREATE TABLE example_items (
id INT PRIMARY KEY,
name VARCHAR(100)
);
The table is created in testdb because that database is active. Without a selected database, a table statement may fail with a “No database selected” error.
Normal MySQL database workflow
A typical beginner workflow is:
- Connect to the MySQL server.
- Create the database.
- Verify that it appears in
SHOW DATABASES. - Select it with
USE. - Create tables and add data.
CREATE DATABASE testdb;
SHOW DATABASES;
USE testdb;
CREATE TABLE example_items (
id INT PRIMARY KEY,
name VARCHAR(100)
);
Database selection is session-specific. If you disconnect and create a new connection, run USE testdb; again unless your client selects a default database during connection.
MySQL commands for creating and using a database
| Command | Purpose | Expected result |
|---|---|---|
CREATE DATABASE database_name; | Create a new database container. | MySQL creates the database if the name is available and the account has permission. |
CREATE DATABASE IF NOT EXISTS database_name; | Run setup safely when the script may execute more than once. | MySQL creates the database if it does not exist; an existing database is left unchanged. |
SHOW DATABASES; | List databases visible to the connected account. | A result set displays accessible database names, possibly including system databases. |
USE database_name; | Set the active database for the current session. | Later unqualified table operations use that database. |
Safe repeated execution with IF NOT EXISTS
For installation scripts, development setup, or other commands that may run repeatedly, use IF NOT EXISTS:
CREATE DATABASE IF NOT EXISTS testdb;
This clause prevents the usual error when a database with that name already exists. It does not delete, replace, empty, or reset the existing database. Existing tables and data remain in place.
Use this form only when continuing with the existing database is acceptable. If you need a clean database, inspect the existing one and follow a deliberate backup or removal procedure rather than assuming the statement will reset it.
Permissions and ownership
A MySQL account needs sufficient privileges—permissions to perform operations such as creating a database—to run CREATE DATABASE. If the account lacks the required privilege, MySQL returns an access-denied error.
A user may also see only databases for which that account has privileges, depending on the server configuration and granted permissions. Therefore, a database missing from SHOW DATABASES does not always prove that it does not exist on the server.
Troubleshooting common errors
Database already exists
Cause: Another database already uses the selected name.
Resolution: Choose a different name, inspect the existing database if it is yours to use, or run CREATE DATABASE IF NOT EXISTS when preserving the existing database is acceptable.
Access denied when creating the database
Cause: The connected MySQL account lacks the required create privilege.
Resolution: Connect with an authorized account or ask a database administrator to grant the needed privilege.
The database is not shown
Possible causes: The create statement failed, the account cannot see the database, or the client is connected to a different server.
Resolution: Check the response to CREATE DATABASE, confirm the connection target and account, run SHOW DATABASES; again, and verify privileges.
No database selected
Cause: The current session has not selected a database.
Resolution: Run USE database_name; before creating or querying tables. Alternatively, use a database-qualified table name such as testdb.example_items.
SQL syntax error near the database name
Possible causes: The name contains unsupported characters, conflicts with a reserved word, or the statement is incomplete.
Resolution: Choose a simple identifier, avoid problematic names, and check that the statement is complete and ends with the expected delimiter.
Exam-relevant summary
CREATE DATABASEcreates a named container, not tables or data.SHOW DATABASESlists databases visible to the current account.USE database_name;selects the active database for the current session.- Selection must usually be repeated after opening a new connection.
CREATE DATABASE IF NOT EXISTSavoids an existing-database error without modifying that database.- Creating a database and granting a user access to it are separate tasks.
For a concise reference, the essential sequence is create, verify, and select a MySQL database.