Mysql Create Table文のDateタイプ
Mysql作成文のデータ型には、時間型が含まれます.いくつかの種類があります.
| DATE | TIME[(
このいくつかのタイプの中で、特に注目すべきはDATE、DATETIME、TIMESTAMPの違いは何ですか?
DATE
DATETIME
TIMESTAMP
TIME
YEAR
上記で用いたのはformatの定義再関数可能DATE_FORMAT(date,format)でフォーマットの定義を見つけます.
Abbreviated weekday name (
Abbreviated month name (
Month, numeric (
Day of the month with English suffix (
Day of the month, numeric (
Day of the month, numeric (
Microseconds (
Hour (
Hour (
Hour (
Minutes, numeric (
Day of year (
Hour (
Hour (
Month name (
Month, numeric (
Time, 12-hour (
Seconds (
Seconds (
Time, 24-hour (hh:mm:ss)
Week (
Week (
Week (
Week (
Weekday name (
Day of the week (
Year for the week where Sunday is the first day of the week, numeric, four digits; used with
Year for the week, where Monday is the first day of the week, numeric, four digits; used with
Year, numeric, four digits
Year, numeric (two digits)
A literal “
| 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