Sub topic 1
DDL: Statements That Change the Schema
Prerequisites
This chapter assumes that you know basic SELECT, INSERT, UPDATE, and DELETE statements. You should also understand CREATE TABLE, common data types, primary keys, and foreign keys. You need access to a test database such as PostgreSQL, MySQL, SQL Server, or SQLite.
What DDL Changes
DDL means Data Definition Language. DDL statements define and change database objects, such as tables, columns, constraints, indexes, and views. A table's structure is called its schema.
DML means Data Manipulation Language. DML changes the rows stored inside existing objects. INSERT, UPDATE, and DELETE are DML statements.
| Category | Main purpose | Examples |
|---|---|---|
| DDL | Change database structure | `CREATE`, `ALTER`, `DROP` |
| DML | Change stored rows | `INSERT`, `UPDATE`, `DELETE` |
For example, adding a column changes the table's schema. Updating a customer's email changes data in an existing column.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
INSERT INTO customers (id, name)
VALUES (1, 'Amina');
ALTER TABLE customers
ADD COLUMN email VARCHAR(255);
UPDATE customers
SET email = '[email protected]'
WHERE id = 1;The CREATE TABLE statement creates the table. The INSERT statement adds a row. The ALTER TABLE statement adds a column. The UPDATE statement changes a value in that column.
The Main DDL Operations
ALTER TABLE changes an existing table without recreating it from scratch. Depending on the database system, it can:
- Add or remove columns.
- Change a column's data type.
- Rename columns.
- Add, change, or remove constraints.
- Add or remove default values.
- Change whether a column accepts
NULL.
DROP TABLE removes the table itself. It normally removes the table's rows, columns, constraints, and indexes as part of that operation.
A DDL change can affect application code. If you drop or rename a column, queries that use the old column can fail. If you add a NOT NULL column, existing rows must receive valid values.
DDL Is Database-Specific
SQL syntax is not identical across database systems. For example, PostgreSQL, MySQL, SQL Server, and SQLite differ in how they change column types, rename objects, and handle transactions around DDL.
Before running a script, identify:
- The database product and version.
- Whether the operation can run inside a transaction.
- Whether the operation takes a lock that blocks reads or writes.
- How the system handles dependent foreign keys, views, and indexes.
Later chapters examine these operations and differences in detail. The central model is simple: DDL changes the shape or existence of database objects, while DML changes the rows those objects contain.
Exercise
Is ALTER TABLE customers ADD COLUMN phone VARCHAR(30) DDL or DML, and why?
Show answer
It is DDL because it changes the structure of the customers table by adding a column. It does not change an existing row value.