CHANGE COLUMN If you have already created your MySQL database, and decide after the fact that one of your columns is named incorrectly, you don't need to remove it and make a replacement, you can simply rename it using change column.
ALTER TABLE MyTable CHANGE COLUMN foo bar VARCHAR(32) NOT NULL FIRST;
MODIFY COLUMN This command does everything CHANGE COLUMN can, but without renaming the column.You can use the modify SQL command if you need to resize a column in MySQL. By doing this you can allow more or less characters than before. You can't rename a column using modify and other
ALTER TABLE MyTable MODIFY COLUMN foo VARCHAR(32) NOT NULL AFTER baz;
Note : ALTER TABLE is used for altering a table means to change column name, size, drop column. CHANGE COLUMN and MODIFY COLUMN commands cannot be used without help of ALTER TABLE command.
The difference is whether you want to change the column name, column definition or both.
Can change a column name or definition, or bothALTER TABLE t1 CHANGE a b BIGINT NOT NULL
Can change a column definition but not its nameALTER TABLE t1 MODIFY b INT NOT NULL
Can change a column name but not its definitionALTER TABLE t1 RENAME COLUMN b TO a
Also, CHANGE
and MODIFY
can be followed by an optional COLUMN
keyword.
For complete explanation:
I found one difference after more than an hour of effort in trying to make a non auto_increment column into auto_increment statement:
alter table `doctor_experience` modify column `id` int(11) unsigned auto_increment
works, but statment:
alter table `doctor_experience` change column `id` `id` int(11) unsigned auto_increment
will report an error.
That is the same. It was done to support another syntax (Oracle ALTER TABLE as I know). You can use both of them.
Note: ALTER TABLE CHANGE old_col_name new_col_name
syntax allows renaming column using one command.
Change Column : Used when we want to change the column name with its definition.
eg - alter table student
CHANGE name
full_name
VARCHAR(32) NOT NULL;
Modify column : Used when column name is to be same but change in its definition.
eg - alter table student
MODIFY full_name
VARCHAR(64) NOT NULL;
Rename column : Used when we only need to change the column name (its definition will be same)
alter table student
RENAME COLUMN full_name
TO name
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With