在MySQL中插入默认值作为当前日期+ 30天 [英] Inserting default value as current date + 30 days in MySQL
本文介绍了在MySQL中插入默认值作为当前日期+ 30天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在MySQL中,如何使列的默认值等于当前日期+ 30天?例如,如果当前日期是10-1-2011,则必须将列值插入为9-2-2011.
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.
推荐答案
如果您使用的是MySQL> = 5.0,请使用触发器:
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);
触发会在您插入date
设置为现在+ 30天.如果您的插入内容设置了日期,则由于BEFORE
,它将覆盖此默认值.日期是使用 ADDDATE
.
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
.
这篇关于在MySQL中插入默认值作为当前日期+ 30天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文