MySQL automatically adds system time

MySQL time type

  • DATE type: storage year month day
  • TIME type: store hours, minutes and seconds
  • DATETIME type: store YYYY-MM-DD HH:MM:SS
  • TIMESTAMP type: store YYYYMMDDHHMMSS

When using the mysql database, the commonly used types are date and datetime

What is the difference between datetime and timestamp

  • Both can be used to store: year, month, day, hour, minute, and second, both of which can be accurate to milliseconds, such as: datetime(3), timestamp(3), which are accurate to three-digit milliseconds
  • timestamp is more suitable for business across time zones. Versions after mysql5.6.4 can be accurate to milliseconds, and you can specify

The time type of mysql has two attributes, namely CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP. The usage conditions are as follows:

  • CURRENT_TIMESTAMP: When performing an insert operation to the database, if there is a timestamp field attribute set to CURRENT_TIMESTAMP, the current system time will be inserted regardless of whether the field has a set value
  • ON UPDATE CURRENT_TIMESTAMP: When the update operation is performed, and the field has the ON UPDATE CURRENT_TIMESTAMP attribute. No matter whether the value of the field has changed or not, its value will also be updated to the time of the current UPDATE operation

For example: specify the creation time and modification time fields when creating a table name:

CREATE TABLE `example` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID信息',
`name` varchar(100) NOT NULL DEFAULT "" COMMENT '姓名',
`age`  int  NOT NULL COMMENT '年龄',
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Look at the table structure, desc example, as follows:

mysql> DESC example;
+----------+--------------+------+-----+-------------------+-----------------------------------------------+
| Field    | Type         | Null | Key | Default           | Extra                                         |
+----------+--------------+------+-----+-------------------+-----------------------------------------------+
| id       | int unsigned | NO   | PRI | NULL              | auto_increment                                |
| name     | varchar(100) | NO   |     |                   |                                               |
| age      | int          | NO   |     | NULL              |                                               |
| created  | timestamp    | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED                             |
| modified | timestamp    | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
+----------+--------------+------+-----+-------------------+-----------------------------------------------+
5 rows in set (0.00 sec)

Execute the command to insert data, and observe the creation and modification time, both of which are the current operation time, as shown below:

mysql> INSERT INTO example (name,age) values ("jerry",25);
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM example;
+----+-------+-----+---------------------+---------------------+
| id | name  | age | created             | modified            |
+----+-------+-----+---------------------+---------------------+
|  1 | kim   |  18 | 2023-07-18 14:11:31 | 2023-07-18 14:12:33 |
|  2 | jerry |  25 | 2023-07-18 14:33:06 | 2023-07-18 14:33:06 |
+----+-------+-----+---------------------+---------------------+
2 rows in set (0.00 sec)

After executing the update command, check the modification time again, as follows:

mysql> UPDATE example SET age=18 WHERE id=1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> SELECT * FROM example;
+----+-------+-----+---------------------+---------------------+
| id | name  | age | created             | modified            |
+----+-------+-----+---------------------+---------------------+
|  1 | kim   |  18 | 2023-07-18 14:11:31 | 2023-07-18 14:12:33 |
|  2 | jerry |  25 | 2023-07-18 14:33:06 | 2023-07-18 14:33:06 |
+----+-------+-----+---------------------+---------------------+
2 rows in set (0.00 sec)

Summary: When the default value of the field is set to CURRENT_TIMESTAMP, when new content is added, the corresponding time will be filled with the current time, but the time remains unchanged when modified. If the default value is set to CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, when new content is added , the corresponding time will be filled with the current time, and when modified, the time will also be refreshed to the current modified time

Note: It is also possible to change the field type of the above created table from timestamp to datetime. COMMENT means a comment, so you can know the function of this field

Guess you like

Origin blog.csdn.net/qq_32262243/article/details/131825792