DROP TABLE and ALTER TABLE in SQL - Time & Space Complexity
When we change or remove tables in a database, it takes some time to do so.
We want to understand how this time grows as the table size or structure changes.
Analyze the time complexity of these SQL commands.
ALTER TABLE employees
ADD COLUMN birthdate DATE;
ALTER TABLE employees
DROP COLUMN middle_name;
ALTER TABLE employees
RENAME TO staff;
DROP TABLE staff;
This code removes a table and changes its structure by adding and dropping columns and renaming the table.
Look for repeated work inside these commands.
- Primary operation: Scanning or updating all rows when changing table structure.
- How many times: Once per command, but some commands may touch every row.
Changing or dropping a table depends on how many rows it has.
| Input Size (rows) | Approx. Operations |
|---|---|
| 10 | About 10 row updates or deletions |
| 100 | About 100 row updates or deletions |
| 1000 | About 1000 row updates or deletions |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the time to drop or alter a table grows roughly in step with the number of rows in the table.
[X] Wrong: "Dropping or altering a table always happens instantly, no matter the size."
[OK] Correct: The database often needs to update or remove every row, so bigger tables take more time.
Understanding how table changes scale helps you explain database behavior clearly and confidently.
"What if we only rename a table without changing its data? How would the time complexity change?"
Practice
DROP TABLE do?Solution
Step 1: Understand the purpose of DROP TABLE
The DROP TABLE command removes the whole table and all its data from the database permanently.Step 2: Compare with other commands
ALTER TABLE changes structure but keeps data; DROP TABLE deletes everything.Final Answer:
Deletes the entire table and all its data permanently. -> Option CQuick Check:
DROP TABLE = Delete table and data [OK]
- Thinking DROP TABLE only deletes data but keeps structure
- Confusing DROP TABLE with ALTER TABLE
- Assuming DROP TABLE can be undone easily
age of type INTEGER to a table called users?Solution
Step 1: Identify correct ALTER TABLE syntax
To add a column, use: ALTER TABLE table_name ADD COLUMN column_name data_type;Step 2: Check each option
ALTER TABLE users ADD COLUMN age INTEGER; matches correct syntax; others misuse DROP or omit TABLE keyword.Final Answer:
ALTER TABLE users ADD COLUMN age INTEGER; -> Option BQuick Check:
ALTER TABLE ADD COLUMN = ALTER TABLE users ADD COLUMN age INTEGER; [OK]
- Using DROP TABLE instead of ALTER TABLE
- Omitting TABLE keyword after ALTER
- Using ADD without COLUMN keyword
employees with columns id, name, and salary, what will be the result of this SQL command?ALTER TABLE employees DROP COLUMN salary;
Solution
Step 1: Understand ALTER TABLE DROP COLUMN
This command removes the specified column from the table structure but keeps the table and other data intact.Step 2: Analyze options
Only Thesalarycolumn will be removed from theemployeestable. correctly describes removing the salary column; others describe deleting data or table, or adding columns.Final Answer:
The salary column will be removed from the employees table. -> Option DQuick Check:
ALTER TABLE DROP COLUMN removes column [OK]
- Thinking DROP COLUMN deletes all data
- Confusing DROP COLUMN with DROP TABLE
- Assuming DROP COLUMN adds a column
ALTER TABLE customers DROP age;
Solution
Step 1: Check ALTER TABLE DROP syntax
To remove a column, the syntax requires DROP COLUMN column_name, not just DROP column_name.Step 2: Validate other options
DROP is valid with ALTER TABLE if used correctly; table name is correct; DROP TABLE deletes whole table, not column.Final Answer:
The command is missing the keyword COLUMN after DROP. -> Option AQuick Check:
ALTER TABLE DROP COLUMN required [OK]
- Omitting COLUMN keyword after DROP
- Confusing DROP TABLE with DROP COLUMN
- Assuming DROP alone removes columns
fullname to name in the table students. Which SQL command correctly does this in a standard way?Solution
Step 1: Identify standard rename column syntax
The standard SQL command to rename a column is ALTER TABLE table_name RENAME COLUMN old_name TO new_name;Step 2: Evaluate options
ALTER TABLE students RENAME COLUMN fullname TO name; matches standard syntax; A wrongly uses DROP TABLE; B is MySQL-specific CHANGE syntax; D drops and adds column losing data.Final Answer:
ALTER TABLE students RENAME COLUMN fullname TO name; -> Option AQuick Check:
Rename column = ALTER TABLE RENAME COLUMN [OK]
- Using DROP TABLE instead of ALTER TABLE
- Dropping and adding columns loses data
- Using non-standard syntax without checking compatibility
Ready to prove it?
Pass the test and this pattern moves unknown
