MySQL
database management
SQL queries
column modification
data definition language

How to change MySQL column definition?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Changing a MySQL column definition is common during schema evolution, but it can affect constraints, defaults, indexes, and application compatibility. The correct SQL statement depends on whether you are renaming the column, changing its type, or both. A safe migration process minimizes lock time and prevents data loss.

MODIFY Versus CHANGE

MySQL supports two related commands in ALTER TABLE.

  • MODIFY COLUMN changes definition but keeps the existing column name.
  • CHANGE COLUMN can rename the column and change definition in one step.
sql
1-- Change type and nullability, keep name
2ALTER TABLE users
3MODIFY COLUMN age INT UNSIGNED NOT NULL;
4
5-- Rename and redefine in one statement
6ALTER TABLE users
7CHANGE COLUMN full_name name VARCHAR(120) NOT NULL;

If you use CHANGE COLUMN, you must specify the new name and full definition, even if only one detail changes.

Always Specify the Full Target Definition

A frequent mistake is changing one property and unintentionally dropping another. MySQL treats the definition in MODIFY or CHANGE as the complete desired state for that column.

sql
1-- Existing column might be: status VARCHAR(20) NOT NULL DEFAULT 'active'
2-- This statement removes NOT NULL and DEFAULT if omitted.
3ALTER TABLE accounts
4MODIFY COLUMN status VARCHAR(20);
5
6-- Safer: include all intended attributes
7ALTER TABLE accounts
8MODIFY COLUMN status VARCHAR(20) NOT NULL DEFAULT 'active';

Before running migrations, inspect current schema:

sql
SHOW CREATE TABLE accounts;

Treat this as your source of truth when building the new definition.

Type Changes and Data Compatibility

Type changes can fail or truncate data if existing values do not fit the target type. For example, shrinking VARCHAR(255) to VARCHAR(50) may silently truncate depending on SQL mode.

sql
1-- Check risky rows before shrinking a text column
2SELECT id, CHAR_LENGTH(description) AS len
3FROM products
4WHERE CHAR_LENGTH(description) > 50;
5
6-- Apply change only after cleanup
7ALTER TABLE products
8MODIFY COLUMN description VARCHAR(50) NOT NULL;

For numeric conversion, validate ranges first.

sql
1SELECT id, score
2FROM metrics
3WHERE score < 0 OR score > 65535;
4
5ALTER TABLE metrics
6MODIFY COLUMN score SMALLINT UNSIGNED NOT NULL;

Pre checks prevent runtime failures during migration windows.

Defaults, Nullability, and Generated Columns

When changing nullability or defaults, coordinate with application writes and existing rows.

sql
1-- Backfill nulls before enforcing NOT NULL
2UPDATE orders
3SET source = 'web'
4WHERE source IS NULL;
5
6ALTER TABLE orders
7MODIFY COLUMN source VARCHAR(20) NOT NULL DEFAULT 'web';

For generated columns, redefine the expression explicitly.

sql
ALTER TABLE invoices
MODIFY COLUMN total_with_tax DECIMAL(10,2)
GENERATED ALWAYS AS (subtotal * 1.13) STORED;

Do not assume generated expression metadata is preserved unless included.

Reducing Lock Impact

Large tables can be blocked by schema changes, depending on MySQL version and operation type. Use online DDL options when available.

sql
1ALTER TABLE events
2MODIFY COLUMN payload JSON,
3ALGORITHM=INPLACE,
4LOCK=NONE;

Some changes still require table copy. Test in staging with realistic data volume and monitor migration time. If downtime risk is high, use online migration tools and phased rollouts.

Production Migration Workflow

A practical sequence for safe changes:

  1. Inspect current table definition.
  2. Validate existing data against the target constraint or type.
  3. Apply backfill updates if needed.
  4. Run ALTER TABLE in staging and measure runtime.
  5. Deploy migration during a controlled window with rollback plan.

For rollback, predefine reverse SQL where possible.

sql
-- Example rollback
ALTER TABLE users
MODIFY COLUMN age INT NULL;

Even if rollback is not always lossless, planning it reduces incident response time.

Common Pitfalls

  • Using CHANGE COLUMN and forgetting that the command requires both column name and full new definition.
  • Omitting NOT NULL or DEFAULT in a MODIFY statement and accidentally removing constraints.
  • Running type shrink operations without checking existing data length or numeric range.
  • Applying heavy ALTER TABLE changes directly in peak traffic windows without lock testing.
  • Assuming index behavior is unchanged after type conversion without validating query plans.

Summary

  • Use MODIFY COLUMN for definition changes and CHANGE COLUMN when renaming is needed.
  • Always declare the full intended column definition in ALTER TABLE.
  • Validate data compatibility before any type or constraint tightening.
  • Plan for lock impact and test migration runtime in staging.
  • Treat schema changes as operational events with monitoring and rollback preparation.

Course illustration
Course illustration

All Rights Reserved.