CREATE DATABASE
CREATE DATABASE is the command that makes a brand new, empty database on the server. The basic form is simple: CREATE DATABASE school;. After running it, school exists as an empty container, ready to hold tables but it has none yet. A common safe habit is CREATE DATABASE IF NOT EXISTS school;. Without IF NOT EXISTS, trying to create a database that already exists causes an error; with it, MySQL simply does nothing if it's already there, instead of stopping your script with an error. After creating a database, you still need USE school; before you can start creating tables inside it CREATE DATABASE only makes the container, it doesn't switch you into it automatically.
Why this matters
- ·CREATE DATABASE gives every new project a clean, separate space to build in.
- ·IF NOT EXISTS makes setup scripts safe to run more than once without erroring.
- ·Creating a dedicated database per project keeps unrelated data from ever mixing.
Common mistakes
- ·Forgetting IF NOT EXISTS in a setup script can cause it to fail the second time it's run.
- ·Creating many oddly-named 'test' databases and forgetting about them clutters a server over time.
- ·CREATE DATABASE does not automatically USE it a following command can silently apply to the wrong database if you forget to switch.
Query OK, 1 row affected +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | +--------------------+ 4 rows in set
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Frequently asked questions
No. It only creates an empty container. Tables are created separately, inside it, with CREATE TABLE.
Practice questions
Question 1
Write a safe command to create a database called library, without erroring if it already exists.
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Question 2
Create a database called blog, then list all databases to confirm it exists.
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Quick check
Just for this topic answer honestly, then check yourself.
1 / 3
