24

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
  • 14,142
  • 48
  • 129
  • 212

3 Answers3

28
ALTER TABLE MyTable MODIFY Col3 varchar(20) NULL;
eggyal
  • 118,441
  • 18
  • 203
  • 234
28

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

ALTER TABLE `MyTable`
ALTER COLUMN `Col3` varchar(20) DEFAULT NULL
octopusgrabbus
  • 10,250
  • 13
  • 64
  • 126
Tschareck
  • 3,751
  • 8
  • 44
  • 73
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
  • 704,970
  • 168
  • 1,303
  • 1,425
EHP
  • 1
  • 1