Create MySQL Database in Three Ways

How to Create MySQL Database in Three Ways?

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)

  1. 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
    login to mysql via windows command prompt
  2. 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;
    create MySql database via windows command prompt
  3. Verify the Database Creation:

    You can confirm that the database was created by listing the databases using the SHOW DATABASES; command:

    SHOW DATABASES;
    show MySQL databases via windows command prompt
  4. Exit the MySQL Command-Line Client:

    You can exit the MySQL command-line client by typing:

    EXIT;
  5. 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

  1. 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).

  2. Log In to phpMyAdmin:

    If you have set a password, you will need to Log in using your MySQL username and password.

  3. 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.

    create new database via phpmyadmin

    On the “Create database” page –

    1. Enter a name for your new database in the “Database name” field.
    2. Choose a default collation (usually, the default is fine).
    3. Click the “Create” button.
    enter the database name and choose collation in phpMyAdmin
  4. Verify the Database Creation:

    After creating the database, you’ll see it listed in the left-hand navigation panel and under the “Databases” section.

    database listed in the left-hand navigation panel of phpMyAdmin

MySQL Workbench: Creating a Database

  1. Connect to a MySQL Server:

    Open your MySQL Workbench, and connect a MySQL server.

  2. 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 –

    1. Click on the Create New Schema icon to create a new database.
    2. Right-click on the Schemas Navigation bar, then click on the Create Schemas.
    MySQL workbench Create New Schema icon to create a new database
    Right-click on the Schemas Navigation bar to create new schema or database in MySQL workbench
  3. 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.
    Mysql workbench enter schema or database name  and choose collation
  4. Apply the review SQL script:
    Mysql workbench enter schema or database name  and choose collation
  5. 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.