Common MySQL DDL Statements: A Practical Guide

    This article explains the role of DDL in SQL and walks through common MySQL DDL statements for databases and tables, including creation, inspection, modification, deletion, and truncation. It also highlights MySQL's implicit commit behavior and key practices for running schema changes safely in production.

    1. Where DDL Fits in SQL

    When working with a relational database, SQL statements are usually grouped into three categories. Understanding the boundary between them is the prerequisite for using DDL correctly.

    • DDL (Data Definition Language): Defines and modifies database objects themselves, including databases, tables, columns, indexes, views, and constraints. The core keywords are CREATE, ALTER, and DROP. TRUNCATE and RENAME are also commonly grouped here.
    • DML (Data Manipulation Language): Operates on data records inside tables. The core keywords are INSERT, DELETE, UPDATE, and SELECT.
    • DCL (Data Control Language): Manages access permissions and security levels. The core keywords are GRANT and REVOKE.

    The key difference is the target of each category: DDL acts on structure, DML acts on data, and DCL acts on permissions. This distinction is not purely theoretical. It directly determines transaction behavior: in MySQL, most DDL statements trigger an implicit commit and cannot be undone through rollback the way DML can. As a result, structural changes in production must be treated as irreversible operations.

    2. Database-Level DDL

    2.1 Creating and Viewing Databases

    The most basic database-level DDL syntax is:

    1. CREATE DATABASE demo_db;
    2. SHOW DATABASES;

    After a successful run, the client usually returns a message similar to Query OK, 1 row affected (0.00 sec). This feedback can be broken down into three parts:

    • Query OK: The statement executed successfully. MySQL returns this marker for all successful DDL and DML statements (except SELECT), which distinguishes it from some other databases.
    • 1 row affected: The number of affected rows. For structural changes such as creating a database or table, this value is only a placeholder and does not mean a row of data was actually written.
    • 0.00 sec: The execution time, which can be used to roughly gauge the cost of the statement.

    2.2 Why the System Databases Matter

    An instance comes with several built-in databases after installation, each with its own purpose. information_schema stores metadata such as database and table information, column information, permission information, and character set and partition information. The mysql database stores account and permission configuration, and the test database is used for temporary validation.

    It is important to understand that metadata resides in information_schema. Every later need to “inspect structure” is essentially a query against metadata.

    2.3 Modifying and Dropping Databases

    1. ALTER DATABASE demo_db CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
    2. DROP DATABASE demo_db;

    DROP DATABASE removes all tables and data inside the database as well, and it cannot be rolled back. Always confirm the database name and backup status before running it.

    3. Core Table-Level DDL

    3.1 Creating a Table

    The general syntax structure is:

    1. CREATE TABLE table_name (
    2. column_name_1 column_type_1 [constraints],
    3. column_name_2 column_type_2 [constraints],
    4. ...
    5. column_name_n column_type_n [constraints]
    6. );

    The column name identifies the field, the column type determines how values are stored and compared, and constraints declare rules such as NOT NULL, uniqueness, default values, and primary and foreign keys.

    A MySQL table exists on disk as a directory, so the character set available for table names is tied to directory naming rules. Even so, it is still recommended to consistently use lowercase letters, digits, and underscores to avoid case-sensitivity issues.

    Example:

    1. CREATE TABLE emp (
    2. ename VARCHAR(10),
    3. hiredate DATE,
    4. sal DECIMAL(10,2),
    5. deptno INT
    6. );

    Using DECIMAL rather than a floating-point type for monetary fields is a general best practice. Using DATE rather than a string for dates lets the database take on format validation.

    3.2 Viewing a Table Definition

    1. SHOW CREATE TABLE emp;
    2. DESC emp;

    The first statement outputs the full CREATE TABLE statement, which is suitable for copying and version control. The second outputs the column names, types, nullability, default values, and an index overview, which is suitable for a quick scan. For more complex metadata retrieval, query information_schema.COLUMNS.

    3.3 Modifying a Table

    ALTER TABLE is the most common tool for schema evolution:

    1. ALTER TABLE emp ADD COLUMN dept_name VARCHAR(20);
    2. ALTER TABLE emp MODIFY COLUMN sal DECIMAL(12,2);
    3. ALTER TABLE emp CHANGE COLUMN ename emp_name VARCHAR(20);
    4. ALTER TABLE emp DROP COLUMN dept_name;
    5. ALTER TABLE emp ADD INDEX idx_deptno (deptno);
    6. ALTER TABLE emp ENGINE=InnoDB;

    Distinguish MODIFY from CHANGE: the former changes only the type and constraints, while the latter can change the column name at the same time. Changing a column type may trigger a full table rebuild, so on large tables you should assess the lock duration and remaining disk space.

    3.4 Dropping and Truncating

    1. TRUNCATE TABLE emp;
    2. DROP TABLE emp;

    TRUNCATE clears the data but keeps the structure, and it is usually more efficient than deleting row by row. DROP TABLE removes the structure as well. Neither can be rolled back.

    4. DDL Practices for Production

    1. Always back up before a change: Structural changes cannot be rolled back, so a logical backup is the most basic safety net.
    2. Prefer additive changes that can be rolled back: Adding a column or an index carries lower risk than changing a column type.
    3. Watch locks and duration on large tables: Run changes during business off-peak hours, and use batched or tool-based online change approaches when necessary.
    4. Keep scripts under version control: Store CREATE and change statements in a repository so environments can be rebuilt.
    5. Standardize naming and character sets: Standardize on utf8mb4 and naming conventions to reduce later migration costs.
    6. Verify after the change: Use metadata queries to confirm that columns, indexes, and character sets match expectations.

    Mastering these DDL statements and their execution behavior covers the vast majority of everyday database and table design and evolution scenarios, and applies equally to cloud databases and self-managed instances.