0

我想要做的是,我想更改列属性,并为某个列关联一个默认值,这个值将来自子查询。基本上 timeOfCreation 的默认值应该是一个子查询--> (select NOW())。但是由于某种原因它返回一个语法错误,我不明白。这是我输入的命令。

 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;

SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;

SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';



ALTER TABLE `horizon`.`articles` CHANGE COLUMN `timeOfCreation` `timeOfCreation` DATETIME NULL DEFAULT (select NOW())  ;



 SQL_MODE=@OLD_SQL_MODE;

    SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

    SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

和错误:

Executing SQL script in server

ERROR: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select NOW())' at line 1





ALTER TABLE `horizon`.`articles` CHANGE COLUMN `timeOfCreation` `timeOfCreation` DATETIME NULL DEFAULT (select NOW())  



SQL script execution finished: statements: 3 succeeded, 1 failed

附言。我将工作台用作软件,但我认为这并不重要。

4

1 回答 1

2

默认值必须是常量,不能是表达式或查询。有一个例外,恰好是您需要的情况:TIMESTAMP 或 DATETIME 列可以CURRENT_TIMESTAMP用作其默认值。

ALTER TABLE `horizon`.`articles` MODIFY COLUMN `timeOfCreation` DATETIME NULL DEFAULT CURRENT_TIMESTAMP  ;
于 2013-04-30T08:42:24.100 回答