Mysql

如何將 TIMESTAMP 的預設值設置為未來日期?

  • July 15, 2016

我知道我可以像這樣使用目前時間戳設置預設值

... DEFAULT CURRENT_TIMESTAMP

有沒有辦法根據目前時間戳設置具有固定數量單位的列?

... DEFAULT CURRENT_TIMESTAMP + 10 days?

使用觸發器:

mysql> CREATE TRIGGER dateinsert BEFORE INSERT ON testtable
   -> FOR EACH ROW
   -> SET NEW.yourdate =  DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL 10 DAY);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into testtable (blah) values (1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from testtable;
+------+------------+
| blah | yourdate   |
+------+------------+
|    1 | 2016-07-25 |
+------+------------+
1 row in set (0.00 sec)

mysql>

引用自:https://dba.stackexchange.com/questions/143953