Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Set to default value on update

Tags:

mysql

Is it possible to set a column to its default value (or any specified value) on update when no value is specifically given in the statement? I was thinking that a trigger might accomplish this. Something like

IF ISNULL(NEW.column) THEN
 NEW.column = value
END IF;

didn't work.

like image 557
Jan Traenkner Avatar asked Jan 05 '14 20:01

Jan Traenkner


1 Answers

MySQL has function called DEFAULT(), which gets the default value from specified column.

UPDATE tbl SET col = DEFAULT(col);

MySQL Reference

like image 114
aksu Avatar answered Nov 18 '22 13:11

aksu