CREATE DATABASE creates a database container in a server system; DROP DATABASE removes that container and its data, with permissions and syntax depending on the engine.
A database is a larger boundary than a table. It can hold many tables, views and other objects. These commands belong to administration and setup, so practise with a uniquely named disposable database rather than the database holding your course data.
This reference adds detail to the main course. You can read it independently: the small database below supplies every table and row used in the examples. Start with the question each query answers, then compare the returned values with the data rather than memorising a line of syntax.
Set up the practice database
Run this setup once in a new practice database, before the examples. It uses four customers and six sales. Ana has two sales, Ben has one, Cara has two, and Dev has none.
Sale 106 has no customer link; sale 104 has an unknown amount. These deliberate test cases expose missing information and unmatched relationships.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
city VARCHAR(40),
manager_id INTEGER
);
CREATE TABLE sales (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
amount DECIMAL(10,2),
status VARCHAR(20) NOT NULL,
created_on DATE NOT NULL
);
INSERT INTO customers (id,name,city,manager_id) VALUES
(1,'Ana','Leeds',NULL),(2,'Ben','Leeds',1),
(3,'Cara','Porto',1),(4,'Dev',NULL,2);
INSERT INTO sales (id,customer_id,amount,status,created_on) VALUES
(101,1,10,'paid','2026-01-01'),
(102,1,30,'pending','2026-01-02'),
(103,2,20,'paid','2026-01-02'),
(104,3,NULL,'pending','2026-02-01'),
(105,3,20,'paid','2026-02-02'),
(106,NULL,5,'paid','2026-02-03');An amount of zero would be a known value. The NULL amount here means the value is unknown. A customer with no matching sale differs from a sale whose customer_id is NULL.
The primary keys identify individual records even when amounts or cities repeat. Dates use year-month-day format. The examples specify sorting whenever order matters; insertion order alone does not guarantee display order.
How CREATE DATABASE and DROP DATABASE works
CREATE DATABASE chooses a name and requests a new database container. It does not automatically define customer tables or insert records. Character encoding, collation, ownership and default storage settings may be chosen at creation time or inherited from a template. These choices affect later text comparison and object management. In PostgreSQL, a database is a connection boundary; after creating it you connect to it separately before creating its application tables.
MySQL normally selects a database with USE database_name. SQL Server also supports USE, while PostgreSQL clients establish a new connection or use a client command such as psql’s backslash-c. A client command is not portable SQL and should not be sent through an ordinary SQL driver as if it were a server statement. Check the current database before running schema creation, especially in a tool that keeps several connections open.
What DROP DATABASE removes
DROP DATABASE removes the entire named database, not just its rows or one table. There is no WHERE clause that limits it to selected records. A backup or exported script is not useful unless it can actually restore the required objects and data. Server-specific rules may prohibit dropping the current database or a database with other active connections. Permission to read a table does not imply permission to create or remove a database.
IF EXISTS and IF NOT EXISTS support is not uniform. MySQL offers both forms for the relevant database commands. PostgreSQL supports DROP DATABASE IF EXISTS but not CREATE DATABASE IF NOT EXISTS. PostgreSQL CREATE DATABASE and DROP DATABASE cannot run inside a transaction block. MySQL database DDL commonly causes implicit commits. SQL Server has its own context and permission rules. SQLite has no CREATE DATABASE or DROP DATABASE statement: opening a database file creates or accesses it, and file lifecycle is handled outside this SQL command family.
MySQL, PostgreSQL, SQL Server and SQLite
This CREATE statement is an administration example for PostgreSQL, MySQL or SQL Server when the caller has the required permissions. Additional options and transaction restrictions differ. SQLite uses a database file or in-memory connection instead, so do not execute CREATE DATABASE there. To remove only the disposable server database later, use the documented DROP DATABASE command for that engine.
Check the engine-specific syntax and version requirements in PostgreSQL CREATE DATABASE, PostgreSQL DROP DATABASE.
Practice task and worked solution
In PostgreSQL, verify whether a database called training_reference exists. This read-only check should be run after the creation example in a server connection.
Try the task before reading its worked solution. The supplied rows make the expected answer checkable by hand.
SELECT datname FROM pg_database
WHERE datname='training_reference';datname
training_referencepg_database is PostgreSQL’s database catalog. The filter checks the explicit practice name and does not remove or modify anything. An absent result means that name is not present on the server visible to this connection; it is not proof that a SQLite file or another server has no such database.
On a disposable server, create the named practice database once and verify its catalog entry. Attempting the same creation again should be handled according to the dialect’s duplicate-name behaviour, not assumed idempotent. Reconnect to it and inspect its initially empty application schema. In SQLite practise opening a separate disposable database instead of executing unsupported database-creation SQL.
SQL CREATE DATABASE and DROP DATABASE: Dialect Guide Syntax
CREATE DATABASE and DROP DATABASE
-- PostgreSQL administration example, outside a transaction.
CREATE DATABASE training_reference;SQL CREATE DATABASE and DROP DATABASE: Dialect Guide Summary
CREATE DATABASE namecreates an empty database container; tables are created separately after connecting to it.- Encoding, collation and ownership are chosen at creation time or inherited from defaults, so decide them deliberately.
DROP DATABASEremoves every table, view and row inside it, with noWHEREclause to limit it.IF NOT EXISTSandIF EXISTSmake set-up scripts safe to rerun, where the database supports them.- SQLite has no
CREATE DATABASE: opening a new file creates the database.
SQL CREATE DATABASE and DROP DATABASE: Dialect Guide Examples
Each example shows the code, the output it produces and what each part does.
Creating a disposable practice database
This PostgreSQL example creates a separate disposable database. It does not switch the connection or copy the customers and sales tables into it. The successful command is a state change rather than a row result, and the client’s confirmation wording can differ.
-- PostgreSQL administration example, outside a transaction.
CREATE DATABASE training_reference;No query result set. A successful command creates training_reference.Common SQL CREATE DATABASE and DROP DATABASE: Dialect Guide Mistakes
Dropping or writing to the wrong database
Running DROP DATABASE against the main course database removes every object it contains. Confusing a client tab with a connection target can also create tables in the wrong database. Verify the explicit database name and current connection, and use a separate disposable name for these demonstrations.
SQL CREATE DATABASE and DROP DATABASE: Dialect Guide in Real Projects
Provisioning a database for a new environment
An application deployment may provision an isolated database for a new environment. Choose its ownership, text comparison rules and connection settings deliberately, then run table migrations through that specific connection. Database provisioning and application table creation are separate responsibilities even when a setup tool performs both.
Use a separate provisioning connection with the appropriate administrative role and an explicit environment name. Application migration code should connect to the created container deliberately. Record ownership, encoding and collation decisions before creating production objects, because later text comparisons and operational permissions can depend on these settings rather than the table query alone.
SQL CREATE DATABASE and DROP DATABASE: Dialect Guide FAQ
Does CREATE DATABASE also create my tables?
No. It creates an empty container. Connect to the new database and run the table definitions separately, ideally from migration scripts so every environment ends up with the same structure.
Can I use USE in PostgreSQL?
No. PostgreSQL has no USE statement; reconnect through the client or driver, for example \c shop_test in psql. USE database_name switches databases in MySQL and SQL Server.
Does SQLite support DROP DATABASE?
No. Its database lifecycle is normally a file or connection operation rather than an SQL statement: delete the file to remove the database, and use ATTACH DATABASE 'other.db' AS other to work with a second file in the same connection.
How do I see which databases exist?
In PostgreSQL, run \l in psql or query SELECT datname FROM pg_database. In MySQL, use SHOW DATABASES;, and in SQL Server query sys.databases. The schema metadata lesson shows how to list tables and columns too.
What is the difference between a database and a schema?
A database is the top-level container you connect to. A schema is a named group of tables inside a database, such as PostgreSQL's default public schema or a separate reporting schema. In MySQL the two words mean the same thing: CREATE SCHEMA and CREATE DATABASE are synonyms.
SQL CREATE DATABASE and DROP DATABASE: Dialect Guide Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
After PostgreSQL CREATE DATABASE training_reference succeeds, what else is required before defining tables there?
SQL CREATE DATABASE and DROP DATABASE: Dialect Guide Quiz
2 questions to check what you learned.
What does DROP DATABASE remove?
- Only selected rows in the currently open table
- Only the database name while all tables stay available
- The named database container and its contents
How does SQLite normally create a database?
- By opening a database file or in-memory connection
- By issuing CREATE DATABASE through the SQLite server
- By issuing USE on the current SQLite connection