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

  1. <legend id='ZRBHw'><style id='ZRBHw'><dir id='ZRBHw'><q id='ZRBHw'></q></dir></style></legend>
    <tfoot id='ZRBHw'></tfoot>

  2. <small id='ZRBHw'></small><noframes id='ZRBHw'>

      是否可以在 MySQL 中创建一个具有 UNIX_TIMESTAMP 默认值的列?

      Is it possible to create a column with a UNIX_TIMESTAMP default in MySQL?(是否可以在 MySQL 中创建一个具有 UNIX_TIMESTAMP 默认值的列?)
        <i id='5yI2A'><tr id='5yI2A'><dt id='5yI2A'><q id='5yI2A'><span id='5yI2A'><b id='5yI2A'><form id='5yI2A'><ins id='5yI2A'></ins><ul id='5yI2A'></ul><sub id='5yI2A'></sub></form><legend id='5yI2A'></legend><bdo id='5yI2A'><pre id='5yI2A'><center id='5yI2A'></center></pre></bdo></b><th id='5yI2A'></th></span></q></dt></tr></i><div id='5yI2A'><tfoot id='5yI2A'></tfoot><dl id='5yI2A'><fieldset id='5yI2A'></fieldset></dl></div>
        <tfoot id='5yI2A'></tfoot>

          1. <small id='5yI2A'></small><noframes id='5yI2A'>

          2. <legend id='5yI2A'><style id='5yI2A'><dir id='5yI2A'><q id='5yI2A'></q></dir></style></legend>
              <bdo id='5yI2A'></bdo><ul id='5yI2A'></ul>

                  <tbody id='5yI2A'></tbody>
                本文介绍了是否可以在 MySQL 中创建一个具有 UNIX_TIMESTAMP 默认值的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试这样做,但 MySQL 似乎不允许我这样做.是否有解决此问题的方法,还是希望我始终在 INSERT 查询中包含该函数?

                I'm trying to do this, but it seems like MySQL isn't allowing me. Is there a solution to this issue or am I expected to always include the function in my INSERT queries?

                CREATE TABLE foo(
                  created INT NOT NULL DEFAULT UNIX_TIMESTAMP()
                )
                

                我知道接受 CURRENT_TIMESTAMP 默认值的 TIMESTAMP 类型,但我的客户坚持在数据库中使用纪元时间.

                I'm aware of the TIMESTAMP type that accepts a CURRENT_TIMESTAMP default, but my client insisted on using epoch time in the database.

                推荐答案

                MySQL 实现TIMESTAMP 数据类型的方式,实际上是在数据库中存储纪元时间.所以你可以只使用 TIMESTAMP 列的默认值 CURRENT_TIMESTAMP 并应用 UNIX_TIMESTAMP() 如果你想将它显示为内部:

                The way MySQL implements the TIMESTAMP data type, it is actually storing the epoch time in the database. So you could just use a TIMESTAMP column with a default of CURRENT_TIMESTAMP and apply the UNIX_TIMESTAMP() to it if you want to display it as an int:

                CREATE TABLE foo(
                  created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
                );
                
                insert into foo values (current_Date()),(now());
                
                select unix_timestamp(created) from foo;
                +-------------------------+
                | unix_timestamp(created) |
                +-------------------------+
                |              1300248000 |
                |              1300306959 |
                +-------------------------+
                2 rows in set (0.00 sec)
                

                但是,如果您确实希望列的数据类型为 INT,则可以使用 R. Bemrose 的建议并通过触发器进行设置:

                However, if you really want the datatype of the column to be INT, you can use R. Bemrose's suggestion and set it via trigger:

                CREATE TABLE foo(
                  created INT NULL
                );
                
                delimiter $$
                
                create trigger tr_b_ins_foo before insert on foo for each row
                begin
                  if (new.created is null)
                  then
                    set new.created = unix_timestamp();
                  end if;
                end $$
                
                delimiter ;
                
                
                insert into foo values (unix_timestamp(current_Date())), (null);
                
                select created from foo;
                +------------+
                | created    |
                +------------+
                | 1300248000 |
                | 1300306995 |
                +------------+
                2 rows in set (0.00 sec)
                

                这篇关于是否可以在 MySQL 中创建一个具有 UNIX_TIMESTAMP 默认值的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How do I split flat file data and load into parent-child tables in database?(如何拆分平面文件数据并加载到数据库中的父子表中?)
                How to import CSV into sqlite using RSqlite?(如何使用 RSqlite 将 CSV 导入 sqlite?)
                Import CSV to Update rows in table(导入 CSV 以更新表中的行)
                Importing MaxMind#39;s GeoLite2 to MySQL(将 MaxMind 的 GeoLite2 导入 MySQL)
                Import / Export database with SQL Server Server Management Studio(使用 SQL Server Server Management Studio 导入/导出数据库)
                How do you import a large MS SQL .sql file?(如何导入大型 MS SQL .sql 文件?)

                <small id='fR5oF'></small><noframes id='fR5oF'>

                1. <legend id='fR5oF'><style id='fR5oF'><dir id='fR5oF'><q id='fR5oF'></q></dir></style></legend>

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