ALTER COLUMN — Change a Data Type
Change an existing column's data type or size with ALTER COLUMN — and learn how MySQL and PostgreSQL write it differently.
A new overseas signing has a long name that doesn't fit in VARCHAR(50). You need the name column to hold up to 100 characters — same column, bigger type. In SQL Server that is ALTER COLUMN.
Definition
ALTER COLUMN is the part of
ALTER TABLEthat changes the definition of an existing column — its data type, its size, or whether it allows NULL — while keeping the column's name, position and (when they can be converted) its existing values.
- Data type / size — e.g.
VARCHAR(50)→VARCHAR(100), orINT→BIGINT. - NULL / NOT NULL — e.g. make an optional column required.
- Keeps values — SQL Server converts each existing value to the new type. If any value cannot be converted, the whole statement fails and nothing changes.
Syntax (SQL Server)
sql
ALTER TABLE table_name
ALTER COLUMN column_name new_data_type [NULL | NOT NULL];| Part | Meaning |
|---|---|
ALTER TABLE table_name | The table to change |
ALTER COLUMN column_name | The column to change |
new_data_type | The full new type, e.g. VARCHAR(100) |
NULL / NOT NULL | Restate the nullability — if you leave it out, SQL Server makes the column NULL-able! |
Example — VARCHAR(50) → VARCHAR(100)
Before:
| Column | Type |
|---|---|
| id | INT |
| name | VARCHAR(50) |
| team | VARCHAR(10) |
| country | VARCHAR(30) |
| runs | INT |
sql
ALTER TABLE players
ALTER COLUMN name VARCHAR(100) NOT NULL;After — same column, bigger type:
| Column | Type |
|---|---|
| id | INT |
| name | VARCHAR(100) |
| team | VARCHAR(10) |
| country | VARCHAR(30) |
| runs | INT |
Step by step: 1️⃣ ALTER TABLE players → 2️⃣ ALTER COLUMN name with the new type. The rows (Virat Kohli, Rohit Sharma…) are untouched.
Note: In the course slide the statement is
ALTER TABLE players ALTER COLUMN name VARCHAR(100). That works, but becausenamewasNOT NULL, it's best to repeatNOT NULL— otherwise SQL Server silently changes the column to allow NULL.
Widening vs narrowing
| Change | Safe? | Why |
|---|---|---|
VARCHAR(50) → VARCHAR(100) | ✅ Safe | Every existing value still fits |
INT → BIGINT | ✅ Safe | Bigger range |
VARCHAR(100) → VARCHAR(10) | ⚠️ Risky | Fails if any value is longer than 10 characters ("String or binary data would be truncated") |
VARCHAR → INT | ⚠️ Risky | Fails if any value is not a number (e.g. 'N/A') |
DECIMAL(5,2) → INT | ⚠️ Data loss | Decimals are cut off |
Check your data before narrowing a column:
sql
SELECT MAX(LEN(name)) AS longest_name
FROM players;| longest_name |
|---|
| 14 |
The longest name ("Jasprit Bumrah") has 14 characters, so even VARCHAR(20) would be safe.
Syntax differs by database
| Database | Change the data type |
|---|---|
| SQL Server | ALTER TABLE players ALTER COLUMN name VARCHAR(100); |
| MySQL | ALTER TABLE players MODIFY COLUMN name VARCHAR(100); |
| PostgreSQL | ALTER TABLE players ALTER COLUMN name TYPE VARCHAR(100); |
| SQLite (playground) | ❌ Not supported — you rebuild the table instead |
Renaming a column (bonus)
Renaming is a different operation from changing a type:
sql
-- SQL Server
EXEC sp_rename 'players.name', 'player_name', 'COLUMN';
-- MySQL 8+ / PostgreSQL
ALTER TABLE players RENAME COLUMN name TO player_name;Common mistakes
- ❌ Forgetting
NOT NULLwhen altering a required column. ✅ Restate it:ALTER COLUMN name VARCHAR(100) NOT NULL. - ❌ Using MySQL's
MODIFYin SQL Server (or vice versa). ✅ Use the syntax of your database. - ❌ Narrowing a column without checking the data. ✅ Check
MAX(LEN(col))first. - ❌ Changing a column that is part of a PRIMARY KEY, index or FOREIGN KEY without dropping those first → SQL Server error. ✅ Drop the dependent constraint/index, alter, then recreate it.
Interview tip
Q: How do you change a column's data type in SQL Server, MySQL and PostgreSQL?
A: SQL Server — ALTER TABLE t ALTER COLUMN c new_type; MySQL — ALTER TABLE t MODIFY COLUMN c new_type; PostgreSQL — ALTER TABLE t ALTER COLUMN c TYPE new_type.
Key points to remember
- ALTER COLUMN changes an existing column's data type, size or NULL/NOT NULL setting, keeping its values when they can be converted.
- SQL Server: ALTER TABLE players ALTER COLUMN name VARCHAR(100) NOT NULL;
- Restate NOT NULL in SQL Server — leaving it out makes the column nullable.
- MySQL uses MODIFY COLUMN; PostgreSQL uses ALTER COLUMN name TYPE new_type.
- Widening (VARCHAR(50) → VARCHAR(100), INT → BIGINT) is safe; narrowing can fail or lose data.
- Check the data first (e.g. MAX(LEN(col))) before shrinking a column.
- Renaming a column is separate: sp_rename in SQL Server, RENAME COLUMN in MySQL/PostgreSQL.
Try it yourself
SQL Server: make name bigger
-- SQL Server only (ALTER COLUMN is not supported in the browser playground)
ALTER TABLE players
ALTER COLUMN name VARCHAR(100) NOT NULL;
-- MySQL: ALTER TABLE players MODIFY COLUMN name VARCHAR(100) NOT NULL;
-- PostgreSQL: ALTER TABLE players ALTER COLUMN name TYPE VARCHAR(100);SQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Before narrowing: check the longest value
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Try it: same idea in the playground (add, copy, drop)
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
