SQL / MySQL Beginner Relational Design · 7 min read

CREATE DATABASE and Character Set utf8mb4

Lesson 1 of 33 in the SQL / MySQL learning path.

❮ SQL / MySQL Home

How do I create a MySQL database that stores Unicode correctly?

Creating the school database is the first concrete step in a registrar, and it happens before any student, course, or enrollment row exists. MySQL stores text using the character set and collation on that database, and tables created later inherit both settings. The utf8mb4 character set holds full Unicode, so a student name can include accents, Chinese characters, and emoji. The collation utf8mb4_unicode_ci defines how those characters compare and how they sort.

The script creates school, selects it, sets the client character set, and inserts four users so the names can be read back with character and byte counts. The people table is named users because each student signs in as a user account, which is also why this path continues at sql-create-users. Run it on MySQL 8. InnoDB is the default engine and the engine the later lessons use for keys, constraints, and multi-statement work.

Syntax & Parameters

Syntax
CREATE DATABASE db_name
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

USE db_name;

Example

schema.sql
CREATE DATABASE school
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

USE school;

SET NAMES utf8mb4;

DROP TABLE IF EXISTS enrollments;
DROP TABLE IF EXISTS courses;
DROP TABLE IF EXISTS users;

CREATE TABLE users (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(120) NOT NULL,
  email VARCHAR(254) NOT NULL,
  password_hash VARCHAR(255) NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_users_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO users (name, email, password_hash, created_at)
VALUES ('Ada Lovelace', 'ada@school.test', 'sample-hash-not-a-secret', '2026-01-15 09:00:00');

INSERT INTO users (name, email, password_hash, created_at)
VALUES ('José García', 'jose@school.test', 'sample-hash-not-a-secret', '2026-01-15 09:05:00');

INSERT INTO users (name, email, password_hash, created_at)
VALUES ('李娜', 'lina@school.test', 'sample-hash-not-a-secret', '2026-01-15 09:10:00');

INSERT INTO users (name, email, password_hash, created_at)
VALUES ('Sam 🎓', 'sam@school.test', 'sample-hash-not-a-secret', '2026-01-15 09:15:00');

SELECT id, name, CHAR_LENGTH(name) AS chars, LENGTH(name) AS utf8_bytes
FROM users
ORDER BY id;

SHOW CREATE DATABASE school;

Expected Output

id | name         | chars | utf8_bytes
1  | Ada Lovelace | 12    | 12
2  | José García  | 11    | 13
3  | 李娜         | 2     | 6
4  | Sam 🎓       | 5     | 8
4 rows in set

Database | Create Database
school   | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */ /*!80016 DEFAULT ENCRYPTION='N' */
1 row in set

Line-by-Line Walkthrough

  1. CREATE DATABASE school names the registrar database and sets the default character set to utf8mb4 with the collation utf8mb4_unicode_ci in the same statement.
  2. Tables added later in school inherit that character set, so student names and course titles share one Unicode definition.
  3. USE school makes school the current database, and the following statements apply there without a database prefix.
  4. SET NAMES utf8mb4 aligns this client connection with the database so accents, Chinese characters, and emoji are not mangled on the way in.
  5. The three DROP TABLE statements clear an older practice copy, children first, so the script can be run again after later lessons have created enrollments.
  6. CREATE TABLE users stores an integer id, a name, a unique email, a password hash, and created_at as DATETIME on the InnoDB engine with utf8mb4.
  7. The four INSERT statements store an ASCII name, a name with accents, a two-character Chinese name, and a name that includes an emoji.
  8. CHAR_LENGTH counts characters and LENGTH counts bytes, which is how the emoji row shows a four-byte character.
  9. SHOW CREATE DATABASE prints the character set MySQL saved, including utf8mb4 and utf8mb4_unicode_ci.
  10. The older MySQL charset name utf8 is an alias of utf8mb3 and stops at three bytes per character, so the emoji row is the proof that this database is using utf8mb4.

Common Pitfalls & Security

The character set on the database does not repair a connection that still speaks a three-byte charset. If the mysql client, a dump, or PHP sends bytes MySQL cannot store, the INSERT fails or the name is already corrupted before the column sees it. DROP DATABASE school deletes every table in that database at once and has no row filter, so it belongs on a practice server you are willing to empty.

  • Running CREATE DATABASE school a second time raises error 1007 because the name is taken. CREATE DATABASE IF NOT EXISTS is the form that continues when school already exists.
  • SET NAMES lasts for one connection. The next PHP or GUI session needs its own utf8mb4 setting, often the PDO charset option, or the emoji insert fails on an otherwise correct database.
  • A table created under an older database default keeps that old character set. New defaults apply to tables you create after the change.
  • With utf8mb4_unicode_ci, Ada@school.test and ada@school.test compare as equal, so a unique email index treats them as the same address.
  • Accents are not a reliable identity either: José and Jose often compare as equal under this collation. Keep the integer id and the unique email as the real identifiers.
  • DROP DATABASE and DROP TABLE are permanent. They are not undone by a missing WHERE clause because they do not use WHERE at all. Back up a live registrar before either statement.

Best Practices & Modern Standards

Choose utf8mb4 when you create the database, and use the same character set on every connection, including the charset option in the PHP PDO DSN. MySQL 8 also provides the collation utf8mb4_0900_ai_ci, which many servers use as their default and which sorts with a newer Unicode algorithm. This lesson uses utf8mb4_unicode_ci because that collation is widely taught and still stores full Unicode. Keep one collation for users, courses, and enrollments so unique checks and joins compare text the same way.

Treat school as a database you create once. A classroom script may drop and recreate it. A live registrar should be created, backed up, and then changed with reviewed migrations. The next lesson builds the users table that holds each student account.

Real-World Use

A school enrollment system starts as an empty database named school. Students will be stored as rows in users, courses in courses, and the link between a student and a course in enrollments. Those tables inherit utf8mb4 from this database, so a legal name with an accent or characters outside basic English survives registration, class lists, and transcripts. Setting the character set before the first student signs up avoids a later rewrite of every enrollment row.

Frequently Asked Questions

Why does MySQL already have a charset named utf8?

In MySQL the name utf8 is an alias of utf8mb3. That charset stores at most three bytes per character. Emoji and some rare characters need four bytes, so this lesson creates the database with utf8mb4, which is the full Unicode charset in MySQL 8.

Does the database collation apply to every new table?

A table created without its own CHARSET and COLLATE clauses inherits the database character set and collation. A table that already exists keeps the character set it was created with until you alter that table.

Will utf8mb4 use more disk space than the older utf8 alias?

Letters in a course code still use one byte. Extra bytes are used only by characters that need them. That extra range is what lets a student name store an accent, a Chinese character, or an emoji.

Can the character set be changed after students are enrolled?

ALTER TABLE ... CONVERT TO CHARACTER SET utf8mb4 can convert a column, and it rewrites the rows. The safe point to choose utf8mb4 is before the first enrollment is loaded, while the database is still empty.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…