Insert timestamp into MySQL table
Today, we will learn how to TIMESTAMP
insert date and time into a type column of a MySQL table according to the table definition.
Create a MySQL table
First, we will create the tables that we will use in this tutorial.
Sample code:
CREATE TABLE demo_one(
demo_one_id INT NOT NULL,
demo_one_timestamp TIMESTAMP NOT NULL,
PRIMARY KEY(demo_one_id)
);
Use DESCRIBE as follows to learn more about the table definition.
DESCRIBE demo_one;
Output:
+--------------------+-----------+------+-----+---------+-------+
| 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)
We can see that both attributes do not accept NULL values and we have not set any default values for them. This means we have to insert values for both columns.
Inserting timestamp into MySQL table using NOW()
The first method we will demonstrate for inserting a TIMESTAMP into a MySQL table is NOW().
INSERT INTO demo_one (demo_one_id, demo_one_timestamp)
VALUES
(1, NOW());
Use the SELECT statement to view the current table data.
SELECT * FROM demo_one;
Output:
+-------------+---------------------+
| demo_one_id | demo_one_timestamp |
+-------------+---------------------+
| 1 | 2022-05-14 11:04:11 |
+-------------+---------------------+
1 row in set (0.00 sec)
Inserting timestamp into MySQL table using CURRENT_TIMESTAMP()
We can also use CURRENT_TIMESTAMP()
the method to insert a TIMESTAMP into the MySQL table we created earlier.
INSERT INTO demo_onE (demo_one_id, demo_one_timestamp)
VALUES
(2, CURRENT_TIMESTAMP());
We can view the data of the table using the SELECT statement given below.
SELECT * FROM demo_one;
Output:
+-------------+---------------------+
| 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)
Set null or default value for TIMESTAMP type column in MySQL table
If we do not want to insert TIMESTAMP
a value for a type column, we can do this in two ways. Set the column to accept NULL values or set a default value for the column.
Sample code (accepts null value for demo_two_timestamp field):
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;
Output:
+-------------+---------------------+
| demo_two_id | demo_two_timestamp |
+-------------+---------------------+
| 1 | NULL |
| 2 | 2022-05-14 11:15:18 |
+-------------+---------------------+
2 rows in set (0.04 sec)
Here, if we don't want to insert demo_two_timestamp
the value of the column, we can use NULL. We also have the flexibility to use NOW()
or CURRENT_TIMESTAMP()
to insert the correct value if we don't want a particular record to be NULL.
Sample code (setting the default value of the demo_three_timestamp field):
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;
Output:
+---------------+----------------------+
| 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)
If we neither want to enter demo_three_timestamp
a value for the column nor want to have NULLs, then we can use DEFAULT CURRENT_TIMESTAMP
set a default value as shown in the above example.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Select first row from MySQL table
Publish Date:2025/04/23 Views:112 Category:MySQL
-
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic
Converting from datetime type to date-only in MySQL
Publish Date:2025/04/23 Views:199 Category:MySQL
-
Today, we will learn the DATE(), CAST(), CONVERT() and DATE_FORMAT() methods to convert DATETIME type to DATE type in MySQL. The above mentioned methods can be used in MySQL 4.0 and above. Converting from DATETIME to DATE in MySQL To unders
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:193 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:196 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Compare only MySQL timestamp dates to date parameters
Publish Date:2025/04/22 Views:64 Category:MySQL
-
In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:67 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re
Selecting multiple values using WHERE in MySQL
Publish Date:2025/04/22 Views:186 Category:MySQL
-
This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi
Changing the connection timeout in MySQL
Publish Date:2025/04/22 Views:59 Category:MySQL
-
We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti