Creating & Managing Databases
With a MySQL server running and a client connected, the very first real thing to build is a
database — a named container that groups together every table belonging to
one project. In this lesson we create streamhub, the database every remaining
lesson in this track builds inside.
What a "Database" Actually Is in MySQL
A single MySQL server can happily host many separate databases at once — think of each one as
its own isolated workspace. Tables in the streamhub database (like users
and movies) have nothing to do with tables in some unrelated database sitting on
the same server; they're grouped only for organization and access control, not merged together.
Creating the StreamHub Database
Creating a database is a single statement:
CREATE DATABASE streamhub;
Run that in your connected mysql client and MySQL creates an empty database named
streamhub, ready to hold tables. Nothing visible happens beyond a short
confirmation — there's nothing to see yet because no tables exist inside it.
Listing Every Database on the Server
To confirm it was actually created, list every database the server currently knows about:
SHOW DATABASES;
| Database |
|---|
| information_schema |
| mysql |
| performance_schema |
| streamhub |
| sys |
streamhub now sits alongside MySQL's own internal databases, confirming it exists.
Selecting a Database With USE
Creating a database doesn't make it "active." Before running table-level commands, tell MySQL which database you mean by unqualified table names:
USE streamhub;
It's worth understanding exactly what this does: USE is a session-level
setting. It doesn't alter anything permanently in the database itself — it simply tells your
current client connection "assume streamhub for any table name I mention from now
on." Close that session and reconnect, and you'll need to run USE streamhub; again
before referring to tables like movies without spelling out
streamhub.movies in full.
Character Sets and Collations
When you create a database, MySQL also decides how text inside it gets stored and compared — controlled by a character set (which bytes represent which characters) and a collation (the rules for sorting and comparing those characters). It's good practice to be explicit about both rather than trusting whatever the server's default happens to be:
CREATE DATABASE streamhub
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
The specific choice of utf8mb4 over plain utf8 matters more than it
sounds like it should. MySQL's utf8 character set is a legacy implementation that
only supports Unicode characters up to 3 bytes long — which quietly excludes a large range of
valid Unicode, including most emoji and some less common characters used in real names. A
StreamHub username or review comment containing an emoji would either fail to insert or get
silently mangled under plain utf8. utf8mb4 supports the full 4-byte
Unicode range and is the character set every new MySQL database should use today — this is a
genuinely common gotcha that catches people who copy older tutorials using plain utf8.
Dropping a Database
Removing a database entirely uses DROP DATABASE:
DROP DATABASE streamhub;
DROP DATABASE deletes every table and every row inside that
database immediately and permanently. There is no confirmation prompt in a script, no "undo,"
and no recycle bin to recover from — once it runs, that data is gone unless you have a backup.
Never run this against a database you care about without being certain, and see
Backup & Restore later in this track for how to
protect yourself against exactly this risk before it becomes a problem.
Renaming a Database
Somewhat surprisingly, MySQL has no direct RENAME DATABASE statement. If you
created streamhub_test and want it called streamhub instead, the
common workarounds are:
- Create the new, correctly-named database, then move each table into it individually with
RENAME TABLE old_db.table_name TO new_db.table_name;for every table. - Take a full dump of the old database's structure and data, create the new database, and restore the dump into it — the more reliable option for a database with many tables or complex foreign keys.
Either way, the old database still needs a separate DROP DATABASE afterward if you
want it gone, since neither approach removes the original automatically.
streamhub created and selected, head to Data Types to decide exactly what kind of data each future column should hold before we create any tables.