Sub topic 6
Changing a Column's Type
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, can create tables, and understand primary keys and foreign keys. This chapter uses PostgreSQL examples. Other database systems use similar ideas but different syntax.
Changing a column's type changes how the database stores and validates its values. For example, you might change a text column containing numbers into an integer column. The database must convert every existing value, so the operation can fail or lock the table.
The basic operation
In PostgreSQL, use ALTER TABLE with ALTER COLUMN ... TYPE:
CREATE TABLE products (
id INTEGER PRIMARY KEY,
price TEXT NOT NULL
);
INSERT INTO products (id, price)
VALUES (1, '19.99'), (2, '5.50');
ALTER TABLE products
ALTER COLUMN price TYPE NUMERIC(10, 2)
USING TRIM(price)::NUMERIC;The USING expression tells PostgreSQL how to convert each existing value. TRIM removes surrounding spaces, and ::NUMERIC converts the cleaned text to a numeric value.
If every value can be converted automatically, PostgreSQL may not need USING:
ALTER TABLE products
ALTER COLUMN id TYPE BIGINT;Before changing a type, inspect the existing data. A single invalid value can make the entire operation fail.
SELECT price
FROM products
WHERE price !~ '^[0-9]+(\.[0-9]+)?$';What happens to constraints and application code
A type change can affect:
NOT NULL,CHECK, and foreign key constraints.- Indexes and sort behavior.
- Queries that compare the column with a different type.
- Application code that expects strings, dates, or a particular numeric range.
- Views, stored procedures, and generated values that reference the column.
A conversion can also lose information. Changing NUMERIC(10, 2) to INTEGER removes the fractional part or rounds it, depending on the database. Test the conversion on a copy or in a staging database first.
Dialect differences
The same intention has different syntax in common database systems:
| Database | Typical syntax |
|---|---|
| PostgreSQL | `ALTER COLUMN name TYPE VARCHAR(200)` |
| MySQL | `MODIFY COLUMN name VARCHAR(200) NOT NULL` |
| SQL Server | `ALTER COLUMN name VARCHAR(200) NOT NULL` |
| SQLite | Many type changes require creating a replacement table |
MySQL's MODIFY COLUMN usually requires you to repeat properties such as NOT NULL and DEFAULT. SQL Server uses ALTER COLUMN but omits the TYPE keyword. Always check the version and documentation for the database you will run.
Locking and large tables
Most databases need to inspect or rewrite rows during a type change. The operation may acquire a lock that blocks reads or writes. On a large table, measure the expected duration and schedule the change during a low-traffic period.
A safer migration can add a new column, copy data in batches, validate it, update application code, and remove the old column later. This takes more work but reduces the time of one blocking operation.
Exercise
Why might ALTER COLUMN price TYPE NUMERIC fail when price currently stores text?
Show answer
At least one existing text value may not be a valid numeric value, so the database cannot convert every row. Inspect and clean the data, then provide an explicit conversion expression such as PostgreSQL's USING.