2

I know I can set default value with current timestamp like this

... DEFAULT CURRENT_TIMESTAMP

Is there any way to set the column with fixed amount of units based on current timestamp?

... DEFAULT CURRENT_TIMESTAMP + 10 days?
2
  • 2
    You'll have to use a trigger, as you can't use functions for MySQL defaults
    – Philᵀᴹ
    Jul 15 '16 at 7:46
  • Yeah, I tried. Even TRIGGERs appear to be a bitch. Jul 15 '16 at 8:14
6

Use a trigger:

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.