MySQL
The CREATE DATABASE statement is used to create a new database in MySQL. A database groups related tables, views, and other objects so your data stays organized.
EmployeeDB).employee_db) for portability and readability.Basic syntax to create a new database:
-- Syntax to create a new database
CREATE DATABASE database_name;
Safer syntax using IF NOT EXISTS:
-- Create database only if it does not already exist
CREATE DATABASE IF NOT EXISTS database_name;
In both cases, database_name is the identifier you choose for the database. Using IF NOT EXISTS prevents an error if someone already created it earlier.
Example 1: Create a simple employee database
-- Create a new database for employee records
CREATE DATABASE EmployeeDB;
Example 2: Use naming convention with lowercase + underscore
-- Create a database using a clean, portable name
CREATE DATABASE employee_db;
Example 3: Create only if it does not already exist
-- Avoid error by checking existence first
CREATE DATABASE IF NOT EXISTS employee_db;
SHOW DATABASES;.For example, after running CREATE DATABASE EmployeeDB;, you can confirm its creation:
-- List all databases to verify creation
SHOW DATABASES;
You should now see EmployeeDB (or employee_db) in the result set, confirming that the database was successfully created.
inventory_db).CREATE DATABASE IF NOT EXISTS in setup scripts to avoid errors on re-runs.SHOW DATABASES; after running the command.school_db using CREATE DATABASE.CREATE DATABASE IF NOT EXISTS school_db; and observe that it does not fail.SHOW DATABASES; and confirm that school_db exists.USE school_db; and create at least one table.