Last answered:

21 Apr 2023

Posted on:

16 Apr 2023

2

Modify or change?

The assigment solution uses MODIFY to make the column NULL and CHANGE to make it NOT NULL. Is it necessary to do it this way or can we use whcih every we like for setting both NULL or NOT NULL.

For example is it okay to use CHANGE to make column NULL? or use MODIFY to make column NOT NULL?

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

21 Apr 2023

2

Hi Shandana!
Thanks for reaching out.


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.

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.

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.
Best,
Tsvetelin

Submit an answer