mysql - 实时服务器中当前时间戳字段的默认值无效

标签 mysql timestamp

下面的查询在我的本地主机上工作正常但是当我尝试在实时服务器上执行这个查询时它给出了 sql 错误

CREATE TABLE IF NOT EXISTS `tbl_claims` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`claimed_profile_id` int(11) NOT NULL,
`claimed_by_profile_id` int(11) NOT NULL COMMENT 'this will be only doctor''s profile id',
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`phone_no` varchar(100) NOT NULL,
`status` enum('approved','disapproved','pending') NOT NULL DEFAULT 'pending',
`added_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

出现以下错误:

 Invalid default value for 'added_on'

我不知道为什么上面的查询在 loalhost 中运行良好,而在实时服务器中却不能运行。

查询不工作的服务器有 PHP 版本 5.6.33

如有任何建议,我们将不胜感激。

最佳答案

我怀疑这是因为您试图将 CURRENT_TIMESTAMP 用作 datetime 字段的默认值。

For MySQL >= 5.6 :

The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for TIMESTAMP and DATETIME columns.

For MySQL <= 5.5 :

The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column.

如您所见,对于 5.5 及以下版本,您只能使用 CURRENT_TIMESTAMP 作为 TIMESTAMP 字段的默认值。

关于mysql - 实时服务器中当前时间戳字段的默认值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49009054/

相关文章:

voip - 如何计算RTP中的有效时间偏移量

python - Django ORM for select max(field1) from table group by (field2);

sql - 判断月份是否包含条目

java - 如何以自定义日期格式返回日历对象

postgresql - 默认时间戳格式和小数秒

datetime - 如何找到 2 个 NaiveDateTimes 之间的区别?

php - 在一个表中为另一表中的每第 n 个条目添加行

php - 我如何使用 Nginx 列出目录及其文件?

javascript - HTML表格,单独编辑记录

php - MySql 多条件查询