设为首页 收藏本站
查看: 645|回复: 0

[经验分享] Mysql Create Table 语句中Date类型

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-4-29 09:10:15 | 显示全部楼层 |阅读模式
Mysql创建语句中的数据类型包括时间类型,有一下几类:
| DATE  | TIME[(fsp)]  | TIMESTAMP[(fsp)]  | DATETIME[(fsp)]  | YEAR
这几个类型中,特别值得注意的是DATE,DATETIME,TIMESTAMP有什么区别?
DATE
1
2
3
4
5
6
7
mysql> select get_format(date,'ISO');   
+------------------------+
| get_format(date,'ISO') |
+------------------------+
| %Y-%m-%d               |
+------------------------+
1 row in set (0.00 sec)



DATETIME
1
2
3
4
5
6
7
mysql> select get_format(datetime,'ISO');
+----------------------------+
| get_format(datetime,'ISO') |
+----------------------------+
| %Y-%m-%d %H:%i:%s          |
+----------------------------+
1 row in set (0.00 sec)



TIMESTAMP
1
2
3
4
5
6
7
mysql> select get_format(timestamp,'ISO');      
+-----------------------------+
| get_format(timestamp,'ISO') |
+-----------------------------+
| %Y-%m-%d %H:%i:%s           |
+-----------------------------+
1 row in set (0.00 sec)



TIME
1
2
3
4
5
6
7
mysql> select get_format(time,'ISO');   
+------------------------+
| get_format(time,'ISO') |
+------------------------+
| %H:%i:%s               |
+------------------------+
1 row in set (0.00 sec)



YEAR
1
2
3
4
5
6
7
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'
正确的创建默认时间只能如下:
1
2
3
4
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/ ... function_get-format
http://dev.mysql.com/doc/refman/5.6/en/create-table.html

http://dev.mysql.com/doc/refman/ ... initialization.html


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-61780-1-1.html 上篇帖子: mysql高可用方案之MHA 下篇帖子: mysql锁机制详解及死锁处理方式 Create
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表