码迷,mamicode.com
首页 > 数据库 > 详细

Mysql Create Table 语句中Date类型

时间:2015-04-28 12:05:17      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:mysql   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)中找到对格式的定义:

%aAbbreviated weekday name (Sun..Sat)
%bAbbreviated month name (Jan..Dec)
%cMonth, numeric (0..12)
%DDay of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%dDay of the month, numeric (00..31)
%eDay of the month, numeric (0..31)
%fMicroseconds (000000..999999)
%HHour (00..23)
%hHour (01..12)
%IHour (01..12)
%iMinutes, numeric (00..59)
%jDay of year (001..366)
%kHour (0..23)
%lHour (1..12)
%MMonth name (January..December)
%mMonth, numeric (00..12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss followed by AM or PM)
%SSeconds (00..59)
%sSeconds (00..59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00..53), where Sunday is the first day of the week; WEEK() mode 0
%uWeek (00..53), where Monday is the first day of the week; WEEK() mode 1
%VWeek (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%vWeek (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%WWeekday name (Sunday..Saturday)
%wDay of the week (0=Sunday..6=Saturday)
%XYear for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%xYear for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%YYear, numeric, four digits
%yYear, numeric (two digits)
%%A literal % character
%xx, 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


本文出自 “LinuxOracle” 博客,请务必保留此出处http://onlinekof2001.blog.51cto.com/3106724/1639582

Mysql Create Table 语句中Date类型

标签:mysql   date类型   

原文地址:http://onlinekof2001.blog.51cto.com/3106724/1639582

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!