ALTER TABLE operations in MySQL - Time & Space Complexity
When we change a table's structure using ALTER TABLE, the time it takes can vary a lot.
We want to understand how the work grows as the table gets bigger.
Analyze the time complexity of the following code snippet.
ALTER TABLE employees ADD COLUMN birthdate DATE;
ALTER TABLE employees DROP COLUMN middle_name;
ALTER TABLE employees MODIFY COLUMN salary DECIMAL(10,2);
ALTER TABLE employees ADD INDEX idx_lastname (last_name);
This code changes the employees table by adding, dropping, modifying columns, and adding an index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning or rewriting the entire table data.
- How many times: Once per ALTER TABLE command that changes data storage or structure.
When the table is small, changes happen quickly. As the table grows, the work grows too.
| Input Size (rows) | Approx. Operations |
|---|---|
| 10 | About 10 data rows processed |
| 100 | About 100 data rows processed |
| 1000 | About 1000 data rows processed |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the time to complete ALTER TABLE grows roughly in step with the number of rows in the table.
[X] Wrong: "ALTER TABLE always runs instantly no matter the table size."
[OK] Correct: Many ALTER TABLE changes require rewriting all rows, so bigger tables take longer.
Understanding how table changes scale helps you explain database behavior clearly and shows you know practical impacts of schema changes.
"What if we only add an index without changing columns? How would the time complexity change?"
Practice
ALTER TABLE statement do in MySQL?Solution
Step 1: Understand the purpose of ALTER TABLE
The ALTER TABLE command is used to change the structure of an existing table, such as adding or removing columns, without deleting the data inside.Step 2: Compare with other options
Options A, B, and D describe different commands: creating tables, deleting data, or backing up, which are not the purpose of ALTER TABLE.Final Answer:
It changes the structure of an existing table without deleting data. -> Option AQuick Check:
ALTER TABLE modifies table structure [OK]
- Confusing ALTER TABLE with DELETE or DROP commands
- Thinking ALTER TABLE creates new tables
- Assuming ALTER TABLE backs up data
age of type INT to a table called users?Solution
Step 1: Recall correct ALTER TABLE syntax for adding columns
The correct syntax is: ALTER TABLE table_name ADD COLUMN column_name datatype; The keyword COLUMN is optional but recommended.Step 2: Check each option
ALTER TABLE users ADD COLUMN age INT; matches the correct syntax. ALTER users ADD age INT; misses TABLE keyword. ALTER TABLE users ADD age; misses datatype. ALTER TABLE ADD COLUMN age INT TO users; has wrong order.Final Answer:
ALTER TABLE users ADD COLUMN age INT; -> Option CQuick Check:
ADD COLUMN syntax = ALTER TABLE users ADD COLUMN age INT; [OK]
- Omitting TABLE keyword
- Forgetting datatype for new column
- Incorrect order of keywords
employees with columns id INT, name VARCHAR(50), what will be the result of this query?ALTER TABLE employees DROP COLUMN name;
Solution
Step 1: Understand DROP COLUMN in ALTER TABLE
Using DROP COLUMN removes the specified column and all its data from the table structure permanently.Step 2: Analyze the query effect
The query removes the 'name' column from 'employees'. It does not delete the table or cause syntax errors.Final Answer:
The column name will be removed from the table. -> Option DQuick Check:
DROP COLUMN removes column and data [OK]
- Thinking DROP COLUMN deletes the whole table
- Assuming DROP COLUMN sets data to NULL only
- Believing DROP COLUMN causes syntax error
ALTER TABLE orders MODIFY COLUMN order_date VARCHAR(20);
But get an error. What is the likely cause?
Solution
Step 1: Check MODIFY COLUMN syntax validity
MySQL supports MODIFY COLUMN to change a column's data type, so syntax is correct.Step 2: Identify possible causes of error
If the column does not exist, MySQL throws an error. VARCHAR(20) is valid, so the likely cause is the column missing.Final Answer:
The column order_date does not exist in the table. -> Option BQuick Check:
Nonexistent column causes MODIFY error [OK]
- Assuming MODIFY COLUMN syntax is wrong
- Thinking data type change is not allowed
- Ignoring column existence
username to user_name in the accounts table and change its type to VARCHAR(100). Which command is correct?Solution
Step 1: Understand renaming columns in MySQL
MySQL uses CHANGE to rename a column and optionally change its type: CHANGE old_name new_name datatype.Step 2: Evaluate each option
ALTER TABLE accounts CHANGE username user_name VARCHAR(100); uses correct CHANGE syntax. ALTER TABLE accounts RENAME COLUMN username TO user_name VARCHAR(100); uses invalid RENAME COLUMN syntax for MySQL. Options C and D use incorrect keywords.Final Answer:
ALTER TABLE accounts CHANGE username user_name VARCHAR(100); -> Option AQuick Check:
CHANGE renames and modifies column [OK]
- Using RENAME COLUMN which is not supported in MySQL
- Confusing MODIFY with renaming
- Omitting datatype when using CHANGE
Ready to prove it?
Pass the test and this pattern moves unknown
