DDL (data definition language) is the subset of SQL whose statements define and change the structure of a database rather than the data stored in it. In databases, DDL commands such as CREATE, ALTER, DROP, TRUNCATE and RENAME describe tables, columns, constraints, indexes, views and schemas, and the database engine records every such definition in its system catalog.
DDL sits beside the other SQL sublanguages: DML reads and modifies rows, DCL grants and revokes privileges, and transaction control statements commit or roll back work. Understanding where DDL ends and DML begins matters in practice, because the two families differ in locking behavior, transactional guarantees and deployment risk.
How DDL statements shape the database catalog
A DDL statement changes metadata, the description of objects that the engine keeps in internal catalog tables. When a CREATE TABLE statement runs, the engine validates the column definitions, writes entries to the catalog, allocates storage and makes the new object visible to later queries. Every subsequent SELECT or INSERT relies on that metadata to resolve names, data types and constraints.
The catalog is exposed to users through views such as the ANSI-standard `INFORMATION_SCHEMA` and through vendor-specific views like pg_catalog in PostgreSQL or sys.objects in SQL Server. Because DDL alters these shared structures, the engine usually takes a metadata lock on the affected object, which blocks or waits for concurrent queries that touch the same table.
The table below lists the core DDL statements and what each one does to the schema.
| Statement | Typical targets | Effect on the schema |
|---|---|---|
| CREATE | Table, index, view, schema, sequence | Adds a new object to the catalog |
| ALTER | Table, column, constraint, index | Changes the definition of an existing object |
| DROP | Any named object | Removes the object and its data permanently |
| TRUNCATE | Table | Deallocates all rows while keeping the definition |
| RENAME | Table, column, index | Changes the name recorded in the catalog |
| COMMENT | Table, column | Stores descriptive text as metadata |
DDL compared with DML, DCL and TCL
SQL is traditionally divided into four sublanguages, and DDL is the one that works on definitions instead of rows. The division is conceptual rather than strict: the SQL standard groups statements into schema, data, transaction and session categories, and vendors classify some commands differently. TRUNCATE, for example, empties a table like a DELETE but is treated as DDL in most engines because it deallocates storage pages instead of removing rows one by one.
The comparison below shows how the sublanguages differ in purpose and in operational behavior.
| Sublanguage | Purpose | Example statements | Usual rollback behavior |
|---|---|---|---|
| DDL | Defines structure | CREATE, ALTER, DROP | Depends on the engine |
| DML | Reads and changes rows | SELECT, INSERT, UPDATE, DELETE | Fully transactional |
| DCL | Controls access | GRANT, REVOKE | Varies by engine |
| TCL | Manages transactions | COMMIT, ROLLBACK, SAVEPOINT | Ends or reverts work |
The practical consequence is that a migration script mixing DDL and DML may behave differently across platforms. A statement that is safely reversible in PostgreSQL can be permanent in MySQL or Oracle Database, so teams cannot assume that a failed script leaves the schema untouched.
Transactional DDL and implicit commits
Transactional DDL means that schema changes participate in a transaction and can be rolled back together with data changes. PostgreSQL, SQL Server, SQLite and IBM Db2 support this for most statements, so a migration that creates a table, copies data and adds a constraint either succeeds completely or leaves no trace.
Other engines issue an implicit commit before and after each DDL statement. In MySQL and MariaDB, running ALTER TABLE inside an open transaction silently commits the earlier DML, and Oracle Database behaves the same way. MySQL 8.0 introduced atomic DDL, which makes a single statement all-or-nothing, but it still does not let several DDL statements share one rollback scope.
| Engine | Rollback of DDL | Notable behavior |
|---|---|---|
| PostgreSQL | Yes, for most statements | Some commands such as CREATE INDEX CONCURRENTLY cannot run in a transaction |
| SQL Server | Yes | Schema locks can block readers during changes |
| MySQL | No | Implicit commit; atomic per statement since 8.0 |
| Oracle Database | No | Implicit commit before and after each statement |
| SQLite | Yes | Limited ALTER TABLE support in older versions |
Writing DDL for production schemas
Good DDL encodes business rules as constraints so the database enforces them regardless of which application writes the data. A well-designed CREATE TABLE declares a primary key, appropriate NOT NULL and CHECK constraints, foreign keys with deliberate ON DELETE actions and data types sized for the real value range, as in CREATE TABLE orders (id BIGINT PRIMARY KEY, customer_id BIGINT NOT NULL REFERENCES customers(id), total NUMERIC(12,2) CHECK (total >= 0)).
Most teams manage DDL through versioned migration scripts stored with application code and applied in order by a migration tool. Each script is reviewed, tested on a copy of production data and applied once, which gives a reproducible schema history. Declarative approaches instead compare a desired schema file with the live database and generate the needed DDL automatically.
The decision table below summarizes common situations and the safer way to express them in DDL.
| Situation | Recommended approach | Reason |
|---|---|---|
| Adding a column to a large table | Add it as nullable or with a constant default | Avoids rewriting every row on many engines |
| Adding an index on a busy table | Use an online or concurrent index build | Prevents long write locks |
| Renaming a column used by live code | Add new column, migrate, then drop old one | Keeps old and new code versions working |
| Removing a table | Rename or archive first, drop later | Allows recovery if something still depends on it |
| Tightening a constraint | Add it as not validated, then validate | Splits locking from the full table scan |
Risks and common mistakes with DDL
DDL failures tend to be more disruptive than DML failures because they affect every query that touches the object. The most frequent problems come from locking, irreversibility and drift between environments.
- Lock queues: an
ALTER TABLEwaiting for a long-running query can block all new queries behind it, so setting a lock timeout is a standard safeguard. - Full table rewrites: changing a column type or adding a volatile default may rewrite the entire table and hold an exclusive lock for minutes or hours.
- Irreversible drops:
DROPandTRUNCATEusually bypass row-level logging, so recovery depends on backups or point-in-time restore. - Schema drift: manual changes applied directly in production leave environments inconsistent and break later migrations.
- Vendor syntax differences: DDL is the least portable part of SQL, with different data types, identity columns and index options in each engine.
A non-technical view
Think of a database as a large filing room. DDL is the set of instructions for building the furniture: which cabinets exist, how many drawers each has and what kind of document belongs in each drawer. It does not file any papers itself.
Other SQL commands put papers in, take them out or change them. DDL only decides the layout. Because changing the layout affects everyone who uses the room, structural changes need planning, while everyday filing can happen at any moment.
A concrete example
An online retailer stores about 80 million order rows in a PostgreSQL table and needs a new column for the delivery method. A developer first writes ALTER TABLE orders ADD COLUMN delivery_method TEXT without a default, which PostgreSQL applies as a quick catalog change instead of a table rewrite.
Next, the team needs an index on the new column. Instead of a plain CREATE INDEX, which would block writes during checkout hours, they run CREATE INDEX CONCURRENTLY with a lock timeout of five seconds. The build takes about twenty minutes, but customers keep placing orders throughout.
The migration is stored as a versioned script and tested on a staging copy first. The schema change reaches production with no downtime, and the same script later rebuilds identical structures in every test environment.