The following should work:
ALTER TABLE tips MODIFY COLUMN year YEAR(4) NOT NULL DEFAULT CURRENT_TIMESTAMP
See Year Data Type for more information.
So, I checked this as soon as I got access and it does not work. As another poster pointed out, CURRENT_TIMESTAMP only works with the TIMESTAMP data type .
Is there a specific problem with saving the full timestamp, and then only using the year in your code? If not, I would recommend storing this value as a timestamp.
Another option is to create a trigger :
CREATE TRIGGER example_trigger AFTER INSERT ON tips FOR EACH ROW BEGIN UPDATE tips SET year = YEAR(NOW()) WHERE tip_id = NEW.tip_id END;
Otherwise, assign this value to the INSERT statement from your code.
The best solution in your case will depend entirely on the circumstances surrounding your particular application.
Noah goodrich
source share