开发者

Inserting default value as current date + 30 days in MySQL

开发者 https://www.devze.com 2023-02-03 07:55 出处:网络
How can I make a column\'s default value equal to the current date + 30 days in MySQL? For example开发者_运维技巧, if current date is 10-1-2011 then the column value must be inserted as 9-2-2011.If yo

How can I make a column's default value equal to the current date + 30 days in MySQL? For example开发者_运维技巧, if current date is 10-1-2011 then the column value must be inserted as 9-2-2011.


If you're using MySQL >= 5.0, use a trigger:

CREATE TRIGGER setDefaultDate
    BEFORE INSERT ON tableName
    FOR EACH ROW
    SET NEW.date = ADDDATE(curdate(), INTERVAL 30 DAY);

The trigger will activate when you insert into tableName, setting date to now + 30 days. If your insert sets the date, it will override this default due to the BEFORE. The date is calculated using ADDDATE.

0

精彩评论

暂无评论...
验证码 换一张
取 消