๐Ÿ”ง MySQL Change Column Type โ€“ ALTER COLUMN Explained with Examples


๐Ÿงฒ Introduction โ€“ Why Learn to Change Column Types in MySQL?

As your application evolves, so do its data requirements. You might need to change a columnโ€™s data type to:

  • Increase storage (e.g., from INT to BIGINT)
  • Allow longer text (VARCHAR(50) โ†’ VARCHAR(255))
  • Convert a DATE column to DATETIME
  • Fix a bad schema design decision

Learning how to safely and efficiently change column types is crucial for schema maintenance, data migrations, and performance tuning.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to change column types using ALTER TABLE
  • Syntax and real-world examples
  • Best practices and common pitfalls
  • Tools and use cases for schema refactoring

๐Ÿ› ๏ธ Syntax โ€“ ALTER TABLE to Change Column Type

ALTER TABLE table_name
MODIFY COLUMN column_name new_datatype [NULL | NOT NULL];

๐Ÿ“ Note: In MySQL, MODIFY is used to change the type, size, nullability, and default values of existing columns.


๐Ÿ’ก Examples โ€“ MySQL Change Column Type in Action

โœ… Example 1: INT to BIGINT

ALTER TABLE orders
MODIFY COLUMN order_id BIGINT;

๐Ÿ“Œ Use case: When INT max value (2,147,483,647) is no longer sufficient.


โœ… Example 2: VARCHAR(100) to VARCHAR(255)

ALTER TABLE users
MODIFY COLUMN email VARCHAR(255) NOT NULL;

๐Ÿ“Œ Use case: Supporting longer email addresses.


โœ… Example 3: DATE to DATETIME

ALTER TABLE events
MODIFY COLUMN start_time DATETIME;

๐Ÿ“Œ Use case: Storing time component alongside date.


โœ… Example 4: Allow NULL values

ALTER TABLE customers
MODIFY COLUMN phone_number VARCHAR(20) NULL;

๐Ÿ“Œ Use case: Make a field optional instead of mandatory.


โœ… Example 5: Change data type and rename column

ALTER TABLE products
CHANGE COLUMN old_price new_price DECIMAL(10,2);

๐Ÿ“Œ Difference: CHANGE COLUMN is used to rename and change type simultaneously.


๐Ÿงฑ ALTER COLUMN vs CHANGE COLUMN โ€“ Quick Comparison

FeatureMODIFY COLUMNCHANGE COLUMN
Rename column?โŒ Not supportedโœ… Yes
Change data type?โœ… Yesโœ… Yes
Change NULL/DEFAULT?โœ… Yesโœ… Yes

โš ๏ธ Pitfalls & How to Avoid Them

โš ๏ธ Data Truncation
Changing from a larger to smaller data type (e.g., VARCHAR(255) โ†’ VARCHAR(100)) can lose data.

โœ… Check max length before shrinking:

SELECT MAX(CHAR_LENGTH(column_name)) FROM table_name;

โš ๏ธ Column Locking
Large tables may be locked during the schema change, impacting application uptime.

โœ… Use pt-online-schema-change from Percona Toolkit for large tables (zero downtime).


โš ๏ธ Incompatible Conversion
Converting between incompatible types (e.g., TEXT โ†’ INT) will fail or produce incorrect values.

โœ… Always back up data and test the conversion on a staging environment.


๐Ÿ“ฆ Real-World Use Cases

ScenarioChange
Increase precision for currencyFLOAT โ†’ DECIMAL(10,2)
Add time trackingDATE โ†’ DATETIME
Expand product name fieldVARCHAR(50) โ†’ VARCHAR(200)
Schema normalizationCHAR(1) โ†’ ENUM('Y','N')
Optimize storageBIGINT โ†’ INT UNSIGNED (if safe)

๐Ÿงฐ Tools and Techniques

๐Ÿ”„ pt-online-schema-change (Percona)

Avoids table locks during ALTERs on large tables.

pt-online-schema-change --alter "MODIFY COLUMN amount DECIMAL(10,2)" D=shop,T=payments --execute

๐Ÿงช DBeaver / MySQL Workbench

GUI-based editing of column types with validation and preview.


๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Changing column types is a common but critical operation in MySQL schema management. Use ALTER TABLE ... MODIFY or CHANGE with caution and validate with backups.

๐Ÿ” Key Takeaways:

  • Use MODIFY COLUMN to change data types or constraints.
  • Use CHANGE COLUMN to rename and modify together.
  • Test for compatibility and backup data before altering.
  • Use Percona tools for large-scale production tables.

โš™๏ธ Real-World Relevance:
Essential for database versioning, schema refactoring, and performance tuning across evolving apps.


โ“ FAQ โ€“ MySQL Change Column Type

โ“ Whatโ€™s the difference between MODIFY and CHANGE in MySQL?

โœ… MODIFY changes only the column definition; CHANGE also allows renaming.


โ“ Can I change a column type without losing data?

โœ… Yes, if the new type can store all current values. Always check with:

SELECT MAX(CHAR_LENGTH(column)) FROM your_table;

โ“ How to change a column from NOT NULL to NULL?

ALTER TABLE mytable MODIFY COLUMN colname VARCHAR(100) NULL;

โ“ Can I rollback a column type change?

โŒ Not directly. You need to:

  1. Backup data
  2. Recreate original type if needed
  3. Restore data manually

โ“ How to make schema changes without downtime?

โœ… Use pt-online-schema-change for live production tables.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ”ง MySQL Change Column Type

Or Copy Link

CONTENTS
Scroll to Top