// dialect comparison

Change a column's type

Convert a column's data to a new type in place, with PostgreSQL's USING or the engine's implicit cast.

Filed under Tables & schema

The verdict

SQL Server, PostgreSQL and MySQL change the type in one statement, and PostgreSQL's USING is the only syntax that lets you transform the data as part of it. Oracle and SQLite need the add-copy-drop-rename pattern for a real type change, which is also what every engine uses for zero-downtime changes on big tables.

Where they differ

Each example turns a text zip column into an integer. SQL Server, PostgreSQL and MySQL convert existing data in one ALTER; PostgreSQL's USING clause lets you say exactly how. Oracle changes a column's datatype only when the column is empty, and SQLite has no ALTER for types at all, so both rebuild: copy into a new column or table, drop, rename.

How each engine writes it

SQL Server

ALTER TABLE customers ALTER COLUMN zip INT NULL;

Restate the full definition, including NULL or NOT NULL, or nullability can change. Fails if any value won't convert, or while an index, constraint or schema-bound view uses the column.

PostgreSQL

ALTER TABLE customers
ALTER COLUMN zip TYPE INTEGER USING zip::integer;

USING gives the conversion expression; without it, text to integer is refused because there is no implicit cast. The table and its indexes are rewritten.

MySQL

ALTER TABLE customers MODIFY COLUMN zip INT;

MODIFY replaces the whole definition, so restate NOT NULL, DEFAULT and COMMENT or they are dropped. Type changes copy the table (ALGORITHM=COPY) and block writes meanwhile.

SQLite

CREATE TABLE customers_new (zip INTEGER, email TEXT);
INSERT INTO customers_new SELECT CAST(zip AS INTEGER), email FROM customers;
DROP TABLE customers;
ALTER TABLE customers_new RENAME TO customers;

No ALTER COLUMN TYPE. Run the rebuild in a transaction with PRAGMA foreign_keys = OFF, then recreate indexes, triggers and views.

Oracle

ALTER TABLE customers ADD (zip_num NUMBER(10));
UPDATE customers SET zip_num = TO_NUMBER(zip);
ALTER TABLE customers DROP COLUMN zip;
ALTER TABLE customers RENAME COLUMN zip_num TO zip;

MODIFY (zip NUMBER(10)) fails with ORA-01439 unless the column is empty. Within a type family, MODIFY works on data: widening VARCHAR2 is metadata-only, while shrinking NUMBER precision also needs an empty column.

Questions that come up

Why does SQLite force a rebuild?

SQLite stores the table definition as the original CREATE TABLE text, and ALTER TABLE can only rename, add or drop columns. Declared types are affinity hints, so a TEXT column already accepts integers; you rebuild when you need the new affinity or a stricter definition.

What can go wrong narrowing a type?

SQL Server raises a conversion or truncation error and changes nothing. MySQL in strict mode (the 8.0 default) errors too; without strict mode it truncates with a warning. Oracle refuses with ORA-01441 or ORA-01440. Look for values that won't fit first, for example WHERE LEN(zip) > 10 or TRY_CAST(zip AS INT) IS NULL in SQL Server.

How do I change a type on a huge table with no downtime?

Expand and contract: add a new column of the target type, backfill it in batches, keep it in sync with a trigger or the application, switch readers over, then drop the old column and rename. The same steps work on every engine.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.