SQL Basics
- What is SQL?
- Data Definition Language
- Data Manipulation Language
- Data Query Language
- Data Control Language
- Transaction Control Language
- Tables and Schemas
- Data Types
- Constraints
Querying and Filtering
Mastering DML in SQL: How to Use INSERT, UPDATE, and DELETE to Handle Data
SQL (Structured Query Language) is a powerful language used to interact with databases. Among its core functionalities, Data Manipulation Language (DML) plays a crucial role in handling the actual data stored in tables. Whether you’re adding a new user to a database, changing a product price, or removing outdated entries, you’re using DML.
In this guide, we’ll explore three fundamental DML commands:
- INSERT– to add new records
- UPDATE– to modify existing records
- DELETE– to remove records
This tutorial is designed for absolute beginners and uses simple language and examples to help you learn effectively.
What is DML (Data Manipulation Language)?
DML is a subset of SQL used to manipulate the data stored in a database table. These operations affect the contents of the tables but do not alter the structure (which is handled by DDL – Data Definition Language).
Key operations in DML:
- Add new data (INSERT)
- Modify existing data (UPDATE)
- Remove data (DELETE)
- Read or query data (SELECT– often categorized under DQL)
DML commands interact directly with rows in tables and are usually part of transactional systems, meaning changes can be rolled back if needed.
1. INSERT – Adding New Records
The INSERT command allows you to add new rows (records) to a table.
Syntax:
INSERT INTO table_name (column1, column2, ...)VALUES (value1, value2, ...);Example:
Suppose we have a table called Customers:
CREATE TABLE Customers (    id INT PRIMARY KEY,    name VARCHAR(50),    email VARCHAR(100),    age INT);To insert a new customer:
INSERT INTO Customers (id, name, email, age)VALUES (1, 'Alice Smith', 'alice@example.com', 28);You can also insert multiple rows at once:
INSERT INTO Customers (id, name, email, age)VALUES(2, 'Bob Lee', 'bob@example.com', 35),(3, 'Carol Jones', 'carol@example.com', 22);💡 Tips:
- Make sure values match the column order.
- Not all columns are always required if default values exist.
- Unique constraints (like primary keys) must be respected.
2. UPDATE – Modifying Existing Records
The UPDATE command is used to change data in one or more rows of a table.
Syntax:
UPDATE table_nameSET column1 = value1, column2 = value2, ...WHERE condition;The
WHEREclause is very important — without it, all rows will be updated!
Example:
Update Alice’s email:
UPDATE CustomersSET email = 'alice.new@example.com'WHERE id = 1;Update multiple columns:
UPDATE CustomersSET age = 29, name = 'Alice S.'WHERE id = 1;Update all records (⚠️ use with caution):
UPDATE CustomersSET age = age + 1;This adds one year to every customer’s age.
💡 Tips:
- Always use WHEREto avoid accidental full-table updates.
- You can use conditions like AND,OR, and comparison operators.
- Test your WHEREclause with aSELECTbefore running anUPDATE.
3. DELETE – Removing Records
The DELETE command is used to remove rows from a table based on a condition.
Syntax:
DELETE FROM table_nameWHERE condition;Example:
Delete the customer with ID 3:
DELETE FROM CustomersWHERE id = 3;Delete all customers under age 25:
DELETE FROM CustomersWHERE age < 25;Delete all rows from a table (⚠️ dangerous):
DELETE FROM Customers;This will remove all records, but not the table itself.
💡 Tips:
- Like UPDATE, always double-check yourWHEREclause.
- Use SELECTto preview which rows would be deleted.
- If supported, use BEGIN TRANSACTIONandROLLBACKfor safety.
DML vs DDL: What’s the Difference?
| Feature | DML | DDL | 
|---|---|---|
| Full Form | Data Manipulation Language | Data Definition Language | 
| Purpose | Deals with actual data | Deals with table structure | 
| Commands | INSERT,UPDATE,DELETE | CREATE,ALTER,DROP | 
| Affects | Table rows (data) | Table design (schema) | 
| Rollback Possible | ✅ Yes (transaction support) | ⚠️ Sometimes not supported | 
Using DML with Transactions
Most SQL databases support transactions, allowing you to group DML commands together. If anything goes wrong, you can ROLLBACK to the previous state.
Example:
BEGIN;
UPDATE CustomersSET age = 40WHERE id = 2;
DELETE FROM CustomersWHERE id = 5;
-- Oops, made a mistake!ROLLBACK;Or, to make changes permanent:
COMMIT;Using transactions helps ensure data integrity during complex operations.
Common Mistakes Beginners Make with DML
- ❌ Forgetting the WHEREclause inUPDATEorDELETE, which can alter or wipe out all rows.
- ❌ Violating constraints, like inserting duplicate primary keys.
- ❌ Using incorrect data types or column order in INSERT.
- ❌ Not testing queries with SELECTbefore running them.
- ❌ Not backing up data before running mass updates or deletions.
Real-Life Use Cases of DML
- 🛒 E-commerce: Add new product listings (INSERT), update prices (UPDATE), remove discontinued items (DELETE).
- 📱 Social Media: Insert new posts, edit content, delete old messages.
- 🏦 Banking: Add new customer data, update account info, delete inactive records.
No matter the industry, DML powers real-time data interactions behind the scenes.
Conclusion
DML commands—INSERT, UPDATE, and DELETE—are the lifeblood of SQL databases. They allow users to interact with and manage data efficiently. Understanding these commands is essential for developers, analysts, and anyone working with structured data.
With practice, you’ll gain the confidence to manipulate data safely and effectively. Just remember: always back up important data, test your queries, and use the WHERE clause wisely.