user3553131
user3553131

Reputation:

mysql, how to set a column default value current year

the table looks like this:

id----name-----value-----year
1-----ab-------10-------2014
2-----ab-------10-------2014

for example now the defalt time is change means the year has changed current system year.

here the entry is only (Insert into table ('id','name','value') value ('3','adfaf','20')

here i want the year column to get default year only from the system time so when i run the upper query the result should be bellow as the system year now is 2015 so the column year get the year form the system. if i set the year to timestamp or current time it get all the value of time and date but i want only year.

3---adfaf---20-2015

the year column is current default year.

how to alter my table so i get the result.

regards

Upvotes: 1

Views: 2172

Answers (2)

user3553131
user3553131

Reputation:

i found it:

CREATE TRIGGER ins_year
BEFORE INSERT ON tableNAME
    FOR EACH ROW SET NEW.year = YEAR(NOW());

Upvotes: 1

Barmar
Barmar

Reputation: 781593

There's no way to make the default automatically be the current year. You could use an INSERT trigger that sets the year to the current year if it's NULL. Or at the beginning of each year you can manually change the default:

ALTER TABLE YourTable MODIFY year INT DEFAULT 2015;

Upvotes: 0

Related Questions