Modifying Tables with ALTER TABLE
Modifying Tables with ALTER TABLE: Add, Drop, Modify, and Rename
In real-world software engineering, application requirements evolve constantly. You will frequently need to add a new column to store user profile pictures, expand a character length from 50 to 100, rename a confusing column, or drop a deprecated field. All schema changes on existing tables are performed using ALTER TABLE.
1. Adding New Columns (ADD COLUMN)
To add a new column to an existing table:
2. Removing Columns (DROP COLUMN)
To delete a column and all its stored data:
DROP COLUMN physically removes the data across every row in the table! This action is irreversible. Always verify that no running application code references the column before dropping it.3. MODIFY vs CHANGE: Modifying Existing Columns
When changing column attributes, MySQL provides two distinct commands:
MODIFY COLUMN (Change Data Type or Constraints, Keep Name)
Use MODIFY when you want to alter a column's data type, length, or nullability without changing its name:
CHANGE COLUMN (Rename Column AND Optionally Change Data Type)
Use CHANGE when you want to rename a column:
ALTER TABLE customers RENAME COLUMN phone_number TO contact_phone;4. Renaming Tables
You can rename a table using either ALTER TABLE or the standalone RENAME TABLE command:
5. Adding & Dropping Constraints
6. Best Practices & Common Pitfalls
- Online DDL & Table Locking: On large tables (millions of rows), running
ALTER TABLEcan lock writes or rebuild the entire table on disk, causing application timeouts. Modern MySQL supports Online DDL (ALGORITHM = INPLACE, LOCK = NONE), allowing reads and writes to continue while modifications execute. - Shrinking Column Lengths: Be cautious when reducing a column's width (e.g., from
VARCHAR(255)toVARCHAR(50)). If any existing rows contain strings longer than 50 characters, MySQL in strict mode will abort with an error!
Multiple Choice Questions
1. Which clause adds a new column immediately following an existing column named last_name?
A. INSERT AFTER last_name B. AFTER last_name C. BEHIND last_name D. NEXT TO last_name Answer: B Explanation: In MySQL's ALTER TABLE ... ADD COLUMN syntax, the AFTER <column_name> clause specifies the precise position for the new column.
2. What is the key difference between MODIFY COLUMN and CHANGE COLUMN in MySQL?
A. MODIFY can only delete columns; CHANGE can only create columns B. MODIFY alters data type/constraints while keeping the column name; CHANGE can rename the column as well as alter its definition C. CHANGE is only supported in SQLite D. MODIFY only works on primary keys Answer: B Explanation: MODIFY COLUMN changes a column's definition without altering its name. CHANGE COLUMN requires specifying both old and new names, allowing column renaming and redefinition simultaneously.
3. Which modern MySQL 8.0+ statement renames an existing column phone to mobile_phone cleanly?
A. UPDATE COLUMN phone SET NAME = 'mobile_phone'; B. ALTER TABLE users RENAME COLUMN phone TO mobile_phone; C. RENAME users.phone AS mobile_phone; D. MOVE COLUMN phone TO mobile_phone; Answer: B Explanation: MySQL 8.0 introduced the simplified ALTER TABLE ... RENAME COLUMN old_name TO new_name; statement.
4. What occurs if you attempt to alter a VARCHAR(100) column to VARCHAR(20) when existing rows contain 40-character strings in strict SQL mode?
A. The existing strings are automatically truncated without warning B. MySQL aborts the operation and throws an error to prevent data loss C. The table is converted to a temporary file D. The rows are permanently deleted Answer: B Explanation: Under strict SQL mode (STRICT_TRANS_TABLES), any schema modification that would result in truncation or data loss is aborted with an error.
5. Why is the standalone RENAME TABLE statement favored when performing zero-downtime database deployments?
A. It restarts the server B. It can rename multiple tables in a single atomic transaction C. It compresses data by 90% D. It bypasses user permission checks Answer: B Explanation: RENAME TABLE old TO archive, staging TO production; executes atomically within a single operation, enabling instantaneous zero-downtime table swaps.
Truncating vs Dropping Tables
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Inspecting Tables with DESCRIBE & SHOW CREATE | Truncating vs Dropping Tables |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.