user2694306
user2694306

Reputation: 4050

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?

Upvotes: 1

Views: 2968

Answers (2)

Matt Saunders
Matt Saunders

Reputation: 4074

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.

Upvotes: 4

Awnage
Awnage

Reputation: 94

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.

Upvotes: 1

Related Questions