Selection of time type
The time field type can be datetime and timestamp. The following table shows the difference between the two:
timestamp Translated into Chinese, it is "timestamp". It is the number of seconds from the current time to the first year of Unix (January 1, 1970, 0:00:00). It occupies 4 bytes and is stored in UTC format. It The current time zone is automatically retrieved and converted. Datetime is stored in 8 bytes and no time zone retrieval is performed. In other words, for timestamp, if the time zone when storing is different from the time zone when retrieving, the data taken out will also be different. For datetime, you get what you save. Here are some common cases and selection suggestions.
Select according to the storage range, such as production time, shelf life, etc. It is recommended to select datetime, because datetime can store a wider range.
It is recommended to use timestamp to record the insertion time and modification time of this row of data.
Select timestamp for the time field related to the time zone.
If you just want to represent the year, date, and time, you can also use year, date, and time. They occupy 1, 3, and 3 bytes respectively, and datetime is their set.
If the timestamp field is often used in queries, we can also use MySQL's built-in functions FROM_UNIXTIME()
, UNIX_TIMESTAMP()
to convert the date Convert back and forth with timestamp numbers. After conversion, you can use INT UNSIGNED to store the time. The numbers are continuous, occupy less space, and indexes can be used to improve query performance. The following is a sample table creation statement and timestamp-related conversion SQL:
CREATE TABLE `tb_time` ( `increment_id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键', `col1` datetime NOT NULL DEFAULT '2020-10-01 00:00:00' COMMENT '到期时间', `unix_createtime` int unsigned NOT NULL COMMENT '创建时间戳', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间', PRIMARY KEY (`increment_id`), KEY `idx_unix_createtime` (`unix_createtime`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='time测试表'; # 插入数据 insert into tb_time (unix_createtime,create_time) values (UNIX_TIMESTAMP(now()),now()); # 时间戳数字与时间相互转换 select UNIX_TIMESTAMP('2020-05-06 00:00:00') select FROM_UNIXTIME(1588694400)
The above is the detailed content of What are the time types in mysql. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
