Verify the active database, effective character set, collation, and account privileges before creating tables or loading data.
The CREATE DATABASE statement creates a new database on the MySQL server. A database is a container that holds tables, views, stored procedures, and other objects. You need the CREATE privilege to run this statement.
-- Basic syntax
CREATE DATABASE shop;
-- Avoid error if database already exists
CREATE DATABASE IF NOT EXISTS shop;
-- Create with explicit character set and collation (recommended)
CREATE DATABASE IF NOT EXISTS shop
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
-- List all databases on the server
SHOW DATABASES;
-- Select the database to use for subsequent queries
USE shop;
-- Confirm which database is currently active
SELECT DATABASE();
Always create databases with utf8mb4 character set and utf8mb4_unicode_ci collation. The older utf8 in MySQL is actually a 3-byte subset that cannot store emoji or some rare Unicode characters - utf8mb4 is the true 4-byte UTF-8 encoding.
-- Change the character set and collation of an existing database
ALTER DATABASE shop
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
-- Show the CREATE DATABASE statement (includes charset/collation)
SHOW CREATE DATABASE shop;
-- Drop a database (CAUTION: deletes all tables and data permanently)
DROP DATABASE shop;
-- Drop only if it exists (avoids error)
DROP DATABASE IF EXISTS shop;
-- Full setup for the shop database used throughout this tutorial
CREATE DATABASE IF NOT EXISTS shop
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
USE shop;
-- Verify the database was created with correct settings
SHOW CREATE DATABASE shop\G
-- Check available character sets
SHOW CHARACTER SET LIKE 'utf8%';
-- Check available collations for utf8mb4
SHOW COLLATION WHERE Charset = 'utf8mb4' AND Collation LIKE '%unicode%';
CREATE DATABASE creates a logical container for tables, views, routines, and other database objects. The database name should describe the application or module clearly. In real projects, separate databases are often used for development, testing, staging, and production so data does not get mixed accidentally.
Character set and collation are not minor details. utf8mb4 supports a wide range of Unicode characters, including emoji and many international scripts. Collation controls how text comparison and sorting behave. Choosing these defaults at creation time prevents many text storage and ordering problems later.
CREATE DATABASE inventory_app
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
USE inventory_app;
It prevents text behavior from depending on server defaults.
Text comparison and sort rules, including case and accent behavior.
No. Use IF NOT EXISTS to avoid an error, but still verify the existing settings.
Practice, interview questions, and compiler links for MySQL.
Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.
Explore 500+ free tutorials across 20+ languages and frameworks.
Fresh tutorials, interview guides, and coding practice in your inbox.