Last answered:

11 Nov 2022

Posted on:

02 Feb 2022

1

Resolved: ALTER COLUMN VS. CHANGE COLUMN

Are there any differences between the two? Or can you use it interchangeably?

2 answers ( 1 marked as helpful)
Instructor
Posted on:

09 Feb 2022

3

Hi Japhet!
Thanks for reaching out.

They do different things, so:

ALTER COLUMN

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

ALTER TABLE MyTable ALTER COLUMN foo SET DEFAULT 'bar';
ALTER TABLE MyTable ALTER COLUMN foo DROP DEFAULT;

CHANGE COLUMN

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

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

Hope this helps.
Best,
Tsvetelin

Posted on:

11 Nov 2022

2

Change column was used to add default and alter column to drop default in the video
you are saying alter column is for adding or dropping defaults.

ALTER TABLE customers
CHANGE COLUMN number_of_complaints number_of_complaints int DEFAULT 0;

ALTER TABLE customers
ALTER COLUMN number_of_complaints DROP DEFAULT;

Submit an answer