To create a MySQL database, you can use various methods, including command-line tools, database management software (e.g., phpMyAdmin, MySQL Workbench), or programming languages that support MySQL database interactions (e.g., PHP, Node, Python).
Below, I will show how to create a MySQL database using command-line tools as well as phpMyAdmin and MySQL Workbench, which provides a graphical user interface.
Table of contents #
SQL Statement or Query to Create a Database
To create a MySQL database, you need to run the following SQL Statement, or the following Query runs behind when you create a MySQL database using a GUI tool like phpMyAdmin.
CREATE DATABASE database_name;
If you try to create a database with a name that already exists, you will get the error (Can’t create database ‘database_name’; database exists).
To prevent the database exists
error you need to verify if a database exists before creating it, simply add ‘IF NOT EXISTS‘ before the database name.
CREATE DATABASE IF NOT EXISTS database_name;
Creating a MySQL Database using Command Line Interface (CLI)
- Access the MySQL Command-Line Client:
Open your terminal or command prompt and then –
Log in to the MySQL server as a user with the necessary privileges. You may be prompted to enter your MySQL password.
mysql -u your_username -p
- Create a New MySQL Database:
Once you’re logged in, you can create a new database using the CREATE DATABASE SQL command. Replace
your_database_name
with your desired name.CREATE DATABASE your_database_name;
- Verify the Database Creation:
You can confirm that the database was created by listing the databases using the SHOW DATABASES; command:
SHOW DATABASES;
- Exit the MySQL Command-Line Client:
You can exit the MySQL command-line client by typing:
EXIT;
- Switch to Database
If you want to run SQL queries for a specific database, such as if you want to create a table inside a database, you have to select that database first, then you can run SQL queries for the database.
To select or switch to a database, use the
USE
keyword:USE database_name;
phpMyAdmin: Creating a MySQL Database
- Access phpMyAdmin:
Open your web browser and navigate to the URL where phpMyAdmin is installed (e.g.,
http://localhost/phpmyadmin
if you are using XAMPP or WAMP on your local machine). - Log In to phpMyAdmin:
If you have set a password, you will need to Log in using your MySQL username and password.
- Create a New Database:
Click on the “New” in the left-hand navigation panel of phpMyAdmin, or you’ll see the “Databases” tab. Click on it.
On the “Create database” page –
- Enter a name for your new database in the “Database name” field.
- Choose a default collation (usually, the default is fine).
- Click the “Create” button.
- Verify the Database Creation:
After creating the database, you’ll see it listed in the left-hand navigation panel and under the “Databases” section.
MySQL Workbench: Creating a Database
- Connect to a MySQL Server:
Open your MySQL Workbench, and connect a MySQL server.
- Create a New Schema (Database):
Once connected to your MySQL server, you will see a list of schemas (databases) in the “Navigator” panel on the left.
Now you can create a database in two ways using MySQL Workbench –
- Click on the Create New Schema icon to create a new database.
- Right-click on the Schemas Navigation bar, then click on the Create Schemas.
- Enter a database name:
- Schema Name: Enter the name for your new database in the “Name” field.
- Default Collation: You can choose the default collation for your database. Leave it as the default setting unless you have specific collation requirements.
- Click on the “Apply” button.
- Apply the review SQL script:
- View the New Schema:
Your database has been created successfully, now click on the “Finish” button.
After the operation is successful, you should see the new schema (database) listed under the “Schemas” folder in the left-hand side Navigator panel.