MySQL - 如何修改列默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20917109/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:45:52  来源:igfitidea点击:

MySQL - How to modify column default value?

mysqlsqldefaultalter

提问by User

How do I change my column's default value from None to something else? For example, I want my dates to have a default value of 0000-00-00 if I don't specify one when I create the row.

如何将我的列的默认值从 None 更改为其他值?例如,如果我在创建行时没有指定日期,我希望我的日期具有默认值 0000-00-00。

I understand this in phpMyAdmin, but I'm not sure how to do it via command prompt.

我在 phpMyAdmin 中了解这一点,但我不确定如何通过命令提示符执行此操作。

I also understand how to do this when adding a column. But all of my columns are made and have data in some of them.

我也了解添加列时如何执行此操作。但我所有的专栏都是制作的,其中一些有数据。

ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0;

From searching, I found this line, but I'm not sure if that's what I want?

通过搜索,我找到了这一行,但我不确定这是否是我想要的?

ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';

回答by Saharsh Shah

Use ALTER TABLEto CHANGEor MODIFYthe DEFAULTvalue of column. Check this link ALTER TABLE SYNTAX

使用ALTER TABLECHANGEMODIFYDEFAULT列的值。检查此链接ALTER TABLE SYNTAX

ALTER TABLE `tableName` CHANGE `columnName` `columnName` DATE DEFAULT '0000-00-00'; 
ALTER TABLE `tableName` MODIFY `columnName` DATE DEFAULT '0000-00-00'; 

回答by Satish Sharma

try this

尝试这个

ALTER TABLE foobar_data CHANGE COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';

user CHANGE to alter a existing column

用户 CHANGE 更改现有列

see Link

链接

回答by Waqar Hussain

In my case, it worked

就我而言,它有效

ALTER TABLE `table_name` CHANGE `col_name` `col_name_again` VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'anydefault_text';

Hope it helps you too.

希望对你也有帮助。