Database DDL Statements Explained: A Complete Guide to Database and Table Operations

    This article explains DDL (Data Definition Language) in relational databases and how it differs from DML, DQL, and DCL. It covers why DDL changes metadata rather than data, why most DDL statements commit implicitly with limited rollback support, and why DDL can be costly on large tables because of locking and structural rebuilds. The guide then walks through core database-level commands for creating, listing, inspecting, and switching databases, with practical SQL examples using conditional creation, explicit character sets and collations, and utf8mb4 to avoid garbled text and support four-byte characters.

    1. Where DDL Fits in the Database Language System

    In relational databases, SQL statements are usually grouped by responsibility: Data Definition Language (DDL), Data Manipulation Language (DML), Data Query Language (DQL), and Data Control Language (DCL). DDL does not process data itself. Instead, it defines the containers and structures that hold data: databases and tables, along with derived objects such as views, indexes, and constraints.

    This distinction matters. DDL operations change metadata in the data dictionary, which gives them two notable characteristics. First, most DDL statements commit implicitly at the underlying layer and offer limited rollback support, so you must confirm them before execution. Second, DDL often involves locks and structural rebuilds, making it far more expensive than ordinary data writes on large tables. Many production incidents trace back to someone running a DDL statement casually, as if it were a routine query.

    The idea that “the core is just create, read, update, and delete” also holds at the database and table level. The only difference is that the target shifts from rows to databases and tables. The rest of this guide follows two main tracks: database operations and table operations.

    2. Database-Level DDL Statements

    A database is a logical namespace, and every table belongs to one. At the database level, the core commands fall into five actions:

    1. -- Create
    2. CREATE DATABASE [IF NOT EXISTS] database_name [CHARACTER SET charset];
    3. -- Drop
    4. DROP DATABASE [IF EXISTS] database_name;
    5. -- List all databases
    6. SHOW DATABASES;
    7. -- Show the creation definition of a database
    8. SHOW CREATE DATABASE database_name;
    9. -- Switch the current database / check the current database
    10. USE database_name;
    11. SELECT DATABASE();

    1. Creating a Database

    The most basic form creates the database directly and raises an error if one with the same name already exists. Production scripts are better off using the conditional form so that repeated execution does not interrupt the workflow:

    1. CREATE DATABASE IF NOT EXISTS bigdata_db;

    Specifying the character set and collation explicitly at creation time is the fundamental way to avoid garbled Chinese text. The default character set comes from the server configuration, and once that default is unsuitable, every table created afterward inherits the problem:

    1. CREATE DATABASE IF NOT EXISTS bigdata_db
    2. CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

    It is best to standardize on utf8mb4 rather than the older three-byte utf8, because only utf8mb4 can fully store four-byte characters such as emoji. Once the character set is fixed at the database level, it becomes the default for new tables in that database. Changing it later requires handling the existing tables as well, which is costly.

    2. Viewing Databases

    Viewing happens at two levels. For listing, SHOW DATABASES returns every database the current account has permission to access. For definitions, SHOW CREATE DATABASE returns the original creation statement, which you can use to confirm whether the character set and collation are