MySQL timestamp用法
与timestamp类型相关的类型包括:date类型与datetime类型。date类型只包含日期部分,不包含时间部分,它的格式为'YYYY-MM-DD',支持的范围为'1000-01-01' to '9999-12-31'。datetime类型包含日期和时间两部分,它的格式为'YYYY-MM-DD HH:MM:SS',支持的范围为'1000-01-01 00:00:00' to '9999-12-31 23:59:59'。timestamp也包含日期和时间两部分,支持的范围为'1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC。
MySQL将timestamp值从当前时区转换为UTC存储,查询时再从UTC转换为当前时区的值。
timestamp支持自动初始化和更新到当前日期和时间。下面是自动初始化和更新到当前日期和时间的几种组合方式:
With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP, the column has the current timestamp for its default value and is automatically updated to the current timestamp.
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
With neither DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP, it is the same as specifying both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP.
CREATE TABLE t1 (
ts TIMESTAMP
);
With a DEFAULT clause but no ON UPDATE CURRENT_TIMESTAMP clause, the column has the given default value and is not automatically updated to the current timestamp.
The default depends on whether the DEFAULT clause specifies CURRENT_TIMESTAMP or a constant value. With CURRENT_TIMESTAMP, the default is the current timestamp.
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
With a constant, the default is the given value. In this case, the column has no automatic properties at all.
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT 0
);
With an ON UPDATE CURRENT_TIMESTAMP clause and a constant DEFAULT clause, the column is automatically updated to the current timestamp and has the given constant default value.
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP
);
With an ON UPDATE CURRENT_TIMESTAMP clause but no DEFAULT clause, the column is automatically updated to the current timestamp. The default is 0 unless the column is defined with the NULL attribute, in which case the default is NULL.
CREATE TABLE t1 (
ts TIMESTAMP ON UPDATE CURRENT_TIMESTAMP -- default 0
);
CREATE TABLE t2 (
ts TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL
);
datetime或者timestamp类型可在末尾包含小数部分,用于表示毫秒数。
无效的date、datetime、timestamp值会转换为'0000-00-00' 或者 '0000-00-00 00:00:00'。
MySQL采用如下规则处理两位数年的情况:
00-69被转换为2000-2069
70-99被转换为1970-1999
MySQL timestamp用法的更多相关文章
- 【转载】 mysql explain用法
转载链接: mysql explain用法 官网说明: http://dev.mysql.com/doc/refman/5.7/en/explain-output.html 参数: htt ...
- 知识点:Mysql 基本用法之存储过程
存储过程 一. 介绍 存储过程包含了一系列可执行的sql语句,存储过程存放于MySQL中,通过调用它的名字可以执行其内部的一堆sql 使用存储过程的优点: 用于替代程序写的SQL语句,实现程序与sql ...
- 知识点:Mysql 基本用法之视图
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时 ...
- 知识点:Mysql 基本用法之流程控制
流程控制 一. 条件语句 if 语句实例: delimiter // CREATE PROCEDURE proc_if () BEGIN declare i int default 0; if i = ...
- 知识点:Mysql 基本用法之函数
函数 MySQL中提供了许多内置函数 例如: sql 内置函数: 一.数学函数 ROUND(x,y) 返回参数x的四舍五入的有y位小数的值 RAND() 返回0到1内的随机值,可以通过提供一个参数(种 ...
- mysql timestamp字段定义的
Cause: java.sql.SQLException: Cannot convert value '2017-07-26 20:40:41.000000' from column 10 to TI ...
- 知识点:Mysql 基本用法之触发器
触发器 使用触发器可以定制用户对表进行[增.删.改]操作时前后的行为 注意:没有查询 一 .创建触发器 触发器基础语法: # 插入前 CREATE TRIGGER tri_before_insert_ ...
- 知识点:Mysql 基本用法之事务
事务 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. 事务实例: create table user( id int primar ...
- Mysql基本用法-存储引擎-04
MYSQL中只有 INNODB和BDB 类型的数据表才能支持事务处理!其他的类型是不支持的!(切记!) Mysql基本用法-存储引擎-02中的test_user表 和 phpcvs表 <?php ...
随机推荐
- 【C#学习笔记】检测进程是否存在并关闭
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- LeetCode: 3SumClosest
Title : Given an array S of n integers, find three integers in S such that the sum is closest to a g ...
- 【转】No JVM could be found on your system解决方法
原文网址:http://my.oschina.net/liusicong/blog/324964 在安装android studio时,报错: Error launching android Stud ...
- Android Message和obtainMessage的区别
类概述 定义一个包含任意类型的描述数据对象,此对象可以发送给Handler.对象包含两个额外的int字段和一个额外的对象字段,这样可以使得在很多情况下不用做分配工作. 尽管Message的构造器是公开 ...
- 【转】android:layout_gravity和android:gravity的区别
1.首先来看看android:layout_gravity和android:gravity的使用区别. android:gravity: 这个是针对控件里的元素来说的,用来控制元素在该控件里的显示位置 ...
- Ant编译和部署java web项目
1.在myeclipse中创建javaWeb项目AntDemo 2.将build.xml放到AntDemo根目录下 3.修改build.xml中的Project name,工程目录名,工程名,还有to ...
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- Mobile testing基础之签名
1. 什么是数字签名? 数字签名就是为你的程序打上一种标记,来作为你自己的标识,当别人看到签名的时候会知道它是与你相关的 2. 为什么要数字签名? 最简单直接的回答: 系统要求的. Android系统 ...
- 《Python核心编程》 第三章 Python基础 - 练习
创建文件: # -*- coding: gbk -*- #! /auto/ERP/python_core/chapter ''' Created on 2014年5月21日 @author: user ...
- bug报告-常用词汇中英对照表