'MySQL - Default Timestamp of n days in the future
Is there anyway to set a fields default timestamp to n days in the future. For example I can setup a table as follows:
CREATE TABLE t1 (
name varchar(64),
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
I would want that whenever ts is not indicated that it would set the default value for n days in the future. I've seen here that it can be done via triggers (Inserting default value as current date + 30 days in MySQL), but is there any other way of doing this without triggers?
Solution 1:[1]
Per the MySQL docs (and like @bacon-bits wrote), this is not currently possible. DEFAULT must be a static value, with the exception of CURRENT_TIMESTAMP.
Solution 2:[2]
This is (now?) possible. To add a column ts to table t1 with a default of the current date / time plus 30 days, use:
ALTER TABLE t1 ADD ts DATETIME DEFAULT (CURRENT_TIMESTAMP + INTERVAL 30 DAY);
Tested with MySQL 8.0.23.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Awnage |
| Solution 2 | Matt Saunders |
