DEV Community

Cover image for Building vs Running: A Simple Way to Understand DDL & DML.
Josephine Mackylah
Josephine Mackylah

Posted on

Building vs Running: A Simple Way to Understand DDL & DML.

Picture opening Sunrise Supermarket.
Before you sell a single item, you build the shelves tables for customers, products, orders. That's construction.
Once the shelves are up, you run the shop taking orders, updating them, clearing out the cancelled ones.
In SQL, these are two different jobs: DDL and DML.

What is DDL?

DDL stands for Data Definition Language. These are the commands that build and shape your database creating tables, changing their structure, or removing them entirely.
If SQL were a building, DDL pours the foundation and puts up the walls.

What is DML?

DML stands for Data Manipulation Language. These are the commands that work with the data inside those tables adding records, updating them, deleting them, or reading them.
DML is what happens once the building is up and people start moving in.

Common Commands

Type Command What it does
DDL CREATE Builds a new table
DDL ALTER Changes an existing table's structure
DDL DROP Deletes a table entirely
DML INSERT Adds new records
DML UPDATE Changes existing records
DML DELETE Removes records
DML SELECT Reads/retrieves records

Examples of Each Command

CREATE => build a new table

CREATE TABLE customers (
    customer_id SERIAL PRIMARY KEY,
    full_name VARCHAR(100) NOT NULL,
    email VARCHAR(80) UNIQUE NOT NULL,
    phone_number VARCHAR(15) UNIQUE NOT NULL,
    city VARCHAR(50)
);
Enter fullscreen mode Exit fullscreen mode

Table 'customers' created successfully.

ALTER => add a new column

ALTER TABLE customers
ADD COLUMN loyalty_points INT NOT NULL DEFAULT 0;
Enter fullscreen mode Exit fullscreen mode

Table 'customers' altered successfully.

DROP => delete a table entirely

DROP TABLE customers;
Enter fullscreen mode Exit fullscreen mode

Table 'customers' dropped successfully.

INSERT => add a new customer

INSERT INTO customers (full_name, email, phone_number, city)
VALUES ('Grace Wambui', 'grace.wambui@gmail.com', '0711223344', 'Nairobi');
Enter fullscreen mode Exit fullscreen mode

1 row inserted.

UPDATE => change an order's status

UPDATE orders
SET status = 'Delivered'
WHERE order_id = 2;
Enter fullscreen mode Exit fullscreen mode

1 row updated.

DELETE => remove a cancelled order

DELETE FROM orders
WHERE order_id = 4;
Enter fullscreen mode Exit fullscreen mode

1 row deleted.

SELECT => view all orders


SELECT * FROM orders;
Enter fullscreen mode Exit fullscreen mode
order_id customer_id status
1 1 Delivered
2 2 Delivered

This is the simplest way for me to remember this

DDL builds the structure. DML runs the business inside it.

Once you understand the difference between the two, half the confusion around SQL commands disappears.

Top comments (0)