MySQL TIMESTAMP 默认为 NULL,而不是 CURRENT_TIMESTAMP

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

MySQL TIMESTAMP to default NULL, not CURRENT_TIMESTAMP

mysqlsqltimestamp

提问by natsuki_2002

Using MySQL, I'm trying to make a timestamp column from a date column and a time column. If the date or time column contains a NULL value, MySQL automatically sets the corresponding value in the TIMESTAMPcolumn to the CURRENT_TIMESTAMP.

使用 MySQL,我试图从日期列和时间列创建时间戳列。如果日期或时间列包含 NULL 值,MySQL 会自动将TIMESTAMP列中的相应值设置为CURRENT_TIMESTAMP.

Is there a way to make it default to a NULL, instead of the CURRENT_TIMESTAMP?

有没有办法让它默认为 a NULL,而不是CURRENT_TIMESTAMP

Something like:

就像是:

ALTER TABLE customers ADD s_timestamp TIMESTAMP;
UPDATE customers
    SET s_timestamp = timestamp(s_date,s_time) DEFAULT NULL;

回答by zessx

Use this query to add your column

使用此查询添加您的列

ALTER TABLE `customers` 
ADD COLUMN `s_timestamp` TIMESTAMP NULL DEFAULT NULL;

And, if you want to get current timestamp on update only :

而且,如果您只想在更新时获取当前时间戳:

ALTER TABLE `customers` 
ADD COLUMN `s_timestamp` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP;

回答by niyou

I thought this should work:

我认为这应该有效:

ALTER TABLE customers ADD COLUMN s_timestamp TIMESTAMP DEFAULT NULL;