DDL

Also known as: Data definition language

On this page 7

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.

StatementTypical targetsEffect on the schema
CREATETable, index, view, schema, sequenceAdds a new object to the catalog
ALTERTable, column, constraint, indexChanges the definition of an existing object
DROPAny named objectRemoves the object and its data permanently
TRUNCATETableDeallocates all rows while keeping the definition
RENAMETable, column, indexChanges the name recorded in the catalog
COMMENTTable, columnStores descriptive text as metadata
Core DDL statements and their effect

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.

SublanguagePurposeExample statementsUsual rollback behavior
DDLDefines structureCREATE, ALTER, DROPDepends on the engine
DMLReads and changes rowsSELECT, INSERT, UPDATE, DELETEFully transactional
DCLControls accessGRANT, REVOKEVaries by engine
TCLManages transactionsCOMMIT, ROLLBACK, SAVEPOINTEnds or reverts work
SQL sublanguages side by side

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.

EngineRollback of DDLNotable behavior
PostgreSQLYes, for most statementsSome commands such as CREATE INDEX CONCURRENTLY cannot run in a transaction
SQL ServerYesSchema locks can block readers during changes
MySQLNoImplicit commit; atomic per statement since 8.0
Oracle DatabaseNoImplicit commit before and after each statement
SQLiteYesLimited ALTER TABLE support in older versions
DDL transaction support in common engines

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.

SituationRecommended approachReason
Adding a column to a large tableAdd it as nullable or with a constant defaultAvoids rewriting every row on many engines
Adding an index on a busy tableUse an online or concurrent index buildPrevents long write locks
Renaming a column used by live codeAdd new column, migrate, then drop old oneKeeps old and new code versions working
Removing a tableRename or archive first, drop laterAllows recovery if something still depends on it
Tightening a constraintAdd it as not validated, then validateSplits locking from the full table scan
Choosing a DDL approach for common changes

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 TABLE waiting 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: DROP and TRUNCATE usually 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.