MySQL
varchar
column size
database
SQL tutorial

How to change Column size of varchar type in mySQL?

Master System Design with Codemia

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

Introduction

Changing the size of a VARCHAR column in MySQL is done with ALTER TABLE ... MODIFY COLUMN .... The important part is not the syntax itself but preserving the rest of the column definition and understanding what happens if you shrink the column below the size of existing data.

Basic Syntax

To increase or decrease the length of a VARCHAR column, use MODIFY COLUMN:

sql
ALTER TABLE customers
MODIFY COLUMN email VARCHAR(320);

That changes the maximum allowed length of email to 320 characters. If the column is nullable, has a default value, or is indexed, you should specify the full intended definition instead of relying on memory or assumptions.

For example:

sql
ALTER TABLE customers
MODIFY COLUMN email VARCHAR(320) NOT NULL;

MySQL expects the new definition to describe the column as it should exist after the change.

Preserve Nullability and Defaults

One of the most common mistakes is changing the size but accidentally changing another attribute of the column.

Suppose the original column is:

sql
email VARCHAR(150) NOT NULL DEFAULT '';

Then the safer alteration is:

sql
ALTER TABLE customers
MODIFY COLUMN email VARCHAR(320) NOT NULL DEFAULT '';

If you omit part of the definition, you may unintentionally change NULL handling or defaults depending on the exact starting schema and migration workflow. The safest habit is to inspect the current definition first:

sql
SHOW CREATE TABLE customers;

Increasing vs. Decreasing the Length

Increasing the length is usually straightforward:

sql
ALTER TABLE products
MODIFY COLUMN sku VARCHAR(64) NOT NULL;

Shrinking the length is riskier. If existing values exceed the new size, the operation can fail or force truncation behavior depending on SQL mode and migration tooling.

Before reducing a column from VARCHAR(255) to VARCHAR(100), check the existing data:

sql
SELECT id, name
FROM products
WHERE CHAR_LENGTH(name) > 100;

Do this first. Schema changes should follow data validation, not the other way around.

Character Set and Storage Considerations

VARCHAR(n) is defined in characters, but MySQL storage depends on the column's character set. With multibyte encodings such as utf8mb4, storage can grow quickly, and row-size limits still matter.

That does not usually change the syntax, but it affects the practical limit and index size. If a column is part of a large row or a long index, review the full table design before increasing lengths aggressively.

Indexes and Large Columns

If the VARCHAR column is indexed, a size change can affect index behavior and migration cost. Example:

sql
ALTER TABLE users
MODIFY COLUMN username VARCHAR(191) NOT NULL;

This pattern appears frequently in older utf8mb4 setups because index length constraints were tighter. Modern MySQL versions are better here, but indexed text columns still deserve a quick review before expansion.

Use Migrations in Real Projects

In application code, prefer a schema migration managed by your framework instead of running ad hoc SQL in production. A migration records intent, can be reviewed, and is easier to roll forward safely.

Even if the framework generates the SQL for you, it is still worth understanding the underlying statement so you can spot dangerous changes before deployment.

Common Pitfalls

  • Changing the length but forgetting to preserve NOT NULL or the default value.
  • Shrinking a column without checking whether existing rows already exceed the new limit.
  • Ignoring character set and index implications when expanding text columns.
  • Running direct production SQL instead of using a reviewed migration path.
  • Assuming VARCHAR(n) means the same storage cost under every character set.

Summary

  • Use ALTER TABLE ... MODIFY COLUMN ... to change a VARCHAR size in MySQL.
  • Restate the full column definition so nullability and defaults stay correct.
  • Increasing size is usually easy; shrinking requires checking existing data first.
  • Indexed and multibyte text columns need extra attention.
  • Prefer migration-based schema changes over manual production edits.

Course illustration
Course illustration

All Rights Reserved.