SQL Keywords

SQL DATABASE

What is the SQL DATABASE statement?

Manages an entire database by creating, altering, dropping, or setting it as the current connection context.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL DATABASE: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite (via ATTACH/DETACH), Snowflake

SQL DATABASE Full Explanation

The DATABASE keyword represents operations that act on a whole logical database rather than on individual tables or schemas. Vendors expose these operations through statements such as CREATE DATABASE, ALTER DATABASE, DROP DATABASE, and USE (to switch the current database). Although the exact options differ by vendor, the core behavior is consistent: a database is created in the server catalog, optional properties (owner, encoding, collation, file location) can be changed later, it can be removed entirely, and a session can be pointed to it so unqualified object references resolve inside that database.Caveats:- In the SQL standard, CREATE SCHEMA is defined, while CREATE DATABASE remains vendor specific.- Dropping a database is irreversible; all contained objects are deleted.- Some engines (SQLite) implement the concept through ATTACH/DETACH rather than CREATE DATABASE.- Permissions to execute DATABASE statements are typically restricted to super-users or admins.

SQL DATABASE Syntax

CREATE DATABASE database_name
    [WITH option = value [, ...]];

ALTER DATABASE database_name
    option = value;

DROP DATABASE [IF EXISTS] database_name;

-- MySQL and SQL Server
USE database_name;

SQL DATABASE Parameters

  • - database_name (identifier) - Name of the database to act upon
  • - option (varies) - Optional settings like OWNER, ENCODING, LC_COLLATE, CHARACTER SET, FILEGROUP, MAXSIZE, etc.
  • - IF EXISTS (keyword, optional) - Suppresses an error if the specified database does not exist when dropping.

Example Queries Using SQL DATABASE

-- Create a new database with explicit owner and encoding (PostgreSQL)
CREATE DATABASE analytics
    WITH OWNER = analyst
    ENCODING = 'UTF8';

-- Switch the session to the new database (MySQL)
USE analytics;

-- Change default collation (SQL Server)
ALTER DATABASE analytics
    COLLATE Latin1_General_CS_AS;

-- Remove a sandbox database if it exists (MySQL/PostgreSQL)
DROP DATABASE IF EXISTS sandbox;

Expected Output Using SQL DATABASE

  • - CREATE DATABASE adds analytics to the server catalog and allocates storage
  • - USE analytics changes the current database context; subsequent queries run inside analytics
  • - ALTER DATABASE updates metadata so new objects inherit the specified collation
  • - DROP DATABASE permanently deletes sandbox, its tables, and all data

Use Cases with SQL DATABASE

  • Provisioning a new environment for an application or micro-service.
  • Separating staging and production data sets on the same server.
  • Changing ownership or default encoding before loading data.
  • Cleaning up obsolete test databases in CI pipelines.
  • Switching a client session to the correct database before running queries.

Common Mistakes with SQL DATABASE

  • Confusing DATABASE with SCHEMA; CREATE SCHEMA does not create a separate catalog in most systems.
  • Forgetting to switch context with USE, leading to queries running against the wrong database.
  • Dropping a database without backups.
  • Assuming IF EXISTS is available in every dialect (Oracle does not support it).
  • Passing table-level options (e.g., STORAGE) to CREATE DATABASE.

Related Topics

CREATE DATABASE, ALTER DATABASE, DROP DATABASE, USE, SCHEMA, CREATE SCHEMA, GRANT

First Introduced In

Vendor implementations; popularized in MySQL 3.23 (1998) and SQL Server 2000

Frequently Asked Questions

What permissions are required to create a database?

Most systems require superuser, sysadmin, or the CREATEDB privilege. Regular users cannot create databases unless explicitly granted.

How is CREATE DATABASE different from CREATE SCHEMA?

CREATE DATABASE creates an entirely new catalog. CREATE SCHEMA defines a namespace inside an existing database. Some vendors map databases to schemas (e.g., Snowflake).

Does SQLite support CREATE DATABASE?

SQLite databases are individual files. Instead of CREATE DATABASE, you use ATTACH DATABASE to open another file in the same session.

Can I change the name of a database?

Renaming is vendor specific. PostgreSQL supports ALTER DATABASE RENAME TO, while MySQL requires creating a new database and migrating objects.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!