DML (INSERT, UPDATE, DELETE) in DBMS Theory - Time & Space Complexity
When working with database commands like INSERT, UPDATE, and DELETE, it's important to understand how the time to complete these actions changes as the amount of data grows.
We want to know: how does the work needed increase when we add more rows or update more records?
Analyze the time complexity of the following SQL commands.
-- Insert multiple rows
INSERT INTO employees (name, age) VALUES ('Alice', 30), ('Bob', 25), ('Carol', 28);
-- Update rows matching a condition
UPDATE employees SET age = age + 1 WHERE department = 'Sales';
-- Delete rows matching a condition
DELETE FROM employees WHERE retired = TRUE;
These commands add new data, change existing data, or remove data based on conditions.
Look at what repeats when these commands run.
- Primary operation: Scanning rows to find which ones to update or delete.
- How many times: Once for each row in the table or for each row inserted.
As the number of rows in the table grows, the time to find and change rows grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks or inserts |
| 100 | About 100 row checks or inserts |
| 1000 | About 1000 row checks or inserts |
Pattern observation: The work grows roughly in direct proportion to the number of rows involved.
Time Complexity: O(n)
This means the time to complete the command grows linearly with the number of rows affected or checked.
[X] Wrong: "Updating or deleting a few rows always takes the same time no matter how big the table is."
[OK] Correct: Even if only a few rows change, the database often needs to check many rows to find them, so time depends on table size.
Understanding how data modification commands scale helps you explain database performance clearly and shows you know how data size affects work done.
"What if the table has an index on the column used in the WHERE clause? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of DML commands
DML commands are used to manipulate data in tables: adding, changing, or removing rows.Step 2: Identify the command for adding rows
TheINSERTcommand adds new rows,UPDATEchanges existing rows, andDELETEremoves rows.Final Answer:
INSERT -> Option AQuick Check:
Add new rows = INSERT [OK]
- Confusing UPDATE with INSERT
- Using DELETE to add data
- Thinking SELECT adds data
Solution
Step 1: Recall the UPDATE syntax
The correct syntax isUPDATE table_name SET column = value WHERE condition;to update specific rows.Step 2: Check each option
UPDATE employees SET salary = 5000 WHERE id = 10; matches the correct syntax with a WHERE clause to target employee id 10. Options B and C miss the SET keyword or equal sign. UPDATE employees SET salary = 5000; lacks WHERE, so it updates all rows.Final Answer:
UPDATE employees SET salary = 5000 WHERE id = 10; -> Option CQuick Check:
UPDATE with SET and WHERE = UPDATE employees SET salary = 5000 WHERE id = 10; [OK]
- Omitting SET keyword
- Missing WHERE clause
- Wrong assignment syntax
products with columns id, name, and price. What will be the result of this query?DELETE FROM products WHERE price < 100;
Solution
Step 1: Understand DELETE with WHERE condition
The DELETE command removes rows matching the WHERE condition. Here, it targets rows where price is less than 100.Step 2: Analyze the condition and syntax
The conditionprice < 100correctly selects products priced below 100. The semicolon is optional in many systems but good practice.Final Answer:
All products with price less than 100 will be removed. -> Option BQuick Check:
DELETE with price < 100 removes matching rows [OK]
- Confusing less than with greater than
- Thinking DELETE needs all rows
- Assuming syntax error without semicolon
UPDATE employees SET salary = 6000 WHERE;
Solution
Step 1: Check the UPDATE statement structure
UPDATE requires SET to assign new values and WHERE to specify which rows to update.Step 2: Identify the error in the WHERE clause
The statement ends with WHERE but no condition follows, which is a syntax error.Final Answer:
Missing condition after WHERE clause. -> Option AQuick Check:
WHERE must have a condition [OK]
- Leaving WHERE empty
- Putting value in quotes unnecessarily
- Confusing UPDATE with DELETE
Solution
Step 1: Understand the requirement to increase price by 10%
Increasing by 10% means multiplying the current price by 1.10 (100% + 10%).Step 2: Analyze each option
UPDATE products SET price = price * 1.10 WHERE price < 50; correctly multiplies price by 1.10 for prices less than 50. UPDATE products SET price = price + 10 WHERE price < 50; adds 10 units, not 10%. UPDATE products SET price = price * 0.10 WHERE price < 50; multiplies by 0.10, reducing price. UPDATE products SET price = price + 0.10 WHERE price < 50; adds 0.10 units, a small increase.Final Answer:
UPDATE products SET price = price * 1.10 WHERE price < 50; -> Option DQuick Check:
Multiply price by 1.10 to increase by 10% [OK]
- Adding fixed amount instead of percentage
- Multiplying by 0.10 reduces price
- Forgetting WHERE clause
Ready to prove it?
Pass the test and this pattern moves unknown
