如何修改 MySQL 列以允许 NULL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/212939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I modify a MySQL column to allow NULL?
提问by zmf
MySQL 5.0.45
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.
我将手册解释为只运行上面的内容,它会重新创建列,这次允许为空。服务器告诉我我有语法错误。我只是看不到他们。
回答by Daniel Spiewak
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.
默认情况下,列可以为空。只要该列未声明UNIQUE
or NOT NULL
,就不应该有任何问题。
回答by ConroyP
Your syntax error is caused by a missing "table" in the query
您的语法错误是由查询中缺少“表”引起的
ALTER TABLE mytable MODIFY mycolumn varchar(255) null;
回答by Krishnrohit
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;
回答by Gerald Senarclens de Grancy
Under some circumstances (if you get "ERROR 1064 (42000): You have an error in your SQL syntax;...") you need to do
在某些情况下(如果你得到“ERROR 1064 (42000): You have an error in your SQL syntax;...”)你需要做
ALTER TABLE mytable MODIFY mytable.mycolumn varchar(255);
回答by Hmerman6006
My solution is the same as @Krishnrohit:
我的解决方案与@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
.
我实际上将列设置为,NOT NULL
但是通过上面的查询,它被更改为NULL
.
P.S. I know this an old thread but nobody seems to acknowledge that CHANGE
is also correct.
PS 我知道这是一个旧线程,但似乎没有人承认这CHANGE
也是正确的。
回答by Jan Nejedly
Use:
ALTER TABLE mytable MODIFY mycolumn VARCHAR(255);
用:
ALTER TABLE mytable MODIFY mycolumn VARCHAR(255);