我正在尝试使用表达式将 DATE 列添加到具有 DEFAULT DATE 值的表中:
ALTER TABLE `wp_ezts_project_params` ADD `est_completion` DATE NOT NULL
DEFAULT DATE_ADD( CURRENT_DATE(), INTERVAL 1 MONTH ) AFTER `client_id`
我尝试了多个 SQL 函数的不同变体,但每次在 DATE_ADD
附近都会出现语法错误。
我们不允许在 phpMyAdmin 中使用表达式作为默认值吗?
最佳答案
Are we not allowed to use expressions as default values in phpMyAdmin?
是的,这是不允许的。 manual 中明确说明了这一点
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
因此,如果您确实想为此列分配一个值,您将 a) 需要传入该值或 b) 使用触发器
但实际上您根本不需要此专栏。听起来您正在存储一个简单日期添加结果的值 - 存储它意味着您正在引入冗余。只需即时计算即可。
关于MySQL 使用 DATE_ADD 设置 DATE 列的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42942977/