TExcelTAD
Home › MySQL › MySQL CREATE and DROP DATABASE

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

mysql · try it yourself
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

sql
CREATE DATABASE database_name;
mysql · try it yourself
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:

mysql · try it yourself
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:

sql
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 utf8 character set in MySQL is an alias for utf8mb3, 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:

mysql · try it yourself
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:

sql
DROP DATABASE database_name;
mysql · try it yourself
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 running DROP DATABASE, and make sure a recent backup exists (see the Users, Privileges and Backups lesson). Production accounts usually don't even have the DROP privilege.

Test yourself

What does DROP DATABASE shop; delete?

  1. Only empty tables
  2. The database and all its tables and data
  3. Only the database name; tables move to mysql
  4. Nothing until you COMMIT

Which statement makes school the current database?

  1. OPEN school;
  2. USE school;
  3. SELECT school;
  4. SET DATABASE school;

Which character set should new MySQL databases use?

  1. latin1
  2. utf8 (utf8mb3)
  3. utf8mb4
  4. ascii