¿Cómo puedo modificar una columna MySQL para permitir NULL?


MySQL 5.0.45

¿Cuál es la sintaxis para alterar una tabla para permitir que una columna sea nula, alternativamente qué hay de malo en esto:

ALTER mytable MODIFY mycolumn varchar(255) null;

Interpreté el manual como simplemente ejecutar lo anterior y recrearía la columna, esta vez permitiendo null. El servidor me dice que tengo errores sintácticos. Simplemente no los veo.

 316
Author: Daniel Spiewak, 2008-10-17

5 answers

Usted quiere lo siguiente:

ALTER TABLE mytable MODIFY mycolumn VARCHAR(255);

Las columnas son nullables por defecto. Mientras la columna no es declarado UNIQUE o NOT NULL, no debería haber ningún problema.

 471
Author: Daniel Spiewak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-17 16:55:11

El error de sintaxis se debe a que falta una "tabla" en la consulta

ALTER TABLE mytable MODIFY mycolumn varchar(255) null;
 172
Author: ConroyP,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-17 16:58:39

Mi solución:

ALTER TABLE table_name CHANGE column_name column_name type DEFAULT NULL

Por ejemplo:

ALTER TABLE SCHEDULE CHANGE date date DATETIME DEFAULT NULL;
 21
Author: Krishnrohit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-24 09:42:56

Bajo algunas circunstancias (si obtienes " ERROR 1064 (42000) : Tienes un error en tu sintaxis SQL;...") usted necesita hacer

ALTER TABLE mytable MODIFY mytable.mycolumn varchar(255);
 6
Author: Gerald Senarclens de Grancy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-02 16:15:27

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

 -4
Author: Jan Nejedly,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-12 13:21:09