Modules / Database / DDL Lab

DDL in SQL

Data Definition Language builds the shape of your database. Design a table column by column, watch the CREATE TABLE statement write itself, then ALTER, TRUNCATE and DROP it to see what happens to the data each time.

not created

DDL: Defining the Shape of Your Data

Before a single row can be stored, something has to declare what a row looks like. That is DDL's job.

Quick Context

Data Definition Language is the subset of SQL that creates and changes structure — tables, columns, types, constraints, indexes. It is distinct from DML, which changes the contents. A useful shorthand: DDL is the blueprint, DML is the furniture.

The Four Commands

  • CREATE — brings a new object into existence. Fails if it already exists (unless you write IF NOT EXISTS).
  • ALTER — changes an existing object: add a column, drop a column, change a type, add a constraint. Existing rows are adjusted to fit.
  • TRUNCATE — removes all rows but keeps the table. Faster than DELETE because it does not log each row individually.
  • DROP — deletes the object entirely: structure, data, indexes, everything.

DDL Usually Cannot Be Rolled Back

In most engines (MySQL and Oracle in particular) DDL statements perform an implicit commit: the moment you run one, any open transaction is committed and the change is permanent. A ROLLBACK afterwards will not save you. PostgreSQL is the notable exception — it supports transactional DDL.

This is why DROP TABLE on the wrong connection is one of the most feared mistakes in this profession. Press the DROP button here and note that the row counter goes to zero with no undo offered.

Constraints: Rules the Database Enforces

Constraints declared in DDL are checked on every write, forever, no matter which application is connecting:

  • PRIMARY KEY — unique and not null; identifies the row.
  • NOT NULL — the column must always have a value.
  • UNIQUE — no two rows may share this value.
  • FOREIGN KEY — the value must exist in the referenced table.
  • CHECK — an arbitrary condition, such as salary > 0.

Putting a rule here rather than in application code means it cannot be bypassed by a script, a migration or a careless intern with a database client.

Interactive Exploration Guide

  1. Build a table and mark one column as PRIMARY KEY. Watch the generated CREATE TABLE update as you type.
  2. Press CREATE, then ALTER · ADD COLUMN. The new column appears on every existing row, filled with NULL — because the rows already existed and had no value to offer.
  3. Press TRUNCATE. Rows go to zero; the columns stay. The blueprint survives.
  4. Press DROP. Both structure and data vanish, and the log records the difference between the two operations.

Key Takeaway

DDL defines the contract your data must obey. Time spent choosing sensible types and constraints up front is repaid every day afterwards — and because these statements are usually irreversible, they deserve more care than any query you will write.

Cheat sheet

DDL in SQL

Data Definition Language builds the shape of your database. Design a table column by column, watch the CREATE TABLE statement write itself, then ALTER, TRUNCATE and DROP it to see what happens to the data each time.

DATABASE · vizlearn.in/database/ddl_in_sql.html