Resolved: Rename a column
It is correct to change the name of a column with the CHANGE COLUMN? And also to change it to DEFAULT in the same line of code?
I realized that i wrote the name of column wrong and I changed in the same time and it worked, but is correct to do that and also good practice?
Example: CHANGE COLUMN number_of_complaint number_of_complaints INT DEFAULT 0;
Thanks for reaching out.
Yes, you can use the CHANGE COLUMN command and change to the default value. It is fine.
The main difference between MODIFY and CHANGE is that when using CHANGE you can also rename the column you are referring to. Other than that both statements serve the same purpose and can be used interchangeably.
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;
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.
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 the help of ALTER TABLE command.
So, you need to use both with COLUMN. It is supposed that some versions may allow skipping it, but we have to stick to the good practices.
Hope this helps.