Databases & tables: CREATE, ALTER, DROP
Create databases with utf8mb4, define tables, change them with ALTER TABLE and remove them safely.
Every MySQL server holds many databases (MySQL also calls them schemas), and each database holds tables. In this lesson you'll create both, inspect them, change their structure with ALTER TABLE, and remove them safely. These statements make up SQL's Data Definition Language (DDL).
Creating a database#
IF NOT EXISTSstops the statement from failing if the database is already there, which is handy in scripts you run more than once.- Character set
utf8mb4is real UTF-8, so Hindi, Chinese and emoji 🐘 all fit. It is the default in MySQL 8, but saying it explicitly documents your intent. - Collation decides how text is compared and sorted.
utf8mb4_0900_ai_ciis accent-insensitive (ai) and case-insensitive (ci), so'resume' = 'Résumé'is true. Useutf8mb4_0900_as_csorutf8mb4_binwhen case must matter.
See what exists and how it was defined:
(The /*!80016 ... */ part is a versioned comment: only MySQL 8.0.16 or newer runs what's inside.)
Database names map to folders on disk, so keep them lowercase, with letters, digits and underscores only.
Creating a table#
A table definition lists each column with a name, a data type and optional constraints:
ENGINE = InnoDBpicks the storage engine. InnoDB is the default and the one you want: it supports transactions, row-level locking and foreign keys. (The old MyISAM engine supports none of those.)- A column without
NOT NULL, likebirth_date, allowsNULL, meaning "unknown". - You'll meet every type in the next lesson and every constraint in Constraints.
Inspect the table in two ways:
SHOW CREATE TABLE students\G prints the exact CREATE TABLE statement MySQL would use to rebuild the table, including indexes, engine and character set. It's great for copying a structure or checking what really got created.
Copying tables#
Changing a table with ALTER TABLE#
Requirements change. ALTER TABLE changes a table's structure without losing the data in it.
Add columns. New columns go at the end unless you say FIRST or AFTER col:
Change a type or constraint with MODIFY. You must restate the whole definition, and anything you leave out (like NOT NULL or a default) is dropped:
Rename a column. RENAME COLUMN (MySQL 8.0+) changes only the name. CHANGE renames and redefines in one go:
Change only a default, which is instant because only metadata changes:
Drop a column. Its data is gone for good:
Rename a table:
Check the result:
Notice that BOOLEAN is stored as tinyint(1), with TRUE = 1 and FALSE = 0. MySQL has no separate boolean type.
On big tables,
ALTER TABLEcan take a long time. MySQL 8 does many changes online (ALGORITHM=INPLACE) or even instantly (ALGORITHM=INSTANT, for example adding a column or changing a default). You can ask for a specific algorithm, and MySQL will give an error rather than silently copying the whole table:ALTER TABLE students ADD COLUMN notes TEXT, ALGORITHM=INSTANT;
Temporary tables#
A TEMPORARY table exists only in your current connection and vanishes when you disconnect. It's perfect for intermediate results in a report:
Other connections can't see it, and it can even share a name with a real table (hiding it for your session).
Emptying and removing tables#
DDL statements such as DROP and TRUNCATE commit automatically and can't be rolled back. Before dropping anything important, take a backup (see Backup & restore).
To remove a whole database and every table in it:
Common mistakes#
- Using reserved words as names.
order,groupandkeyare SQL keywords.CREATE TABLE order (...)fails, so useordersinstead, or quote the name with backticks (`order`). - Forgetting part of the definition in
MODIFY/CHANGE.MODIFY name VARCHAR(80)silently removes an existingNOT NULL. Copy the column fromSHOW CREATE TABLEand edit it. - Running
DROPon the wrong server. CheckSELECT DATABASE(), @@hostname;first.
What's next#
Choosing the right data type for each column saves space, prevents bugs and makes queries faster. That's the next lesson.
Check your understanding
Quick quiz
1.Which character set should you use for new MySQL databases so that every language and emoji can be stored?
2.You want to rename the column
fullnametofull_namewithout changing its type. Which statement is simplest in MySQL 8?3.What is the difference between
TRUNCATE TABLE tandDROP TABLE t?
Finished reading?
Mark this lesson complete to track your progress.