MySQL建表时,日期时间类型选择
MySQL(5.5)所支持的日期时间类型有:DATETIME、 TIMESTAMP、DATE、TIME、YEAR。
几种类型比较如下:
日期时间类型 | 占用空间 | 日期格式 | 最小值 | 最大值 | 零值表示 |
DATETIME | 8 bytes | YYYY-MM-DD HH:MM:SS | 1000-01-01 00:00:00 | 9999-12-31 23:59:59 | 0000-00-00 00:00:00 |
TIMESTAMP | 4 bytes | YYYY-MM-DD HH:MM:SS | 19700101080001 | 2038 年的某个时刻 | 00000000000000 |
DATE | 4 bytes | YYYY-MM-DD | 1000-01-01 | 9999-12-31 | 0000-00-00 |
TIME | 3 bytes | HH:MM:SS | -838:59:59 | 838:59:59 | 00:00:00 |
YEAR | 1 bytes | YYYY | 1901 | 2155 | 0000 |
DATETIME
DATETIME 用于表示 年月日 时分秒,是 DATE 和 TIME 的组合,并且记录的年份(见上表)比较长久。如果实际应用中有这样的需求,就可以使用 DATETIME 类型。
TIMESTAMP
--TIMESTAMP 用于表示 年月日 时分秒,但是记录的年份(见上表)比较短暂。
--TIMESTAMP 和时区相关,更能反映当前时间。当插入日期时,会先转换为本地时区后再存放;当查询日期时,会将日期转换为本地时区后再显示。所以不同时区的人看到的同一时间是 不一样的。
--表中的第一个 TIMESTAMP 列自动设置为系统时间(CURRENT_TIMESTAMP)。当插入或更新一行,但没有明确给 TIMESTAMP 列赋值,也会自动设置为当前系统时间。如果表中有第二个 TIMESTAMP 列,则默认值设置为0000-00-00 00:00:00。
--TIMESTAMP 的属性受 Mysql 版本和服务器 SQLMode 的影响较大。
-如果记录的日期需要让不同时区的人使用,最好使用 TIMESTAMP。
DATE
DATE 用于表示 年月日,如果实际应用值需要保存 年月日 就可以使用 DATE。
TIME
TIME 用于表示 时分秒,如果实际应用值需要保存 时分秒 就可以使用 TIME。
YEAR
YEAR 用于表示 年份,YEAR 有 2 位(最好使用4位)和 4 位格式的年。 默认是4位。如果实际应用只保存年份,那么用 1 bytes 保存 YEAR 类型完全可以。不但能够节约存储空间,还能提高表的操作效率。
-----------------------
The DATE, DATETIME, and TIMESTAMP types are related. This section describes their characteristics, how they are similar, and how they differ. MySQL recognizes DATE, DATETIME, and TIMESTAMP values in several formats. For the DATE and DATETIME range descriptions, “supported” means that although earlier values might work, there is no guarantee. The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'. The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision. In particular, any fractional part in a value inserted into a DATETIME or TIMESTAMP column is stored rather than discarded. With the fractional part included, the format for these values is 'YYYY-MM-DD HH:MM:SS[.fraction]', the range for DATETIME values is '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999', and the range for TIMESTAMP values is '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'. The fractional part should always be separated from the rest of the time by a decimal point; no other fractional seconds delimiter is recognized. For information about fractional seconds support in MySQL. The TIMESTAMP and DATETIME data types offer automatic initialization and updating to the current date and time. For more information. MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval(TIMESTAMP可以做时区转换). (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable. Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').
MySQL建表时,日期时间类型选择的更多相关文章
- Mysql 建表时,日期时间类型选择
mysql(5.5)所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值表示 D ...
- SqlServer数据库表导入SqlLite数据库表保持日期时间类型字段的格式
在写查询功能的过程中遇到一个这样的问题:按日期范围查询,sql语句是:where dt>=用户选择起始日期&&dt<=用户选择结束日期.数据库中的数据如图1,我选择的测试数 ...
- mysql中几个日期时间类型之间的区别和使用
MySQL中有如下几个时间类型:date.time.datetime.timestamp.year MySQL数据类型 含义 date 只存 ...
- MySQL建表时列名同保留字重复问题解决办法
建表时遇到遇到属性名同MySQL的保留字相同导致不能建表的问题,如下SQL语句: CREATE TABLE TBL_ACCOUNT_FROZEN_RECORD ( ID BIGINT NOT NULL ...
- MySQL建表时添加备注以及查看某一张表的备注信息
建表的时候对列和表明添加备注: DROP TABLE IF EXISTS test_table; CREATE TABLE test_table ( ID INTEGER AUTO_INCREMENT ...
- MySQL 建表时 date 类型的默认值设置
在执行下面 SQL 语句时发现报错 CREATE TABLE `jc_site_access_pages` ( `access_date` date NOT NULL DEFAULT '0000-00 ...
- mysql建表时拆分出常用字段和不常用字段
一对一 一张表的一条记录一定只能与另外一张表的一条记录进行对应,反之亦然. 学生表:姓名,性别,年龄,身高,体重,籍贯,家庭住址,紧急联系人 其中姓名.性别.年龄.身高,体重属于常用数据,但是籍贯.住 ...
- mysql建表时
问题:Incorrect column specifier for column 'id' 答案:原来自动增长列用int数据类型,不用varchar
- mysql建表出现Timestamp错误
mysql建表时如果有两个或以上的字段为Timestamp,那么可能会出现如下错误: Incorrect table definition; there can be only one TIMESTA ...
随机推荐
- NOIP 2012 Day1
tags: NOIP 模拟 倍增 高精 Python categories: 信息学竞赛 总结 Luogu P1079 Vigenère 密码 Solution 表示并不是很懂其他人发的题解. 我是这 ...
- jstorm系列-2:入门
有了基本的概念之后,我们用jstorm来做一点小事情吧 做一个很无聊的事情:给定一个时间戳,输出对应的问候语 规则是:时间戳的十位对应的数字对应不同的时间段,0-2代表早上,3代表中午,4-6代表下午 ...
- gitlab和github的区别
先说一下相同点,二者都是基于web的Git仓库,在很大程度上GitLab是仿照GitHub来做的,它们都提供了分享开源项目的平台,为开发团队提供了存储.分享.发布和合作开发项目的中心化云存 ...
- kindeditor异步加载 无法初始化
使用集成的php输出kindeditor无法初始化 function showEditor($id, $value='', $width='700px', $height='300px', $styl ...
- CentOS 7 Docker
安装docker yum install docker 查看docker docker version 这是因为docker还没有运行,需要启动docker 启动docker systemctl st ...
- NestedScrollView
参考文章: Android滑动到顶部悬停 NestedScrollView的使用 效果图: 实现步骤: 将需要悬浮的layout放到CollapsingToolbarLayout之外,AppBarLa ...
- 第5天:Ansible-Playbook
Ansible中的每个模块专注于某一方面的功能.虽然每个模块实现的功能都比较简单,但是,将各个模块结合起来就可以实现比较复杂的功能.在Ansible中,将各个模块组合起来的文件是一个YAML格式的配置 ...
- [AGC012E]Camel and Oases
题意:有$n$个数轴上的绿洲,给定它们的坐标,有一只骆驼想要访问所有绿洲,当它的驼峰容量为$V$时,它可以走到和当前绿洲距离$\leq V$的绿洲,并可以继续走,它也可以用一次跳跃到达任意一个绿洲,只 ...
- [ARC103F]Distance Sums
题意:有一棵树,对于每个点$i$,给出了它到其他点的距离和$i$,现在要还原这棵树,保证$d_i$两两不同 一个点从$u$移到相邻节点$v$时,若删掉$(u,v)$后$u$这边的连通块大小为$siz_ ...
- 【优先队列+贪心】POJ2431-Expedition
解题方法详见<挑战程序设计竞赛(第二版)>P74-75.注意首先要对加油站以位置为关键字快排,不要遗忘把终点视作一个加油量为0的加油站,否则最终只能到达终点前的加油站. /*优先队列*/ ...