迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 数据库 > MySQL >

将时间戳插入 MySQL 表

作者:迹忆客 最近更新:2023/04/19 浏览次数:

今天,我们将学习如何根据表定义向MySQL表的 TIMESTAMP 类型列中插入日期和时间。


创建一个 MySQL 表

首先,我们将创建我们将在本教程中使用的表。

示例代码:

CREATE TABLE demo_one(
    demo_one_id INT NOT NULL,
    demo_one_timestamp TIMESTAMP NOT NULL,
    PRIMARY KEY(demo_one_id)
);

如下使用 DESCRIBE 了解有关表定义的更多信息。

DESCRIBE demo_one;

输出:

+--------------------+-----------+------+-----+---------+-------+
| Field              | Type      | Null | Key | Default | Extra |
+--------------------+-----------+------+-----+---------+-------+
| demo_one_id        | int       | NO   | PRI | NULL    |       |
| demo_one_timestamp | timestamp | NO   |     | NULL    |       |
+--------------------+-----------+------+-----+---------+-------+
2 rows in set (0.08 sec)

我们可以看到这两个属性都不接受 NULL 值,并且我们没有为它们设置任何默认值。 这意味着我们必须为两列插入值。


使用 NOW() 将时间戳插入 MySQL 表

我们将演示的第一种将 TIMESTAMP 插入 MySQL 表的方法是 NOW()。

INSERT INTO demo_one (demo_one_id, demo_one_timestamp)
VALUES
(1, NOW());

使用 SELECT 语句查看当前表数据。

SELECT * FROM demo_one;

输出:

+-------------+---------------------+
| demo_one_id | demo_one_timestamp  |
+-------------+---------------------+
|           1 | 2022-05-14 11:04:11 |
+-------------+---------------------+
1 row in set (0.00 sec)

使用 CURRENT_TIMESTAMP() 将时间戳插入 MySQL 表

我们还可以使用 CURRENT_TIMESTAMP() 方法将 TIMESTAMP 插入到我们之前创建的 MySQL 表中。

INSERT INTO demo_onE (demo_one_id, demo_one_timestamp)
VALUES
(2, CURRENT_TIMESTAMP());

我们可以使用下面给出的 SELECT 语句来查看表的数据。

SELECT * FROM demo_one;

输出:

+-------------+---------------------+
| demo_one_id | demo_one_timestamp  |
+-------------+---------------------+
|           1 | 2022-05-14 11:04:11 |
|           2 | 2022-05-14 11:06:01 |
+-------------+---------------------+
2 rows in set (0.03 sec)

为 MySQL 表中的 TIMESTAMP 类型列设置空值或默认值

如果我们不想插入 TIMESTAMP 类型列的值,我们可以通过两种方式做到这一点。 将该列设置为接受 NULL 值或为该列设置默认值。

示例代码(接受 demo_two_timestamp 字段的空值):

CREATE TABLE demo_two(
    demo_two_id INT NOT NULL,
    # we can also write the following column definition
    # as `demo_two_timestamp TIMESTAMP NULL`
    demo_two_timestamp TIMESTAMP,
    PRIMARY KEY(demo_two_id)
);

INSERT INTO demo_two (demo_two_id, demo_two_timestamp) VALUES (1,null), (2, NOW());

SELECT * FROM demo_two;

输出:

+-------------+---------------------+
| demo_two_id | demo_two_timestamp  |
+-------------+---------------------+
|           1 | NULL                |
|           2 | 2022-05-14 11:15:18 |
+-------------+---------------------+
2 rows in set (0.04 sec)

在这里,如果我们不想插入 demo_two_timestamp 列的值,我们可以使用 NULL。 如果我们不想让特定记录为 NULL,我们还可以灵活地使用 NOW()CURRENT_TIMESTAMP() 插入正确的值。

示例代码(设置 demo_three_timestamp 字段的默认值):

CREATE TABLE demo_three(
    demo_three_id INT NOT NULL,
    demo_three_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY(demo_three_id)
);

INSERT INTO demo_three (demo_three_id) VALUES (1);
INSERT INTO demo_three (demo_three_id, demo_three_timestamp) VALUES (2, NOW());
INSERT INTO demo_three (demo_three_id, demo_three_timestamp)
VALUES (3, CURRENT_TIMESTAMP());

SELECT * FROM demo_three;

输出:

+---------------+----------------------+
| demo_three_id | demo_three_timestamp |
+---------------+----------------------+
|             1 | 2022-05-14 11:21:57  |
|             2 | 2022-05-14 11:22:20  |
|             3 | 2022-05-14 11:22:40  |
+---------------+----------------------+
3 rows in set (0.00 sec)

如果我们既不想输入 demo_three_timestamp 列的值也不希望有 NULL,那么我们可以使用 DEFAULT CURRENT_TIMESTAMP 设置默认值,如上例所示。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 MySQL 中声明和使用变量

发布时间:2024/03/26 浏览次数:115 分类:MySQL

当你需要在 MySQL 中的脚本中存储单个值时,最好的方法是使用变量。变量有不同的种类,有必要知道何时以及如何使用每种类型。

在 MySQL 中实现刷新权限

发布时间:2024/03/26 浏览次数:211 分类:MySQL

本教程介绍了 MySQL 中的刷新权限命令,用于刷新授权表并影响允许的更改。

在 MySQL 中设置时区

发布时间:2024/03/26 浏览次数:93 分类:MySQL

在本教程中,我们将学习如何在 MySQL 服务器中更改时区。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便