How to create database in MySQL

Rumman Ansari   Software Engineer   2024-06-11 11:12:15   68  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

To create a database in MySQL, you use the CREATE DATABASE statement followed by the name of the database you want to create. Here's the basic syntax:


CREATE DATABASE database_name;

Replace database_name with the name you want to give to your database. Here's an example:


CREATE DATABASE database_name;

This statement creates a new database named my_database in your MySQL server.

However, you may also want to specify additional options such as character set and collation when creating the database. Here's how you can do that:


CREATE DATABASE database_name
    CHARACTER SET charset_name
    COLLATE collation_name;

  • CHARACTER SET charset_name: Specifies the character set for the database. For example, utf8, utf8mb4, etc.
  • COLLATE collation_name: Specifies the collation for the database, which determines the rules for comparing and sorting characters. For example, utf8_general_ci, utf8mb4_unicode_ci, etc.

Here's an example with character set and collation specified:


CREATE DATABASE my_database
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;

This statement creates a new database named my_database with the character set utf8mb4 and the collation utf8mb4_unicode_ci.

After executing the CREATE DATABASE statement, your database will be created, and you can start creating tables and inserting data into it. You can switch to the newly created database using the USE statement:


USE my_database;

This statement switches the current database context to my_database, so any subsequent SQL statements will be executed in that database.