MySQL基础知识03数据类型日期时间相关类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL基础知识03数据类型日期时间相关类型相关的知识,希望对你有一定的参考价值。

1. DATE

DATE类型只包含日期,格式:‘YYYY-MM-DD‘。范围从‘0000-01-01‘‘9999-12-31‘。其中年月日只能输入有效的年月日。

 

mysql> create table d1 ( id  date primary key);

Query OK, 0 rows affected (0.02 sec)

 

mysql> show create table d1;

+-------+-------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                          |

+-------+-------------------------------------------------------------------------------------------------------+

| d1    | CREATE TABLE `d1` (

  `id` date NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+-------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> insert into d1 values (‘0001-01-01‘);

Query OK, 1 row affected (0.01 sec)

 

mysql> select * from d1;

+------------+

| id         |

+------------+

| 0001-01-01 |

+------------+

1 row in set (0.00 sec)

 

 

MySQL将对日期的年月日进行合法性校验。

mysql> insert into d1 values (‘2017-02-29‘);

ERROR 1292 (22007): Incorrect date value: ‘2017-02-29‘ for column ‘id‘ at row 1

mysql> insert into d1 values (‘2017-02-28‘);

Query OK, 1 row affected (0.01 sec)

 

年的范围为00009999

ERROR 1292 (22007): Incorrect date value: ‘20170-02-28‘ for column ‘id‘ at row 1

mysql> insert into d1 values (‘10000-02-28‘);

ERROR 1292 (22007): Incorrect date value: ‘10000-02-28‘ for column ‘id‘ at row 1

mysql> insert into d1 values (‘9999-02-28‘);

Query OK, 1 row affected (0.00 sec)

 

 

 

 

2. TIME

时间类型使用TIME表示。格式‘HH:MM:SS‘

 

mysql> create table d2 ( id time primary key);

Query OK, 0 rows affected (0.04 sec)

 

mysql> show create table d2;

+-------+-------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                          |

+-------+-------------------------------------------------------------------------------------------------------+

| d2    | CREATE TABLE `d2` (

  `id` time NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+-------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

 

mysql> insert into d2 values (‘00:00:00‘);

Query OK, 1 row affected (0.00 sec)

 

mysql> insert into d2 values (‘23:59:59‘);

Query OK, 1 row affected (0.01 sec)

 

mysql> insert into d2 values (‘24:00:00‘);

Query OK, 1 row affected (0.01 sec)

 

mysql> insert into d2 values (‘25:00:00‘);

Query OK, 1 row affected (0.02 sec)

 

mysql> insert into d2 values (‘00:60:60‘);

ERROR 1292 (22007): Incorrect time value: ‘00:60:60‘ for column ‘id‘ at row 1

mysql> insert into d2 values (‘00:00:60‘);

ERROR 1292 (22007): Incorrect time value: ‘00:00:60‘ for column ‘id‘ at row 1

mysql> select * from d2;

+----------+

| id       |

+----------+

| 00:00:00 |

| 23:59:59 |

| 24:00:00 |

| 25:00:00 |

+----------+

4 rows in set (0.00 sec)

 

 

TIME类型的取值范围为:‘00:00:00‘‘838:59:59‘

mysql> insert into d2 values (‘838:59:59‘);

Query OK, 1 row affected (0.01 sec)

 

mysql> insert into d2 values (‘839:00:00‘);

ERROR 1292 (22007): Incorrect time value: ‘839:00:00‘ for column ‘id‘ at row 1

 

3. DATETIME

格式:‘YYYY-MM-DD HH:MM:SS‘

范围:‘0000-01-01 00:00:00‘‘9999-12-31 23:59:59‘

 

mysql> create table d3 ( id datetime primary key);

Query OK, 0 rows affected (0.03 sec)

 

mysql> insert into d3 values (‘0000-01-01 00:00:00‘);

Query OK, 1 row affected (0.01 sec)

 

 

DATETIME中的年月日时分秒只能是合法的取值。其中小时不能超过23

mysql> insert into d3 values (‘0000-01-01 24:00:00‘);

ERROR 1292 (22007): Incorrect datetime value: ‘0000-01-01 24:00:00‘ for column ‘id‘ at row 1

 

DATETIME类型的列可以显式指定默认值和更新时自动更新为当前时间。

mysql> create table d9 ( id int primary key,v1 datetime default current_timestamp on update current_timestamp);

Query OK, 0 rows affected (0.06 sec)

 

mysql> show create table d9;

+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                                                                    |

+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| d9    | CREATE TABLE `d9` (

  `id` int(11) NOT NULL,

  `v1` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> insert into d9 values (1);

ERROR 1136 (21S01): Column count doesn‘t match value count at row 1

mysql> insert into d9 (id) values (1);

Query OK, 1 row affected (0.02 sec)

 

mysql> select * from d9;

+----+---------------------+

| id | v1                  |

+----+---------------------+

|  1 | 2017-09-01 12:37:12 |

+----+---------------------+

1 row in set (0.00 sec)

 

mysql> update d9 set id=2;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql> select * from d9;

+----+---------------------+

| id | v1                  |

+----+---------------------+

|  2 | 2017-09-01 12:37:28 |

+----+---------------------+

1 row in set (0.01 sec)

 

4. TIMESTAMP

格式:‘YYYY-MM-DD HH:MM:SS‘

范围:‘0000-01-01 00:00:00‘‘9999-12-31 23:59:59‘

 

mysql> create table d5 (id timestamp primary key);

Query OK, 0 rows affected (0.03 sec)

 

mysql> show create table d5;

+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                                                     |

+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| d5    | CREATE TABLE `d5` (

  `id` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> insert into d5 values ();

Query OK, 1 row affected (0.01 sec)

 

mysql> select * from d5;

+---------------------+

| id                  |

+---------------------+

| 2017-09-01 12:15:16 |

+---------------------+

1 row in set (0.00 sec)

 

TIMESTAMPDATETIME类型的区别:

(1)TIMESTAMP的默认值为CURRENT_TIMESTAMP,即当前时间。

在定义列时,默认情况下具备以下属性:

 DEFAULT CURRENT_TIMESTAMP

DATETIME类型则默认值为NULL,但是可以显式的指定为DEFAULT CURRENT_TIMESTAMP

 

(2)TIMESTAMP类型的列在行数据更新时,默认情况下自动更新为当前时间。

在定义列时,默认情况下具备以下属性:

ON UPDATE CURRENT_TIMESTAMP,

DATETIME类型则默认情况下并不自动更新,但是可以显式的指定ON UPDATE CURRENT_TIMESTAMP属性。

 

(3)TIMESTAMP类型的列在默认情况下为NOT NULL,如果需要赋值为NULL则必须显式定义为NULL属性。 DATETIME类型在默认情况下为NULL

 

 

 

上述自动赋予的默认值和默认更新值仅仅对于数据表中第1timestamp字段有效。

如果存在多个timestamp字段,则会报错:

mysql> create table d6 ( id int primary key,v1 datetime, v2 timestamp, v3 timestamp);

ERROR 1067 (42000): Invalid default value for ‘v3‘

 

此时需要显式的定义其它timestamp字段的default属性和on update属性。

mysql> create table d6 ( id int primary key,v1 datetime, v2 timestamp, v3 timestamp default current_timestamp on update current_timestamp);

Query OK, 0 rows affected (0.03 sec)

 

mysql> show create table d6;

+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                                                                                                                                                                                             |

+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| d6    | CREATE TABLE `d6` (

  `id` int(11) NOT NULL,

  `v1` datetime DEFAULT NULL,

  `v2` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  `v3` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

 

TIMESTAMP类型在默认情况下的DEFAULT值是否定义为CURRENT_TIMESTAMP,以及ON UPDATE属性是否定义为CURRENT_TIMESTAMP,可以通过全局变量来控制:

mysql> show variables like ‘%timestamp%‘;

+---------------------------------+-------------------+

| Variable_name                   | Value             |

+---------------------------------+-------------------+

| explicit_defaults_for_timestamp | OFF               |

| log_timestamps                  | UTC               |

| timestamp                       | 1504241030.617255 |

+---------------------------------+-------------------+

3 rows in set (0.16 sec)

 

explicit_defaults_for_timestamp 这个全局变量默认情况下为OFF,即不需要显式定义TIMESTAMP的默认值;如果修改为ON,则不会在定义数据表时自动加上DEFAULTON UPDATE属性。

 

mysql> set explicit_defaults_for_timestamp=on;

Query OK, 0 rows affected (0.05 sec)

 

mysql> create table d11 ( id timestamp primary key);

Query OK, 0 rows affected (0.05 sec)

 

mysql> show create table d11;

+-------+-------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                |

+-------+-------------------------------------------------------------------------------------------------------------+

| d11   | CREATE TABLE `d11` (

  `id` timestamp NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+-------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

 

5. 日期时间相关函数

 

(1)取得当前日期和时间。

now()取得当前日期和时间。

current_timestamp()取得当前日期和时间。

 

mysql> select now(), current_timestamp() ;

+---------------------+---------------------+

| now()               | current_timestamp() |

+---------------------+---------------------+

| 2017-09-01 12:39:50 | 2017-09-01 12:39:50 |

+---------------------+---------------------+

1 row in set (0.00 sec)

 

mysql> select current_timestamp ;

+---------------------+

| current_timestamp   |

+---------------------+

| 2017-09-01 12:40:01 |

+---------------------+

1 row in set (0.00 sec)

 

now()函数必须是函数调用的形式。

mysql> select now ;

ERROR 1054 (42S22): Unknown column ‘now‘ in ‘field list‘

 

 

(2)独立取得当前日期和时间。

curdate()取得当前日期,curtime()取得当前时间。

mysql> select curdate(),curtime();

+------------+-----------+

| curdate()  | curtime() |

+------------+-----------+

| 2017-09-01 | 13:34:14  |

+------------+-----------+

1 row in set (0.00 sec)

 

(3)取得指定日期时间的年月日时分秒的值。

mysql> select now(),year(now()), month(now()),day(now()), hour(now()),minute(now()),second(now());

+---------------------+-------------+--------------+------------+-------------+---------------+---------------+

| now()               | year(now()) | month(now()) | day(now()) | hour(now()) | minute(now()) | second(now()) |

+---------------------+-------------+--------------+------------+-------------+---------------+---------------+

| 2017-09-01 13:48:26 |        2017 |            9 |          1 |          13 |            48 |            26 |

+---------------------+-------------+--------------+------------+-------------+---------------+---------------+

1 row in set (0.00 sec)

 

 

 

(4)等待一段时间。

sleep()函数提供了等待指定秒数的功能。

mysql> select now(); select  sleep(11); select now();

+---------------------+

| now()               |

+---------------------+

| 2017-09-01 13:54:07 |

+---------------------+

1 row in set (0.00 sec)

 

+-----------+

| sleep(11) |

+-----------+

|         0 |

+-----------+

1 row in set (11.00 sec)

 

+---------------------+

| now()               |

+---------------------+

| 2017-09-01 13:54:18 |

+---------------------+

1 row in set (0.00 sec)

 

(5)sysdate

sysdate()取得当前日期和时间。

now()函数返回的是语句开始执行时系统当前时间;而sysdate()函数返回的是函数执行时系统当前时间。

mysql> select now(), sleep(3), now();

+---------------------+----------+---------------------+

| now()               | sleep(3) | now()               |

+---------------------+----------+---------------------+

| 2017-09-01 14:01:34 |        0 | 2017-09-01 14:01:34 |

+---------------------+----------+---------------------+

1 row in set (3.00 sec)

可以看到两个now()返回的值相同,因为都是SELECT语句开始执行时的时间。

 

mysql> select sysdate(),sleep(3),sysdate();

+---------------------+----------+---------------------+

| sysdate()           | sleep(3) | sysdate()           |

+---------------------+----------+---------------------+

| 2017-09-01 14:01:49 |        0 | 2017-09-01 14:01:52 |

+---------------------+----------+---------------------+

1 row in set (3.01 sec)

两个sysdate()返回的时间并不相同,相隔了3秒钟。

 

 

6. 两位数年份

对于日期相关类型,如果输入两位数年份,则根据年份所在的范围识别为20XX19XX

(1)0069:结果为20XX

(2)7099:结果为19XX

 

mysql> select cast( ‘17-09-01‘ as date) as "17-09-01", cast(‘69-09-01‘ as date) as "69-09-01" ,  cast(‘70-09-01‘ as date ) as "70-09-01";

+------------+------------+------------+

| 17-09-01   | 69-09-01   | 70-09-01   |

+------------+------------+------------+

| 2017-09-01 | 2069-09-01 | 1970-09-01 |

+------------+------------+------------+

1 row in set (0.00 sec)

 

 

7. 微秒

对于时间相关类型TIME,TIMESTAMP,DATETIMEMySQL提供了微秒的表达方式。

mysql> select cast( ‘2017-09-01 13:51:25.123456‘  as datetime(6));

+-----------------------------------------------------+

| cast( ‘2017-09-01 13:51:25.123456‘  as datetime(6)) |

+-----------------------------------------------------+

| 2017-09-01 13:51:25.123456                          |

+-----------------------------------------------------+

1 row in set (0.00 sec)

 

以上是关于MySQL基础知识03数据类型日期时间相关类型的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 基础 -- MySQL 数据类型说明(数值类型字符串类型日期时间类型)SQL通用语法和SQL分类

[每日更新-MySQL基础知识]6.常用数据类型-日期时间

MySQL学习基础篇

MySQL基础知识04数据类型日期时间的格式转换

MySQL数据类型--日期时间

Mysql基础第四天,Mysql数据类型