๐ What is DML (Data Manipulation Language)?
DML (Data Manipulation Language) is the part of SQL used to interact with the actual data stored in tables โ reading, inserting, updating, and deleting records.
If the database structure (tables, columns) is a house built using DDL, then DML is how you live in it โ putting stuff in, changing things around, or throwing stuff out.
๐ง Key DML Commands:
| Command | What It Does |
|---|---|
SELECT | Retrieves data from one or more tables |
INSERT | Adds new rows to a table |
UPDATE | Modifies existing rows in a table |
DELETE | Removes rows from a table |
๐ ๏ธ Example Usage:
1. INSERT
INSERT INTO users (id, name, email)
VALUES (1, 'Gaurav', 'gaurav@example.com');Adds a new row to the
userstable.
2. SELECT
SELECT name, email FROM users WHERE id = 1;Fetches specific data from the table.
3. UPDATE
UPDATE users SET email = 'new@email.com' WHERE id = 1;Changes the email of the user with ID 1.
4. DELETE
DELETE FROM users WHERE id = 1;Deletes the user with ID 1 from the table.
๐งช DML + Transactions:
DML operations can be rolled back using TCL (Transaction Control Language):
BEGIN;
DELETE FROM users WHERE id = 1;
ROLLBACK; -- undoes the deleteSo, DML changes are not permanent until committed.
โ ๏ธ DML vs DDL:
| Feature | DDL | DML |
|---|---|---|
| Affects | Structure (schema) | Data (rows/records) |
| Auto-commit | Yes | No (can rollback) |
| Example | CREATE TABLE | INSERT INTO table |
๐ง Interview Line:
โDML allows users to interact with data using
SELECT,INSERT,UPDATE, andDELETE. Unlike DDL, DML operations are transactional and can be committed or rolled back.โ