DDL (Data Definition Language)
Imagine you are going to open a new toy store. Before selling anything, you need to build the place: put up walls, make shelves, put up labels, decide what goes where.
DDL (Data Definition Language) is like that process of building the space where you are going to store things.
Instead of shelves and walls, with DDL you make:
-
Tables (like boxes to store data).
-
Columns (like divisions to classify information).
-
Databases (like warehouses that store many boxes).
For example:
-
"I want a table to save customers."
-
"This table will have columns for name, email, and phone."
-
"I don't need this table anymore, delete it."
DDL is the part that builds the database from scratch, before putting content in it. What is DDL? (technical definition)
DDL stands for Data Definition Language, and it is a fundamental part of the SQL language. SQL (Structured Query Language) is used to communicate with databases.
The goal of DDL is to create, modify, and delete structures within the database, such as:
-
Tables
-
Views
-
Schemas
-
Indexes
-
Columns
The main DDL commands are:
-
CREATE – creates structures like tables or databases.
-
ALTER – modifies already created structures (for example, adding or changing columns).
-
DROP – deletes structures that are no longer needed.
-
TRUNCATE – deletes all data from a table, but keeps the structure.
DDL does not work with the data directly, but with how it is organized. What is DDL used for?
DDL is used to define and manage the structure of a database. Before you can save information (with DML), you need to build the "container" where that information will live.
With DDL you can:
-
Create new tables to record data.
-
Add new columns if you need more information.
-
Delete tables that are no longer used.
-
Modify names or column types if you made a mistake.
It is the initial step in any database project, because without structure you cannot save anything. Why is it important?
Builds the system's foundation: Everything else (inserting, querying, deleting data) depends on the structure being well designed.
Allows changes and improvements: You can easily adjust the database if business needs change.
Organizes data clearly: A good structure helps data to be well classified and easy to use.
Prevents future errors: Designing the base correctly from the beginning prevents problems like unnecessary columns or poorly organized data.
Common uses of DDL (Real examples)
Create a table to register products:
A store wants to save the products it sells. CREATE TABLE products (...) is used to define name, price, stock, etc.
Add a column for a new need:
It is decided to add a field for "expiration date" in the products table. ALTER TABLE products ADD expiration DATE; is used.
Delete a table that is no longer needed:
A table called “promotions_2024” is no longer used. It is deleted with DROP TABLE promotions_2024;.
Delete all data without deleting the table:
All records in a table are cleared with TRUNCATE TABLE, without deleting the structure.
Practical example
-
CREATE TABLE employees ( -
id INT, -
name VARCHAR(50), -
position VARCHAR(50), -
hire_date DATE -
); -
ALTER TABLE employees ADD email VARCHAR(100); -
DROP TABLE employees; -
TRUNCATE TABLE employees;