Data Manipulation Language (DML) ๐
Mentor's Note: If DDL is building the house, DML is moving the furniture in, changing the sofa, or throwing out old trash. It's how you manage the actual Information! ๐ก
๐ The Scenario: The Warehouse Manager ๐ฆโ
Imagine you manage a grocery warehouse.
- INSERT: A truck arrives with 50 crates of Apples ๐. You add them to your inventory record.
- UPDATE: The price of Milk ๐ฅ goes up. You change the label on the shelf.
- DELETE: A box of Bananas ๐ is rotten. You remove it from the records.
- The Result: Your inventory stays current and accurate. โ
๐ Key DML Commandsโ
1. INSERTโ
Used to add new rows to a table.
INSERT INTO students (id, name, age)
VALUES (101, 'Vishnu', 25);
2. UPDATEโ
Used to modify existing records. Always use a WHERE clause or you will update every row! โ ๏ธ
UPDATE students
SET age = 26
WHERE id = 101;
3. DELETEโ
Used to remove specific rows.
DELETE FROM students
WHERE id = 101;
๐จ Visual Logic: The DML Impactโ
๐ป Implementation: The Inventory Labโ
- Adding Data
- Changing Data
-- ๐ Scenario: Stocking a new product
-- ๐ Action: Inserting a laptop record
INSERT INTO products (pid, pname, stock)
VALUES (5001, 'Dell XPS', 10);
-- ๐๏ธ Outcome: A new row appears in the table.
-- ๐ Scenario: Flash Sale!
-- ๐ Action: Discounting all laptops
UPDATE products
SET price = price * 0.9
WHERE pname LIKE '%Laptop%';
๐ Sample Dry Run (DELETE)โ
Command: DELETE FROM users WHERE status = 'Inactive'
| ID | Name | Status | Action |
|---|---|---|---|
| 1 | Vishnu | Active | KEEP โ |
| 2 | John | Inactive | DELETE ๐๏ธ |
| 3 | Sara | Active | KEEP โ |
๐ Technical Analysisโ
- Transaction Control: In Oracle, DML changes are not permanent until you run the
COMMITcommand. If you make a mistake, you can undo it usingROLLBACK. ๐ก๏ธ
๐ฏ Practice Lab ๐งชโ
Task: You have a GRADES table. Student 105 was given B by mistake. Change it to A. Then, delete all records where the student has Left the school.
Hint: Use UPDATE and DELETE with specific WHERE clauses. ๐ก
๐ก Interview Tip ๐โ
"Interviewers love the 'Where Clause' question. What happens if you run
UPDATE employees SET salary = 90000without a WHERE clause? Answer: Everyone in the company gets the same salary! ๐ฑ Always double-check your WHERE clause."
๐ก Pro Tip: "The only thing constant is change." - Heraclitus