简体   繁体   中英

How to Set a calculated timestamp as a default value MySQL

I am trying to create a table that sets an expiry date within 2 weeks of having been created. The next select statement gives me that record:

 (SELECT TIMESTAMPADD(WEEK,2,(CURRENT_TIMESTAMP)));

| 2014-04-21 18:08:52 |

But DEFAULT on create table does not allow me to put any expression:

create table test (somename varchar(32), expiry TIMESTAMP DEFAULT (SELECT TIMESTAMPADD(WEEK,2,(CURRENT_TIMESTAMP))));

How can achieve this? --UPDATE - I TRY this trigger but my syntax is wrong:

CREATE TRIGGER expiryset AFTER INSERT ON test BEGIN DECLARE exp TIMESTAMP; SET @exp := (SELECT TIMESTAMPADD(WEEK,2,(CURRENT_TIMESTAMP))); UPDATE test SET expiry = @exp WHERE somename = ; END;

this works but i need to get somename value/column of the insert before the trigger....how can i get this?

您不能将默认值设置为NULL或当前时间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM