DDL and DML in PostgreSQL
PostgreSQL is a highly versatile and robust relational database, widely used in everything from small projects to enterprise systems. To harness its full power, we need to understand two fundamental SQL categories:
DDL (Data Definition Language): commands to create and modify database objects like tables, indexes, and schemas.
DML (Data Manipulation Language): commands to insert, update, delete, and query the data stored in those objects.
This guide will give a comprehensive understanding of both and how to use them together effectively.
Part 1: Understanding DDL (Data Definition Language)
What is DDL?
DDL commands define the structure of the database. We can create, alter, or delete tables, indexes, constraints, schemas, and other objects with DDL.
Key DDL Commands
CREATE
To create new table in our database
Syntax CREATE TABLE Table_name ( column1, column2,…);
Example
CREATE TABLE books (
book_id SERIAL PRIMARY KEY,
title VARCHAR(150) NOT NULL,
author VARCHAR(100)
);

ALTER
Add a column to existing table:
Syntax
ALTER TABLE table_name ADD COLUMN column_name;
Example
ALTER TABLE books ADD COLUMN genre VARCHAR(50);

To rename a column for clarity:
Syntax
ALTER TABLE table_name RENAME COLUMN old_column_name To New_column_name;
Example
ALTER TABLE books RENAME COLUMN genre To Genre_of_Books;

DROP
To drop the table from the database
Syntax
DROP TABLE IF EXISTS table_name;
Example
DROP TABLE IF EXISTS books;

Part 2: Understanding DML (Data Manipulation Language)
What is DML?
DML commands handle the data inside your tables — adding, retrieving, updating, and deleting records.
Core DML Commands
INSERT — Adding Data
Syntax
INSERT INTO table_name (column1_name, column2_name, column3_name)
VALUES(values1,values2,values3);
Example
INSERT INTO books (title, author, Genre_of_books)
VALUES
('To Kill a Mockingbird', 'Harper Lee', 'Fiction'),
('1984', 'George Orwell', 'Dystopian'),
('The Great Gatsby', 'F. Scott Fitzgerald', 'Classic');

SELECT: Retrieving Data
Syntax
SELECT * FROM table_name;
Example
SELECT * FROM books;

UPDATE: Modifying Records
Syntax
UPDATE table_name
SET column_name = 'New_Values'
WHERE condition;
Example
UPDATE books
SET Genre_of_books = 'Historical Fiction'
WHERE title = 'The Great Gatsby';


DELETE — Removing Data
DELETE FROM table_name WHERE condition;
Example
DELETE FROM books WHERE book_id=3;

✅ Summary Table
Feature | DDL Example | DML Example |
Create | CREATE TABLE books (...) | INSERT INTO books (...) VALUES (...) |
Modify | ALTER TABLE books ADD COLUMN ... | UPDATE books SET ... WHERE ... |
Delete Structure | DROP TABLE books; | DELETE FROM books WHERE ...; |
Access Data | ✗ | SELECT * FROM books; |
Conclusion
Mastering DDL and DML in PostgreSQL is essential for building effective database schemas and managing your data efficiently.
DDL gives you control over the structure of your database — the tables, columns, and relationships.
DML allows you to work with the data itself — inserting new records, updating existing ones, querying results, and deleting when necessary.
Together, they provide the foundation for powerful, scalable, and maintainable PostgreSQL applications.


