530

MySQL 5.0.45

What is the syntax to alter a table to allow a column to be null, alternately what's wrong with this:

ALTER mytable MODIFY mycolumn varchar(255) null;

I interpreted the manual as just run the above and it would recreate the column, this time allowing null. The server is telling me I have syntactical errors. I just don't see them.

1
  • column is not unique or anything else like that
    – zmf
    Commented Oct 17, 2008 at 16:53

6 Answers 6

764

You want the following:

ALTER TABLE mytable MODIFY mycolumn VARCHAR(255);

Columns are nullable by default. As long as the column is not declared UNIQUE or NOT NULL, there shouldn't be any problems.

2
  • 25
    There's an edge case which is the TIMESTAMP type, which depending on your MySQL version and config can be NOT NULL specifying NULL as suggested by @ConroyP is more correct. Commented Jan 26, 2016 at 13:37
  • 2
    This did not work for me! The column did not change. Maybe because I had a constraint to another table where the column was used (when not null).
    – Rocologo
    Commented Dec 18, 2016 at 10:09
357

Your syntax error is caused by a missing "table" in the query

ALTER TABLE mytable MODIFY mycolumn varchar(255) null;
4
  • 44
    This is actually the correct answer - whilst the NULL clause isn't required, there's nothing wrong with providing it. The missing TABLE from the ALTER TABLE statement was the real problem. Commented Nov 20, 2014 at 20:38
  • @SamStephens and Xofo: There is a primary and secondary ("alternately") question. This is the correct answer to the secondary question while the accepted answer is the correct answer to the primary one.
    – jdunk
    Commented Aug 11, 2016 at 17:45
  • @SamStephens Just because there's nothing wrong with providing NULL, it doesn't make this answer any more "correct" than the accepted answer? Knowing that columns are nullable by default (as mentioned in the accepted answer) is helpful in regards to this particular question.
    – rybo111
    Commented May 11, 2020 at 11:10
  • 1
    OP doesn't know why their statement didn't work. This answer explains why. Commented May 12, 2020 at 20:11
48

My solution:

ALTER TABLE table_name CHANGE column_name column_name type DEFAULT NULL

For example:

ALTER TABLE SCHEDULE CHANGE date date DATETIME DEFAULT NULL;
0
11

My solution is the same as @Krishnrohit:

ALTER TABLE `table` CHANGE `column_current_name` `new_column_name` DATETIME NULL;

I actually had the column set as NOT NULL but with the above query it was changed to NULL.

P.S. I know this an old thread but nobody seems to acknowledge that CHANGE is also correct.

8

Under some circumstances (if you get "ERROR 1064 (42000): You have an error in your SQL syntax;...") you need to do

ALTER TABLE mytable MODIFY mytable.mycolumn varchar(255);
-12

Use: ALTER TABLE mytable MODIFY mycolumn VARCHAR(255);

1
  • 25
    It would be better if your answer provided additional value on top of the other answers. In this case, your answer does not provide additional value, since Daniel Spiewak already posted that solution. If a previous answer was helpful to you, you should vote it up once you have enough reputation
    – Luís Cruz
    Commented Aug 12, 2015 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.