我有一张 table 。我想更改该表,以便它有一列存储创建日期。我查阅了一些文章,坚持认为从 Mysql ver 5.6 开始,您可以将 DATETIME 与 CURRENT_TIMESTAMP 一起使用。但我未能实现。
~:$ mysql --version
mysql Ver 14.14 Distrib 5.7.22, for macos10.13 (x86_64) using EditLine wrapper
MYSQL 版本 5.7
mysql> ALTER TABLE table_name MODIFY datecreated DEFAULT CURRENT_TIMESTAMP;
ERROR 1064 (42000): 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 'DEFAULT CURRENT_TIMESTAMP' at line 1
发生错误
mysql> SHOW COLUMNS FROM table_name;
+---------------+---------------+------+-----+-------------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+-------------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
...
...
..
.
.
.
| datecreated | datetime | YES | | NULL | |
+---------------+---------------+------+-----+-------------------------+----------------+
15 rows in set (0.00 sec)
表格信息。
最佳答案
谢谢大家,特别是@fifonik和@NICK
mysql> ALTER TABLE table_name MODIFY datecreated DATETIME NULL DEFAULT CURRENT_TIMESTAMP;
😀
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
关于MySQL 5.7 使用 CURRENT_TIMESTAMP 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54755029/