If you’re just starting out with SQL, you’ll quickly realise there’s more to it than just fetching data with SELECT
.
There’s also a whole set of commands that shape and control how your database is built — that’s where DDL comes in.
What is DDL?
DDL stands for Data Definition Language.
In plain English: it’s the part of SQL that deals with defining, modifying, and removing database structures — things like tables, indexes, and constraints.
Unlike your everyday SELECT
query, DDL doesn’t return rows of data.Instead, it changes the blueprint of your database. Think of it as the architecture team, not the data-entry clerk.
The main DDL commands you need to know are:
- CREATE – Make something new
- ALTER – Change what’s already there
- DROP – Delete something completely
1. CREATE – Making Something From Scratch
The CREATE
command is used to create new objects in your database. Most often, you’ll use it to make a table.
Example:
CREATE TABLE
persons ( id INT NOT NULL
, person_name VARCHAR(50) NOT NULL
, birth_date DATE
, phone VARCHAR(15) NOT NULL
, CONSTRAINT pk_persons PRIMARY
KEY (id)
);
What’s going on here:
CREATE TABLE persons
– We’re making a table calledpersons
.id INT NOT NULL
– An integer column calledid
that can’t be empty.person_name VARCHAR(50) NOT NULL
– A text column (up to 50 characters).birth_date DATE
– Stores a date.phone VARCHAR(15) NOT NULL
– Stores a phone number as text.CONSTRAINT pk_persons PRIMARY KEY (id)
– Setsid
as the unique identifier for each row.
2. ALTER – Changing an Existing Table
The ALTER
command lets you tweak your table without recreating it from scratch.
You can add new columns, remove columns, change data types, or rename things.
Example:
ALTER TABLE
personsADD email VARCHAR(50) NOT NULL
;
What’s going on here:
ALTER TABLE persons
– We’re changing thepersons
table.ADD email VARCHAR(50) NOT NULL
– Adds a new column calledemail
that must have a value.
Other common uses of ALTER
include:
DROP COLUMN
– Remove a column.ALTER COLUMN
– Change a column’s data type or nullability.RENAME COLUMN
– Give a column a new name (syntax depends on your SQL flavour).
3. DROP – Removing Something Forever
The DROP
command deletes an entire table or other database object. Warning: this is permanent — once dropped, the data is gone unless you’ve backed it up.
Example:
DROP TABLE
persons;
What’s going on here:
DROP TABLE persons
– Deletes thepersons
table entirely.
You can also drop other objects like indexes, views, or constraints using a similar pattern.
Final Thoughts
If you remember nothing else about DDL, remember this:
- CREATE = build it
- ALTER = change it
- DROP = destroy it (carefully!)
DDL is about setting up and maintaining the structure of your database. Once you’ve got the structure right, that’s when the fun SELECT
queries and analysis come in.