1. <i id='3uKpN'><tr id='3uKpN'><dt id='3uKpN'><q id='3uKpN'><span id='3uKpN'><b id='3uKpN'><form id='3uKpN'><ins id='3uKpN'></ins><ul id='3uKpN'></ul><sub id='3uKpN'></sub></form><legend id='3uKpN'></legend><bdo id='3uKpN'><pre id='3uKpN'><center id='3uKpN'></center></pre></bdo></b><th id='3uKpN'></th></span></q></dt></tr></i><div id='3uKpN'><tfoot id='3uKpN'></tfoot><dl id='3uKpN'><fieldset id='3uKpN'></fieldset></dl></div>
    <tfoot id='3uKpN'></tfoot>

    <small id='3uKpN'></small><noframes id='3uKpN'>

      <legend id='3uKpN'><style id='3uKpN'><dir id='3uKpN'><q id='3uKpN'></q></dir></style></legend>

        <bdo id='3uKpN'></bdo><ul id='3uKpN'></ul>

      如何为 MySQL 日期时间列设置默认值?

      时间:2023-08-18
      <legend id='CMyEM'><style id='CMyEM'><dir id='CMyEM'><q id='CMyEM'></q></dir></style></legend>
        <i id='CMyEM'><tr id='CMyEM'><dt id='CMyEM'><q id='CMyEM'><span id='CMyEM'><b id='CMyEM'><form id='CMyEM'><ins id='CMyEM'></ins><ul id='CMyEM'></ul><sub id='CMyEM'></sub></form><legend id='CMyEM'></legend><bdo id='CMyEM'><pre id='CMyEM'><center id='CMyEM'></center></pre></bdo></b><th id='CMyEM'></th></span></q></dt></tr></i><div id='CMyEM'><tfoot id='CMyEM'></tfoot><dl id='CMyEM'><fieldset id='CMyEM'></fieldset></dl></div>
              <tbody id='CMyEM'></tbody>
              <bdo id='CMyEM'></bdo><ul id='CMyEM'></ul>
              <tfoot id='CMyEM'></tfoot>

              1. <small id='CMyEM'></small><noframes id='CMyEM'>

              2. 本文介绍了如何为 MySQL 日期时间列设置默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如何为 MySQL 日期时间列设置默认值?

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

                在 SQL Server 中,它是 getdate().MySQL 的等效项是什么?如果这是一个因素,我正在使用 MySQL 5.x.

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

                推荐答案

                重要MySQL 5.6.5 开始,现在可以使用 DATETIME 字段来实现这一点,请查看 其他帖子 下面...

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

                以前的版本不能用 DATETIME...

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

                但是你可以用 TIMESTAMP 来做到:

                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: 如果您定义了一个默认设置为 CURRENT_TIMESTAMP ON 的列,您将需要始终为此列指定一个值,否则该值将自动重置为now()"在更新.这意味着如果您不想更改该值,则您的 UPDATE 语句必须包含[您的列名] = [您的列名]";(或某个其他值)或该值将变为now()".很奇怪,但确实如此.我正在使用 5.5.56-MariaDB

                这篇关于如何为 MySQL 日期时间列设置默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:MySQL中的自然排序 下一篇:MySQL:@variable 与变量.有什么不同?

                相关文章

                最新文章

                1. <small id='CmrBO'></small><noframes id='CmrBO'>

                  <i id='CmrBO'><tr id='CmrBO'><dt id='CmrBO'><q id='CmrBO'><span id='CmrBO'><b id='CmrBO'><form id='CmrBO'><ins id='CmrBO'></ins><ul id='CmrBO'></ul><sub id='CmrBO'></sub></form><legend id='CmrBO'></legend><bdo id='CmrBO'><pre id='CmrBO'><center id='CmrBO'></center></pre></bdo></b><th id='CmrBO'></th></span></q></dt></tr></i><div id='CmrBO'><tfoot id='CmrBO'></tfoot><dl id='CmrBO'><fieldset id='CmrBO'></fieldset></dl></div>

                2. <tfoot id='CmrBO'></tfoot>
                    <bdo id='CmrBO'></bdo><ul id='CmrBO'></ul>
                  <legend id='CmrBO'><style id='CmrBO'><dir id='CmrBO'><q id='CmrBO'></q></dir></style></legend>