Search code examples
mysqldefaultalter

MySQL set a default value YEAR type


The code below doesn't seem to work. I'm trying to set a default value and a time in the future (taking current year + 6 years). any idea?

ALTER TABLE users CHANGE COLUMN dpicture dpicture YEAR NULL DEFAULT 'YEAR(NOW())+6';

Solution

  • MySQL table defaults cannot be dynamic, except in the case of timestamp fields. There's also no such thing as a "year" field type in mysql, so your alter query is wrong on two levels.

    You'll probably have to use a after-insert/update trigger to set the +6 years, and use a proper date or int field.