Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
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.
ADD COLUMN)To add a new column to an existing table:
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.MODIFY vs CHANGE: Modifying Existing ColumnsWhen 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;You can rename a table using either ALTER TABLE or the standalone RENAME TABLE command:
ALTER TABLE can 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.VARCHAR(255) to VARCHAR(50)). If any existing rows contain strings longer than 50 characters, MySQL in strict mode will abort with an error!last_name?MODIFY COLUMN and CHANGE COLUMN in MySQL?phone to mobile_phone cleanly?VARCHAR(100) column to VARCHAR(20) when existing rows contain 40-character strings in strict SQL mode?RENAME TABLE statement favored when performing zero-downtime database deployments?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Inspecting Tables with DESCRIBE & SHOW CREATE | Truncating vs Dropping Tables |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.