hoelz.ro - MySQL ALTER TABLE: ALTER vs CHANGE vs MODIFY COLUMN

MySQL ALTER TABLE: ALTER vs CHANGE vs MODIFY COLUMN

Whenever I have to change a column in MySQL (which isn't that often), I always forget the difference between ALTER COLUMN, CHANGE COLUMN, and MODIFY COLUMN. Here's a handy reference.

ALTER COLUMN

Used to set or remove the default value for a column. Example:

  1. ALTER TABLE MyTable ALTER COLUMN foo SET DEFAULT 'bar';

CHANGE COLUMN

Used to rename a column, change its datatype, or move it within the schema. Example:

  1. ALTER TABLE MyTable CHANGE COLUMN foo bar VARCHAR(32) NOT NULL FIRST;
  2. ALTER TABLE MyTable CHANGE COLUMN foo bar VARCHAR(32) NOT NULL AFTER baz;

MODIFY COLUMN

Used to do everything CHANGE COLUMN can, but without renaming the column. Example:

  1. ALTER TABLE MyTable MODIFY COLUMN foo VARCHAR(32) NOT NULL AFTER baz;

The official documentation for ALTER TABLE (for MySQL 5.1) is here.


Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki ipv6 ready