MySQL Create Database Tutorial and Examples
In this article, we covers creating a new database using the CREATE DATABASE statement.
A database is a container that includes tables, views, triggers, functions, and stored procedures. To store some data, you must create a table. To create a table, you must create a database first.
In MySQL, the CREATE DATABASE and CREATE SCHEMA statements are used to create a database.
CREATE DATABASE syntax
The CREATE DATABASE statement is used to create a database. The following is the syntax of the CREATE DATABASE statement:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] database_name
[CHARACTER SET charset_name]
[COLLATE collation_name]
[ENCRYPTION {'Y' | 'N'}]
illustrate:
CREATE DATABASEis the same asCREATE SCHEMA.- After 
CREATE DATABASEspecifying the name of the database to be created. IF NOT EXISTSindicates that it will be created only when the specified database does not exist. It is optional.CHARACTER SET charset_namespecifies the character set of the database. It is optional. By default, the configuration of the database server is used.COLLATE collation_namespecifies the collation of the database. It is optional. By default, the configuration of the database server is used.ENCRYPTIONspecifies whether to encrypt the database. It is optional. By default, the configuration of the database server is used.
The rules for MySQL database naming:
- The database name can be up to 64 characters. The length of the name also depends on the operating system.
 - The database name can consist of letters, numbers, underscores, and dollar sign.
 
Although the syntax looks complicated, the most commonly used statement is simple as the following:
CREATE DATABASE db_name;
CREATE DATABASE Examples
Below we use the mysql client tool to create a database named newdb. Please follow the steps below:
- 
Log in to the MySQL database as the
rootuser.mysql -u root -pEnter password of the
rootuser.Note: You can also log in as any other user with permissions to create databases.
 - 
List all databases in the current server using the following statement: the following statement:
SHOW DATABASES;+-----------------------+ | Database | +-----------------------+ | information_schema | | mysql | | performance_schema | | sakila | | sys | +-----------------------+ 5 rows in set (0.01 sec) - 
Create a database name
newdbusing the following statement:CREATE DATABASE newdb;Query OK, 1 row affected (0.02 sec)Here
1 row affectedindicates that the database has been created successfully. - 
Verify that the creation was successful by looking at the list of databases in the current server.
SHOW DATABASES;+-----------------------+ | Database | +-----------------------+ | information_schema | | mysql | | newdb | | performance_schema | | sakila | | sys | +-----------------------+ 6 rows in set (0.01 sec)From the output, we can see that the
newdbdatabase. - 
Finally, we use the
newdbdatabase just created.USE testdb;Database changedThis shows that we have now switched to the
newdbdatabase. Now we can create tables, insert rows, update rows, etc. 
View information about creating a database
We can use the SHOW CREATE DATABASE statement to view information about a database.
SHOW CREATE DATABASE newdb;
+----------+---------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database                                                                                                                 |
+----------+---------------------------------------------------------------------------------------------------------------------------------+
| newdb    | CREATE DATABASE `newdb` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+---------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)From the output we can see that:
- We created the database using the 
CREATE DATABASE ``newdb``command. DEFAULT CHARACTER SET utf8mb4indicates that the default character set isutf8mb4.COLLATE utf8mb4_0900_ai_ciindicates that the default collation isutf8mb4_0900_ai_ci.DEFAULT ENCRYPTION='N'indicates that encryption is not enabled by default.
Conclusion
In this article, we discussed using the usage of the CREATE DATABASE statement. The main points of this article are as follows:
CREATE DATABASEis the same asCREATE SCHEMA.- You specify the name of the database to be created after 
CREATE DATABASE. IF NOT EXISTSindicates that it will be created only when the specified database does not exist. It is optional.- The 
SHOW CREATE DATABASEstatement is used to view information about creating a database. 
If this database is no longer needed, you can drop the database.