// columns reference

ALTER TABLE ALTER COLUMN

Changes an existing column's data type or size. Five engines, five spellings, and one engine with no statement at all: SQLite rebuilds the table instead.

Категория
Столбцы
In this category
2 of 5

Синтаксис

ALTER TABLE t ALTER COLUMN col new_type [NULL | NOT NULL]  -- SQL Server
ALTER TABLE t ALTER COLUMN col TYPE new_type [USING expression]  -- PostgreSQL
ALTER TABLE t MODIFY COLUMN col new_type  -- MySQL
ALTER TABLE t MODIFY (col new_type)  -- Oracle

Each block is a separate form. The support table and the examples show which engine takes which.

Поддержка движка

ДвижокSupportЗаметки
SQL Server Different syntax ALTER COLUMN; dependent constraints and indexes usually must be dropped first
PostgreSQL Да TYPE with an optional USING expression; indexes rebuild automatically
MySQL Different syntax MODIFY or CHANGE COLUMN; plain ALTER COLUMN only sets the default or visibility
SQLite No equivalent no statement: create the new table, INSERT SELECT, DROP, RENAME
Oracle Different syntax MODIFY; shrinking requires the stored data to fit the new size

Примеры

PostgreSQL

ALTER TABLE orders ALTER COLUMN priority TYPE smallint USING priority::smallint;

SQL Server

ALTER TABLE dbo.orders ALTER COLUMN priority SMALLINT NOT NULL;

MySQL

ALTER TABLE orders MODIFY COLUMN priority SMALLINT NOT NULL DEFAULT 0;

Oracle

ALTER TABLE orders MODIFY (priority NUMBER(5));

SQLite

-- the rebuild is the only way; see SQLite's ALTER TABLE docs
PRAGMA foreign_keys = OFF;
BEGIN;
CREATE TABLE orders_new (id INTEGER PRIMARY KEY, priority SMALLINT NOT NULL);
INSERT INTO orders_new SELECT id, priority FROM orders;
DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;
COMMIT;
PRAGMA foreign_keys = ON;

Полезно знать

  • MySQL is the trap: ALTER TABLE t ALTER COLUMN c ... sets or drops the column's DEFAULT, it does not change the type. Type changes go through MODIFY COLUMN, or CHANGE COLUMN when you also rename.
  • On SQL Server, ALTER COLUMN usually fails while the column appears in an index, a primary key or UNIQUE constraint, or a CHECK, FOREIGN KEY or DEFAULT constraint: drop them, alter, recreate. Widening a varchar is the common case that still goes through. It also resets the column to nullable when you omit NULL or NOT NULL, so restate NOT NULL every time.
  • PostgreSQL needs USING when the old and new types have no assignment cast: ALTER TABLE t ALTER COLUMN c TYPE int USING c::int. With no such cast it fails rather than guess, unless USING says how; a cast that does exist still runs, so numeric to int quietly rounds 3.7 to 4.
  • SQLite cannot change a column's type in place. The supported recipe: create a new table with the wanted types, INSERT INTO new SELECT ... FROM old, DROP the old table, RENAME the new one, with foreign keys off and everything in one transaction.

Questions about ALTER COLUMN

Why does MySQL ALTER COLUMN not change my column type?

On MySQL, ALTER COLUMN sets or drops the DEFAULT. The type-changing statement is ALTER TABLE t MODIFY COLUMN c new_type, or CHANGE COLUMN when you also rename. PostgreSQL and SQL Server spell type changes ALTER COLUMN.

How do I change a column type in SQLite?

You rebuild the table: create the replacement with the right types, copy the rows in, drop the original, rename the copy, and recreate indexes. SQLite's ALTER TABLE documentation lists the full procedure, including turning foreign keys off for the duration.

Will changing a column type lose my indexes?

PostgreSQL rebuilds and restores them as part of the statement (though it refuses while a view uses the column), and MySQL keeps them while it rebuilds the table. SQL Server typically makes you drop indexes and constraints first and recreate them yourself, and SQLite's rebuild recreates them by hand anyway.

Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.

// dialect-aware editing

DDL that matches the engine you're connected to

SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.