将时间戳插入 MySQL 表
今天,我们将学习如何根据表定义向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
设置默认值,如上例所示。
相关文章
如何在 MySQL 中声明和使用变量
发布时间:2024/03/26 浏览次数:115 分类:MySQL
-
当你需要在 MySQL 中的脚本中存储单个值时,最好的方法是使用变量。变量有不同的种类,有必要知道何时以及如何使用每种类型。
在 MySQL 中使用 Mysqladmin 刷新主机解除阻塞
发布时间:2024/03/26 浏览次数:82 分类:MySQL
-
你将了解阻止主机的原因。此外,通过使用 phpMyAdmin 和命令提示符刷新主机缓存来解除阻塞的不同方法和效果。