How to create database in MySQL

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

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:

<span class="pln">
CREATE DATABASE database_name</span><span class="pun">;</span><span class="pln">
</span>

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

<span class="pln">
CREATE DATABASE database_name</span><span class="pun">;</span><span class="pln">
</span>

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:

<span class="pln">
CREATE DATABASE database_name
    CHARACTER SET charset_name
    COLLATE collation_name</span><span class="pun">;</span><span class="pln">
</span>
  • 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:

<span class="pln">
CREATE DATABASE my_database
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci</span><span class="pun">;</span><span class="pln">
</span>

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:

<span class="pln">
USE my_database</span><span class="pun">;</span><span class="pln">
</span>

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


No Questions Data Available.
No Program Data.

Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.