27

Initially, the table "MyTable" has been defined in the following way:

CREATE TABLE IF NOT EXISTS `MyTable` (
  `Col1` smallint(6) NOT NULL AUTO_INCREMENT,
  `Col2` smallint(6) DEFAULT NULL,
  `Col3` varchar(20) NOT NULL,
);

How to update it in such a way that the column "Col 3" would be allowed to be NULL?

Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217

4 Answers4

31

The following MySQL statement should modify your column to accept NULLs.

ALTER TABLE `MyTable`
ALTER COLUMN `Col3` varchar(20) DEFAULT NULL
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Tschareck
  • 4,071
  • 9
  • 47
  • 74
29
ALTER TABLE MyTable MODIFY Col3 varchar(20) NULL;
eggyal
  • 122,705
  • 18
  • 212
  • 237
0

This works in PSQL, not sure if it also works in normal SQL.

ALTER TABLE tablename
ALTER COLUMN columnname DROP NOT NULL;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
EHP
  • 1
  • 1
0
ALTER TABLE school MODIFY COLUMN school_van varchar(36) DEFAULT NULL;

"ALTER" keyword didn't work but "MODIFY" worked fine in MySQL 8.0.26

SagitSri
  • 491
  • 6
  • 15
  • This answer was reviewed in the [Low Quality Queue](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/32398208) – Trenton McKinney Aug 03 '22 at 21:44