Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset MySQL field to default value

Tags:

database

mysql

Is there a command in MySQL to reset a field to its default value? You know, in favor of the "Do Not Repeat Yourself" rule, I don't want to write the (quite long) default value multiple times in code, only once in the DB.

I looked around quite some time in google, found nothing. I'm starting to suspect such command doesn't exist, but nevertheless, if it does, sy here's going to know about it. :)

like image 525
Gabor Magyar Avatar asked Mar 28 '13 16:03

Gabor Magyar


People also ask

How do I change my default value?

Set a default valueRight-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.

How do I change the default value to not null in MySQL?

To enforce NOT NULL for a column in MySQL, you use the ALTER TABLE .... MODIFY command and restate the column definition, adding the NOT NULL attribute.


1 Answers

MySQL has a DEFAULT keyword (and function) that will do exactly what you want.

UPDATE table SET col = DEFAULT WHERE id = 2

OR

UPDATE table SET col = DEFAULT(col2) WHERE id = 3
like image 125
Rocket Hazmat Avatar answered Oct 06 '22 11:10

Rocket Hazmat