MySQL Create Database Query – Programming Code Examples
MySQL

MySQL Create Database Query2 min read

In MySQL, we create a database using the CREATE DATABASE statement. MySQL implements a database as a directory containing all the files required and the tables in the database.

We can create a database using the MYSQL command line, i.e., mysqladmin or the MySQL Workbench. We will look at both options.




The basic syntax of creating a database is

The “IF NOT EXISTS” ensures that if a database exists already exists, MySQL does not create it again.

The character and collate are optional and can be dropped. MySQL uses the latin1 character set by default. The character set available is:

,

Collation refers to the rules used for comparing characters.

Create a database using MySQL command-line

To  create a database using the mysqladmin utility:

Login to MySQL command-line client with root credentials.

Type the following commands to see all available databases.

Run the create database command

How to create DATABASE in MySQL

To avoid creating a database with a name that may exist already, we run. If the If Exists call is not included, MySQL will throw an error.

The database gets created.

To use the database, we have the command: How to use database in MySQL

The image below also shows what happens if we try to create a database that already exists.

Leave a Comment