SQL Data Manipulation (DML)
Learn to modify data in your tables using INSERT, UPDATE, and DELETE.
🔐 Introduction: Database Abstraction
We have learned how to query and manipulate data. Now we explore features that improve database **performance**, **security**, and **organizational structure**—namely, Views and Transactions.
A **View** is essential for security, acting as a virtual table that shows only specific columns to specific users. **Transactions** ensure that complex changes are completed fully, or not at all.
👓 Topic 1: SQL Views (Virtual Tables)
A **View** is a virtual table whose content is defined by a query. Unlike actual tables, a View does not store data itself; it simply saves the **query definition**.
✅ Benefits of Views:
- Security: Restrict users from seeing sensitive columns (like salary).
- Simplicity: Complex join queries can be saved as a single, simple View name.
- Reusability: Use the same query logic multiple times without rewriting it.
💻 Example: Creating a View for Public Access
-- Create a View that excludes the Salary columnCREATE VIEW Public_Employee_Info ASSELECT EmployeeID, Name, DepartmentFROM EmployeesWHERE Department != 'HR';
-- Now query the View just like a normal tableSELECT * FROM Public_Employee_Info;
🔍 Topic 2: SQL Indexes (Performance Booster)
An **Index** is a structure that improves the speed of data retrieval operations on a table. Think of it like the **index in the back of a textbook**: it tells you exactly which page to go to find a topic, rather than reading the whole book.
⚠️ Index Trade-offs:
Indexes speed up SELECT queries but slow down INSERT, UPDATE, and DELETE operations because the index structure must be maintained every time the data changes.
💻 Example: Creating an Index
-- Create an index on the LastName columnCREATE INDEX idx_lastnameON Customers (LastName);
-- Create a unique index (ensures no duplicate values in that column)CREATE UNIQUE INDEX idx_email ON Users (Email);
🏦 Topic 3: Transactions & The ACID Test
A **Transaction** is a sequence of SQL statements that are treated as a single logical unit of work. The transaction must either **all succeed** (commit) or **all fail** (rollback).
This is critical for banking: when transferring money, you must debit Account A *and* credit Account B. If the credit fails, the whole transaction must fail (rollback).
🔬 The ACID Properties:
- Atomicity: All or nothing (the whole transaction succeeds or fails).
- Consistency: The database remains valid (rules are maintained).
- Isolation: Concurrent transactions don't interfere with each other.
- Durability: Changes are permanent after the transaction commits.
💻 Example: Basic Transaction Syntax
START TRANSACTION; -- Begin the single unit of workUPDATE Accounts SET Balance = Balance - 100 WHERE ID = 1;UPDATE Accounts SET Balance = Balance + 100 WHERE ID = 2;COMMIT; -- Save changes permanently
-- If an error occurs, you run: ROLLBACK;
📚 Module Summary
- View: A virtual table (saved query) used for security and simplicity.
- Index: Speeds up
SELECTqueries but slows down DML operations (Insert/Update/Delete). - Transaction: A single, atomic unit of work (All or Nothing).
- ACID: The four guarantees (Atomicity, Consistency, Isolation, Durability) that protect data integrity.
🤔 Interview Q&A
Tap on the questions below to reveal the answers.