The CREATE DATABASE statement in MySQL is used to create a new database. we can use `CREATE DATABASE` command to add a new database in the Database management system(DBMS).
After creating the database, it allows us to create Tables and the main structures within a database where data is stored. To create a table, use the `CREATE TABLE` statement.
CREATE DATABASE database_name;
CREATE DATABASE [IF NOT EXISTS] databaseName [CHARACTER SET charset_name] [COLLATE collation_name];
CREATE DATABASE: This is the beginning of the statement and indicates that we want to create a new database.
IF NOT EXISTS: This is an optional clause. If specified, it prevents an error from occurring if the database with the specified name already exists. If the database exists, it will not be created again.
databaseName: This is the name we can give to our database. It should follow the rules for naming conventions, such as not using spaces or special characters.
CHARACTER SET charset_name: This is an optional clause that allows us to specify the default character set for the new database. The character set defines the encoding for storing character data in the database.
COLLATE collation_name: This is an optional clause that allows us to specify the default collation for the new database. The collation defines the rules for comparing and sorting character data.
CREATE DATABASE IF NOT EXISTS your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
In the above example, we are creating a new database named "your_database_name" with the UTF-8 character set and the utf8mb4_unicode_ci collation.
After creating the database, we can switch to the newly created database to perform operations within that database. Switch to the newly created database using the "USE" statement.
we can use the below command to switch between databases through the operating system command-prompt / terminal or workbench command prompt.
USE your_database_name;
After switching to the database, we can start creating tables, defining relationships, and performing various data operations within the context of the specified database.