How do I Alter Table Column datatype on more than 1 column?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Most SQL databases support altering multiple columns in a single ALTER TABLE statement, but the syntax varies by database. In MySQL, use ALTER TABLE t MODIFY col1 new_type, MODIFY col2 new_type. In PostgreSQL, use ALTER TABLE t ALTER COLUMN col1 TYPE new_type, ALTER COLUMN col2 TYPE new_type. In SQL Server, you must run separate ALTER TABLE ALTER COLUMN statements for each column. Altering data types on columns with existing data requires careful planning — the database must be able to convert existing values to the new type, or the operation fails.
MySQL: Multiple Columns in One Statement
MySQL's ALTER TABLE ... MODIFY changes the column data type. Multiple MODIFY clauses can be comma-separated in one statement, which is faster than running separate ALTER TABLE commands because MySQL only rebuilds the table once.
PostgreSQL: ALTER COLUMN TYPE
PostgreSQL's USING clause specifies how to convert existing data. Without it, PostgreSQL uses a default cast, which may fail for incompatible types (e.g., TEXT containing non-numeric values to INTEGER).
SQL Server: Separate Statements Required
SQL Server requires a separate ALTER TABLE ... ALTER COLUMN for each column. Wrap them in a transaction to ensure all changes succeed or none do.
Oracle: Multiple Columns in One Statement
Oracle wraps multiple column modifications in parentheses after a single MODIFY keyword.
SQLite: No ALTER COLUMN Support
SQLite only supports ALTER TABLE ... RENAME and ALTER TABLE ... ADD COLUMN. Changing column types requires recreating the table entirely.
Handling Data Conversion
Always check existing data for compatibility before altering types. A string column containing letters cannot be converted to an integer without handling the invalid values first.
Syntax Comparison Table
| Database | Syntax | Multiple in one statement |
| MySQL | MODIFY COLUMN col TYPE | Yes (comma-separated) |
| PostgreSQL | ALTER COLUMN col TYPE type | Yes (comma-separated) |
| SQL Server | ALTER COLUMN col TYPE | No (separate statements) |
| Oracle | MODIFY (col TYPE, ...) | Yes (parenthesized) |
| SQLite | Not supported | Must recreate table |
Common Pitfalls
- Data loss from narrowing types: Changing
VARCHAR(255)toVARCHAR(50)truncates values longer than 50 characters. ChangingDECIMAL(10,2)toINTEGERdrops decimal places. Always checkMAX(LENGTH(column))orMAX(column)before narrowing a type. - Foreign key and index conflicts: Columns referenced by foreign keys or indexes may block type changes. Drop the constraints first, alter the column, then recreate the constraints. Some databases (MySQL) handle this automatically, others (PostgreSQL) do not.
- Implicit vs explicit casting failures: PostgreSQL's
ALTER COLUMN ... TYPEwithoutUSINGrelies on implicit casts. ConvertingTEXTtoINTEGERfails if any row contains non-numeric text. Always useUSINGwith an explicit cast expression for non-trivial conversions. - Table lock during ALTER TABLE: Most databases acquire an exclusive lock during
ALTER TABLE, blocking all reads and writes. For large tables (millions of rows), this can cause significant downtime. Use tools likept-online-schema-change(MySQL) orpg_repack(PostgreSQL) for zero-downtime migrations. - NULL handling in NOT NULL columns: Adding
NOT NULLconstraint while changing the type fails if the column contains NULL values. Either update NULLs first (UPDATE t SET col = default WHERE col IS NULL) or change the type and constraint in separate steps.
Summary
- MySQL and PostgreSQL support multiple column type changes in one
ALTER TABLEstatement - SQL Server requires separate
ALTER TABLE ALTER COLUMNstatements per column - SQLite does not support
ALTER COLUMN— recreate the table instead - Use PostgreSQL's
USINGclause to control data conversion explicitly - Always check existing data for compatibility before changing types
- Wrap multiple alterations in a transaction to ensure atomicity

