Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set a default value for a MySQL Datetime column?

Tags:

datetime

mysql

How do you set a default value for a MySQL Datetime column?

In SQL Server it's getdate(). What is the equivalant for MySQL? I'm using MySQL 5.x if that is a factor.

like image 631
Brian Boatright Avatar asked Oct 03 '08 20:10

Brian Boatright


People also ask

What is the default value for datetime in MySQL?

DATETIME has a default of NULL unless defined with the NOT NULL attribute, in which case the default is 0.

How do I add a default value to a TIMESTAMP column?

Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP. The DEFAULT clause also can be used to specify a constant (nonautomatic) default value; for example, DEFAULT 0 or DEFAULT '2000-01-01 00:00:00'.

How do I add a default value to a column in MySQL?

To change a default value, use ALTER col_name SET DEFAULT : ALTER TABLE mytbl ALTER j SET DEFAULT 1000; Default values must be constants. For example, you cannot set the default for a date-valued column to NOW( ) , although that would be very useful.

Can we set a default value of integer for a table column of datatype datetime?

Explicit Default Handling Prior to MySQL 8.0. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE . The exception is that, for TIMESTAMP and DATETIME columns, you can specify CURRENT_TIMESTAMP as the default.


1 Answers

IMPORTANT EDIT: It is now possible to achieve this with DATETIME fields since MySQL 5.6.5, take a look at the other post below...

Previous versions can't do that with DATETIME...

But you can do it with TIMESTAMP:

mysql> create table test (str varchar(32), ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP); Query OK, 0 rows affected (0.00 sec)  mysql> desc test; +-------+-------------+------+-----+-------------------+-------+ | Field | Type        | Null | Key | Default           | Extra | +-------+-------------+------+-----+-------------------+-------+ | str   | varchar(32) | YES  |     | NULL              |       |  | ts    | timestamp   | NO   |     | CURRENT_TIMESTAMP |       |  +-------+-------------+------+-----+-------------------+-------+ 2 rows in set (0.00 sec)  mysql> insert into test (str) values ("demo"); Query OK, 1 row affected (0.00 sec)  mysql> select * from test; +------+---------------------+ | str  | ts                  | +------+---------------------+ | demo | 2008-10-03 22:59:52 |  +------+---------------------+ 1 row in set (0.00 sec)  mysql> 

CAVEAT: IF you define a column with CURRENT_TIMESTAMP ON as default, you will need to ALWAYS specify a value for this column or the value will automatically reset itself to "now()" on update. This means that if you do not want the value to change, your UPDATE statement must contain "[your column name] = [your column name]" (or some other value) or the value will become "now()". Weird, but true. I am using 5.5.56-MariaDB

like image 178
sebthebert Avatar answered Sep 20 '22 17:09

sebthebert