Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
918 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...