MySQL: MySQL Database Operations and Character Set…
Last updated: 2026-08-26
A database is a container for storing data—working with a database is the first step in using MySQL.
This lesson will teach you how to perform CRUD operations on databases and configure character sets.
graph TB
A[Database Operations] --> B[CREATE DATABASE<br/>Create a Database]
A --> C[SHOW DATABASES<br/>View Database]
A --> D[USE<br/>Select a Database]
A --> E[ALTER DATABASE<br/>Modify the character set]
A --> F[DROP DATABASE<br/>Delete the database]
B --> B1[Specify a character set utf8mb4]
E --> E1[Modify the character set/Sorting Rules]
1. What You'll Learn
- Create a database and specify the character set
- View and select a database
- Change the database character set
- Delete the database
- A Detailed Explanation of Character Sets and Collation Rules
2. A True Story About a Multilingual Website
(1) Pain Point: Garbled Chinese characters
A developer created an e-commerce database and inserted Chinese product names:
INSERT INTO products (name) VALUES ('iPhone');
SELECT * FROM products;
Result: name is displayed as ??? or garbled characters.
Reason: The database's default character set is latin1, which does not support Chinese.
(2) Solution for utf8mb4
-- Specify utf8mb4 character set when creating the database
CREATE DATABASE ecommerce
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
Benefits: Supports all Unicode characters, including Chinese, Japanese, Korean, and emojis.
| Character Set | Supported Range | Recommendation Level |
|---|---|---|
| latin1 | Western European characters | ❌ |
| utf8 | Basic Multilingual (does not support 4-byte characters) | ⚠️ |
| utf8mb4 | Full Unicode (including Emoji) | ⭐⭐⭐ |
3. Create a Database
(1) Basic Syntax
CREATE DATABASE [IF NOT EXISTS] database_name
[CHARACTER SET charset_name]
[COLLATE collation_name];
▶ Example: Creating a Database
Output:
Query OK, 1 row affected
Query OK, 1 row affected
Query OK, 1 row affected
Query OK, 1 row affected
-- The Simplest Way to Create
CREATE DATABASE mydb;
-- Create it if it doesn't exist (Recommended)
CREATE DATABASE IF NOT EXISTS mydb;
-- Specify a character set (Recommended)
CREATE DATABASE IF NOT EXISTS mydb
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
-- Creating an E-commerce Database
CREATE DATABASE IF NOT EXISTS ecommerce
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
Output:
Query OK, 1 row affected (0.01 sec)
Output:
Output displayed
4. View the Database
▶ Example: View all databases
-- View All Databases
SHOW DATABASES;
-- View the statement used to create the database
SHOW CREATE DATABASE mydb;
-- View the currently used database
SELECT DATABASE();
Output:
+--------------------+
| Database |
+--------------------+
| ecommerce |
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
5. Select a Database
▶ Example: The USE command
-- Select a Database
USE ecommerce;
-- Verify the current database
SELECT DATABASE();
Output:
+------------+
| DATABASE() |
+------------+
| ecommerce |
+------------+
6. Modify the Database
▶ Example: Changing the character set
-- Modify the Database Character Set
ALTER DATABASE mydb
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
-- Verify Changes
SHOW CREATE DATABASE mydb;
Output:
+----------+------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------------------------------+
| mydb | CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */ |
+----------+------------------------------------------------------------------------------------------+
7. Delete the Database
▶ Example: Deleting a Database
-- Delete the database
DROP DATABASE mydb;
-- Secure Deletion (If it exists)
DROP DATABASE IF EXISTS mydb;
Output:
Output displayed
8. Character Sets and Sorting Rules
(1) Common Character Sets
| Character Set | Byte Length | Supported Characters | Use Cases |
|---|---|---|---|
| ASCII | 1 | English + Numbers | All-English scenarios |
| latin1 | 1 | Western European characters | Legacy system compatibility |
| utf8 | 1-3 | Basic Multilingual | Backward Compatibility |
| utf8mb4 | 1-4 | Full Unicode | Recommended default |
(2) Sorting Rules
Sorting rules determine how strings are compared and sorted.
| Sorting Rule | Description | Case-Sensitive |
|---|---|---|
| utf8mb4_general_ci | General-purpose, fast | Case-insensitive |
| utf8mb4_unicode_ci | Accurate, supports multiple languages | Case-insensitive |
| utf8mb4_bin | Binary Comparison | Case-Sensitive |
_ci indicates case-insensitive comparison, and _bin indicates binary comparison (case-sensitive).
▶ Example: Viewing character sets
-- View All Character Sets
SHOW CHARACTER SET;
-- View All Sorting Rules
SHOW COLLATION LIKE 'utf8mb4%';
-- View the server's default character set
SHOW VARIABLES LIKE 'character_set_server';
-- View the database character set
SHOW VARIABLES LIKE 'character_set_database';
Output:
+--------------------------+-----------------------------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------------------------+
| character_set_server | utf8mb4 |
+--------------------------+-----------------------------------------------+
Output:
Output displayed
9. Database Backup and Recovery
❓ FAQ
information_schema:SELECT table_schema, ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS size_mb FROM information_schema.tables GROUP BY table_schema;ALTER DATABASE to change the database character set, use ALTER TABLE to change the table character set, and use CONVERT TO to convert existing data.📖 Summary
- CREATE DATABASE: Create a database; we recommend using
IF NOT EXISTSandutf8mb4 - USE Select the current database; all subsequent operations will be performed in that database.
- SHOW DATABASES to view all databases, SHOW CREATE DATABASE to view the creation statement
- ALTER DATABASE Modifies database properties (character set, collation)
- DROP DATABASE Deletes the database. Be sure to verify that a backup has been made before proceeding.
- utf8mb4 is the recommended character set; it supports the full Unicode character set and emojis
- mysqldump is used for database backups, and mysql < file.sql is used for restoration
📝 Exercises
-
Basic Problem (Difficulty ⭐): Create a database named
schoolwith the character setutf8mb4and the collationutf8mb4_unicode_ci. -
Advanced Exercise (Difficulty ⭐⭐): Review the creation statement for the
schooldatabase to confirm that the character set is configured correctly. Then, create a test table and insert data containing emojis to verify support. -
Challenge (Difficulty: ⭐⭐⭐): Write a backup script to back up the
schooldatabase to thebackup/directory, with filenames that include the date (e.g.,school_20260703.sql).