MySQL CREATE and DROP DATABASE
A MySQL server holds many databases (MySQL also calls them schemas; the two words mean the same thing here). Each database is a namespace for tables, views and stored programs. Usually one application gets one database: shop for the online store, blog for the blog, and so on.
Listing databases
SHOW DATABASES;+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| shop |
| sys |
+--------------------+
Besides our shop, every server has a few system databases:
| Database | Contents |
|---|---|
mysql |
user accounts, privileges and other server settings |
information_schema |
read-only views describing every database, table and column |
performance_schema |
low-level runtime statistics |
sys |
friendlier views over performance_schema |
Never create your own tables in them.
CREATE DATABASE
CREATE DATABASE database_name;CREATE DATABASE school;
SHOW DATABASES LIKE 'sch%';+-----------------+
| Database (sch%) |
+-----------------+
| school |
+-----------------+
Creating a database that already exists is an error. IF NOT EXISTS turns that into a harmless note, which makes setup scripts safe to run twice:
CREATE DATABASE IF NOT EXISTS shop;
CREATE DATABASE shop;ERROR 1007 (HY000) at line 2: Can't create database 'shop'; database exists
Character set and collation
A database has a default character set (how text is encoded) and collation (how text is compared and sorted). New tables inherit them. Always use utf8mb4, the full Unicode encoding that also stores emoji:
CREATE DATABASE school
CHARACTER SET utf8mb4
COLLATE utf8mb4_0900_ai_ci;utf8mb4_0900_ai_ci is MySQL 8's default: ai = accent-insensitive, ci = case-insensitive. MariaDB uses different collation names (utf8mb4_general_ci, utf8mb4_uca1400_ai_ci), so in a MariaDB script you'd leave the COLLATE part out or pick one of those.
Note: The older
utf8character set in MySQL is an alias forutf8mb3, which stores at most 3 bytes per character and can't hold emoji. Don't use it for new databases.
Switching databases: USE
A connection has a current database. Unqualified table names refer to it. USE changes it, and DATABASE() tells you which one is current. You can always reach another database's table with database.table:
SELECT DATABASE();
CREATE DATABASE school;
USE school;
CREATE TABLE pupils (name VARCHAR(30));
INSERT INTO pupils VALUES ('Ada'), ('Linus');
SELECT DATABASE();
SHOW TABLES;
SELECT COUNT(*) AS shop_customers FROM shop.customers;+------------+
| DATABASE() |
+------------+
| shop |
+------------+
+------------+
| DATABASE() |
+------------+
| school |
+------------+
+------------------+
| Tables_in_school |
+------------------+
| pupils |
+------------------+
+----------------+
| shop_customers |
+----------------+
| 15 |
+----------------+
DROP DATABASE
DROP DATABASE deletes a database with every table and all data in it. There is no undo and no confirmation prompt:
DROP DATABASE database_name;CREATE DATABASE scratch;
CREATE TABLE scratch.t (x INT);
DROP DATABASE scratch;
DROP DATABASE IF EXISTS scratch;
SHOW DATABASES LIKE 'scr%';
SELECT 'done' AS result;+--------+
| result |
+--------+
| done |
+--------+
SHOW DATABASES LIKE 'scr%' printed nothing because nothing matched. The second DROP didn't fail thanks to IF EXISTS.
Warning: On a real server, check
SELECT DATABASE();and the name you typed twice before runningDROP DATABASE, and make sure a recent backup exists (see the Users, Privileges and Backups lesson). Production accounts usually don't even have theDROPprivilege.
Test yourself
What does DROP DATABASE shop; delete?
- Only empty tables
- The database and all its tables and data
- Only the database name; tables move to mysql
- Nothing until you COMMIT
Which statement makes school the current database?
- OPEN school;
- USE school;
- SELECT school;
- SET DATABASE school;
Which character set should new MySQL databases use?
- latin1
- utf8 (utf8mb3)
- utf8mb4
- ascii