Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

DDL and DML Operations
SQL

DDL and DML Operations

Master database schema design and data manipulation using CREATE, ALTER, INSERT, UPDATE, and DELETE.

SQL commands are divided into categories based on what they do. The two most fundamental categories are DDL (Data Definition Language) and DML (Data Manipulation Language).

DDL — Data Definition Language

DDL commands define and modify the structure of database objects like tables, indexes, and schemas.

CREATE

Creates a new table or database object:

CREATE TABLE Employees (
    id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    salary DECIMAL(10,2),
    hire_date DATE DEFAULT CURRENT_DATE
);

ALTER

Modifies an existing table structure:

-- Add a column
ALTER TABLE Employees ADD COLUMN email VARCHAR(255);

-- Modify a column type
ALTER TABLE Employees ALTER COLUMN salary TYPE DECIMAL(12,2);

-- Rename a column
ALTER TABLE Employees RENAME COLUMN email TO contact_email;

-- Drop a column
ALTER TABLE Employees DROP COLUMN contact_email;

DROP

Completely removes a table and its data:

DROP TABLE Employees;

Use DROP TABLE IF EXISTS to avoid errors:

DROP TABLE IF EXISTS Employees;

TRUNCATE

Removes all rows but keeps the table structure. Faster than DELETE because it doesn’t generate individual row delete logs:

TRUNCATE TABLE Employees;

DML — Data Manipulation Language

DML commands manage data within the tables.

INSERT

Adds new rows:

INSERT INTO Employees (id, name, salary)
VALUES (1, 'Alice', 60000);

-- Insert multiple rows at once
INSERT INTO Employees (id, name, salary) VALUES
(2, 'Bob', 55000),
(3, 'Charlie', 70000);

-- Insert from another table
INSERT INTO EmployeesArchive
SELECT * FROM Employees WHERE hire_date < '2020-01-01';

UPDATE

Modifies existing rows:

UPDATE Employees
SET salary = 65000
WHERE id = 1;

-- Update multiple columns
UPDATE Employees
SET salary = salary * 1.1, last_raised = CURRENT_DATE
WHERE department_id = 5;

DELETE

Removes rows:

DELETE FROM Employees WHERE id = 1;

-- Delete all rows (slower than TRUNCATE)
DELETE FROM Employees;

DDL vs DML Summary

AspectDDLDML
What it changesStructureData
Auto-committed?Yes (implicit COMMIT)No (transactional)
Can be rolled back?Usually notYes
ExamplesCREATE, ALTER, DROP, TRUNCATEINSERT, UPDATE, DELETE
PerformanceSchema operations are heavyRow-level operations

Transaction Safety

DML commands are transactional — you can roll them back:

BEGIN;
DELETE FROM Employees;
-- Oops, I didn't mean to delete everything
ROLLBACK;
-- All rows are restored!

DDL commands in most databases are auto-committed and cannot be rolled back.

Q: Difference between DELETE and TRUNCATE?

A:

  • DELETE removes rows one by one, logs each row, can be rolled back, and can use a WHERE clause.
  • TRUNCATE removes all rows at once by deallocating data pages, cannot be rolled back in most DBs, and is much faster.

Q: Can you roll back a DDL statement?

A: In most databases (MySQL, PostgreSQL), DDL statements are auto-committed and cannot be rolled back. Some databases like PostgreSQL allow DDL within transactions, but this is an exception.

Q: What is the difference between DROP and TRUNCATE?

A: DROP removes the entire table including its structure. TRUNCATE removes all data but keeps the table structure for future use.

Q: Can INSERT be used with a SELECT statement?

A: Yes. INSERT INTO table SELECT ... copies rows from one table into another, which is useful for archiving or duplicating data.

1. Create a table with constraints.

CREATE TABLE Products (
    id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    price DECIMAL(10,2) CHECK (price > 0),
    category VARCHAR(50) DEFAULT 'General'
);

2. Add a column to an existing table.

ALTER TABLE Products ADD COLUMN description TEXT;

3. Insert data from another table.

INSERT INTO ArchivedOrders
SELECT * FROM Orders WHERE order_date < '2023-01-01';

4. Update salaries for a department.

UPDATE Employees
SET salary = salary * 1.15
WHERE department_id = 3 AND performance_rating = 'A';

5. Delete orders older than a year.

DELETE FROM Orders
WHERE order_date < CURRENT_DATE - INTERVAL '1 year';

6. Truncate a staging table.

TRUNCATE TABLE StagingData;

My Private Notes

Notes are auto-saved locally to this device.