Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How mysql work when using change , modify , column inside alter table ? [closed]

Tags:

mysql

I want know How mysql work when using change , modify , column inside alter table ? by copy the table , and change the differences ? or some ... thanks

like image 552
Svan Avatar asked Jan 21 '26 15:01

Svan


1 Answers

MySQL ALTER TABLE: ALTER vs CHANGE vs MODIFY COLUMN

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

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;

MODIFY COLUMN

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

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

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

Modify column Vs change column

like image 190
jmail Avatar answered Jan 24 '26 08:01

jmail