MySQL Create Database
A MySQL database is a named container that holds all the tables, views, and other objects belonging to one application, so creating it correctly is the first real step in building anything with MySQL.
What CREATE DATABASE Does
The CREATE DATABASE statement asks the MySQL server to allocate a new schema on disk with its own namespace for tables, views, stored procedures, and triggers. Once created, a database is selected with USE before you can create tables inside it, or you can qualify every statement with the database name (for example, shop.customers).
MySQL treats 'database' and 'schema' as synonyms, so CREATE DATABASE and CREATE SCHEMA do exactly the same thing. Database names can contain letters, digits, underscores, and dollar signs, and are case-sensitive on Linux servers but not on Windows, so it is good practice to always type them consistently.
Basic Syntax
Create a simple database
CREATE DATABASE company;Running this once succeeds. Running it a second time raises an error because the database already exists. To avoid that error in scripts that may run more than once, add the IF NOT EXISTS clause.
Create a database safely
CREATE DATABASE IF NOT EXISTS company;Choosing Character Set and Collation
Every database has a default character set (how characters are encoded) and collation (how strings are compared and sorted). New tables inherit these defaults unless you override them, so it pays to set them explicitly when the database is created rather than relying on the server default.
Create a database with UTF-8 settings
CREATE DATABASE company
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;- utf8mb4 stores full Unicode, including emoji, unlike the older utf8 alias which only covers 3-byte characters.
- A _ci suffix on a collation means case-insensitive comparisons; _cs means case-sensitive; _bin compares raw bytes.
- You can inspect available character sets with SHOW CHARACTER SET and available collations with SHOW COLLATION.
Selecting and Inspecting Databases
After creating a database you must switch into it before creating tables. The USE statement changes the active database for the current session, and SHOW DATABASES lists every database the connected user is permitted to see.
Switch into a database
USE company;Naming and Maintenance Tips
Exercise: MySQL Create Database
What is the correct statement to create a new database named shop?