Mysql Create Table 语句中Date类型

Mysql创建语句中的数据类型包括时间类型,有一下几类:

| DATE
 | TIME[(fsp)]
 | TIMESTAMP[(fsp)]
 | DATETIME[(fsp)]
 | YEAR

这几个类型中,特别值得注意的是DATE,DATETIME,TIMESTAMP有什么区别?

DATE

mysql> select get_format(date,‘ISO‘);    
+------------------------+
| get_format(date,‘ISO‘) |
+------------------------+
| %Y-%m-%d               |
+------------------------+
1 row in set (0.00 sec)

DATETIME

mysql> select get_format(datetime,‘ISO‘);
+----------------------------+
| get_format(datetime,‘ISO‘) |
+----------------------------+
| %Y-%m-%d %H:%i:%s          |
+----------------------------+
1 row in set (0.00 sec)

TIMESTAMP

mysql> select get_format(timestamp,‘ISO‘);       
+-----------------------------+
| get_format(timestamp,‘ISO‘) |
+-----------------------------+
| %Y-%m-%d %H:%i:%s           |
+-----------------------------+
1 row in set (0.00 sec)

TIME

mysql> select get_format(time,‘ISO‘);    
+------------------------+
| get_format(time,‘ISO‘) |
+------------------------+
| %H:%i:%s               |
+------------------------+
1 row in set (0.00 sec)

YEAR

mysql> select year(curdate());
+-----------------+
| year(curdate()) |
+-----------------+
|            2015 |
+-----------------+
1 row in set (0.00 sec)

上述中用到的是format的定义可以再函数DATE_FORMAT(date,format)中找到对格式的定义:

%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where
Sunday is the first day of the week; WEEK() mode 0
%u Week (00..53), where
Monday is the first day of the week; WEEK() mode 1
%V Week (01..53), where
Sunday is the first day of the week; WEEK() mode 2; used with %X
%v Week (01..53), where
Monday is the first day of the week; WEEK() mode 3; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four
digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four
digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal “%” character
%x x, for any “x” not listed
above

为什么需要了解这点,首先在创建时间类型的字段时,如果需要指定时间类型和默认值,那么类型和默认值得关系必须明确,在官网中提到以下内容:

The DEFAULT clause specifies a default value for a
column. With one exception, the default value must be a constant; it cannot be a
function or an expression. This means, for example, that you cannot set the
default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP or (as of MySQL
5.6.5) DATETIME column. See Section 11.3.5, “Automatic Initialization
and Updating for TIMESTAMP and DATETIME”.

简单的说就是默认值必须是常量,不允许使用时间函数,只能使用CURRENT_TIMESTAMP作为TIMESTAMP和DATETIME类型的默认值。比如指定了默认值,那么当使用DATE类型时会明显的出现如下错误警告:

[Err] 1067 - Invalid default value for ‘ACT_DATE‘
正确的创建默认时间只能如下:

CREATE TABLE ACT_TAB (
  ACT_TIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  ACT_DATE DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

参照:

http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_get-format

http://dev.mysql.com/doc/refman/5.6/en/create-table.html

http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html

时间: 2024-08-19 02:46:00

Mysql Create Table 语句中Date类型的相关文章

SQL CREATE TABLE 语句\SQL 约束 (Constraints)\SQL NOT NULL 约束\SQL UNIQUE 约束

CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库中的表. SQL CREATE TABLE 语法 CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据类型, 列名称3 数据类型, .... ) 数据类型(data_type)规定了列可容纳何种数据类型.下面的表格包含了SQL中最常用的数据类型: 数据类型 描述 integer(size) int(size) smallint(size) tinyint(size) 仅容纳整数.在括号内规定数字的最

[24]SQL CREATE TABLE 语句

[24]SQL CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库中的表. 表由行和列组成,每个表都必须有个表名. SQL CREATE TABLE 语法 CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), .... ); column_name 参数规定表中列的名称. data_type 参

mysql create table - data_type length -- clwu

mysql create table 时,有时需要指定  data_type length http://dev.mysql.com/doc/refman/5.5/en/create-table.html 但 类型的长度很多开发并不理解,理解上可以分为两种 1)数字类型: 如 int 是 4个byte,32个bit, 对于 signed int     它的最大值是 2**32-1=4294967295,带符号显示为 +4294967295 刚好是 11 个字符, 对于 unsigned int

MySql的like语句中的通配符:百分号、下划线和escape

%代表任意多个字符 Sql代码 http://blog.csdn.net/yc7369/ select * from user where username like '%huxiao'; select * from user where username like 'huxiao%'; select * from user where username like '%huxiao%'; _代表一个字符 Sql代码 select * from user where username like '

Mysql 数据查询语句中between and 是包含边界值的

MySQL的sql语句中可以使用between来限定一个数据的范围,例如: select * from user where userId between 5 and 7; 查询userId为5.6,7的user,userId范围是包含边界值的,也等同如下查询: select * from user where userId >= 5 and userId <= 7; 很多地方都提到between是给定的范围是大于等第一值,小于第二个值,其实这是不对的.此前我一直也是这么认为,通过实验,结论是

SpringMVC返回Json,自定义Json中Date类型格式

http://www.cnblogs.com/jsczljh/p/3654636.html ———————————————————————————————————————————————————————————— SpringMVC返回Json,自定义Json中Date类型格式 SpringMVC返回Json数据依赖jackson这个开源的第三方类库. 若不加任何说明情况下Date类型将以时间戳的形式转换为Json并返回. jackson提供了一些自定义格式的方法.我们只需继承它的抽象类Json

MySQL 如何在一个语句中更新一个数值后返回该值 -- 自增长种子竞态问题处理

什么是竞态问题? 假设有一个计数器,首先当前值自增长,然后获取到自增长之后的当前值.自增长后的值有可能被有些操作用来当做唯一性标识,因此并发的操作不能允许取得相同的值. 为什么不能使用使用UPDATE语句更新计数器,然后SELECT语句获取自增长后的当前值?问题在于并发的操作有可能获取到相同的计数器值. CREATE TABLE counters ( id INT NOT NULL UNIQUE, -- 计数器ID,多个计数器可以存在一个表中, value INT -- 计数器当前值 ); --

转换sql文件的create table语句为drop table语句

1 package com.csii.pweb.query.action; 2 3 import java.io.BufferedReader; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.IOException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 11 public class Ja

Java中Date类型详解

一.Date类型的初始化 1. Date(int year, int month, int date); 直接写入年份是得不到正确的结果的. 因为java中Date是从1900年开始算的,所以前面的第一个参数只要填入从1900年后过了多少年就是你想要得到的年份. 月需要减1,日可以直接插入. 这种方法用的比较少,常用的是第二种方法. 2. 这种方法是将一个符合特定格式,比如yyyy-MM-dd,的字符串转化成为Date类型的数据. 首先,定义一个Date类型的对象 Date date = nul