Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>IMPORTANT EDIT:</strong> It is now possible to achieve this with DATETIME fields since <strong>MySQL 5.6.5</strong>, take a look at the <a href="https://stackoverflow.com/a/10603198/24820">other post</a> below...</p> <p>Previous versions can't do that with DATETIME...</p> <p>But you can do it with TIMESTAMP:</p> <pre><code>mysql&gt; create table test (str varchar(32), ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP); Query OK, 0 rows affected (0.00 sec) mysql&gt; 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&gt; insert into test (str) values ("demo"); Query OK, 1 row affected (0.00 sec) mysql&gt; select * from test; +------+---------------------+ | str | ts | +------+---------------------+ | demo | 2008-10-03 22:59:52 | +------+---------------------+ 1 row in set (0.00 sec) mysql&gt; </code></pre> <p>**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 hope this helps. I am using 5.5.56-MariaDB **</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload