Insert default as current date + 30 days in MySQL

How can I make the default value for a column equal to the current date + 30 days in MySQL? For example, if the current date is 10-1-2011, the column value should be inserted as 9-2-2011.

+6
sql mysql
source share
1 answer

If you are 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); 

trigger will be activated when inserted into tableName , setting date now + 30 days. If your insert sets a date, it overrides this default due to BEFORE . Date is calculated using ADDDATE .

+6
source share

All Articles