MySQL学习笔记11

MySQL日期类型:

MySQL学习笔记11_第1张图片

###㈠ DATE类型(年-月-日)

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

###㈡ DATETIME(年月日 小时分钟秒)

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displaysDATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

DATETIME范围相对于TIMESTAMP范围更广'1000-01-01 00:00:00' to '9999-12-31 23:59:59'

###㈢ TIMESTAMP(年月日小时分钟秒)

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

TIMESTAMP选项如果不插入时间,则MySQL会自动调用系统时间写入数据

###㈣ TIME(小时:分钟:秒)

MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or 'HHH:MM:SS' format for large hours values).TIME values may range from '-838:59:59' to '838:59:59'.

说明:小时部分可以是这么大,因为可以使用TIME类型不仅代表一个时间(必须小于24小时),而且可以表示运行时间或两个事件之间的时间间隔(可能大于24小时,甚至负数)。

==注意:==

TIME这一列如果存储缩写,需要注意mysql的解释方式。无效的时间值会被转换成'00:00:00' 。

'11:12' means '11:12:00', not '00:11:12'.

'12' and 12 are interpreted as '00:00:12'.

###㈤ YEAR

YEAR(4) and YEAR(2) differ in display format, but have the same range of values.

For 4-digit format, MySQL displays YEAR values in YYYY format, with a range of 1901 to 2155, or 0000.

For 2-digit format, MySQL displays only the last two (least significant) digits; for example, 70 (1970 or 2070) or 69 (2069).

无效的值将会被转换成'0000'.

案例:编写tb_article文章表,定义字段(id、title、description、addtime)

mysql> create table tb_article1 (
    -> id int unsigned not null auto_increment,
    -> title varchar(80),
    -> description  varchar(255),
    -> addtime datetime,
    -> primary key(id)
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> create table tb_article2 (
    -> id int unsigned not null auto_increment,
    -> title varchar(80),
    -> description  varchar(255),
    -> addtime timestamp,
    -> primary key(id)
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> desc tb_article1;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title       | varchar(80)      | YES  |     | NULL    |                |
| description | varchar(255)     | YES  |     | NULL    |                |
| addtime     | datetime         | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> desc tb_article2;
+-------------+------------------+------+-----+-------------------+-----------------------------+
| Field       | Type             | Null | Key | Default           | Extra                       |
+-------------+------------------+------+-----+-------------------+-----------------------------+
| id          | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| title       | varchar(80)      | YES  |     | NULL              |                             |
| description | varchar(255)     | YES  |     | NULL              |                             |
| addtime     | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+------------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.00 sec)

我们看到timestamp有一个默认值。

mysql> insert into tb_article1 values (null,'mysql从入门到放弃','mysql很好很强大...',null);
Query OK, 1 row affected (0.00 sec)

mysql> insert into tb_article2 values (null,'mysql从入门到放弃','mysql很好很强大...',null);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> select * from tb_article1;
+----+-------------------------+-------------------------+---------+
| id | title                   | description             | addtime |
+----+-------------------------+-------------------------+---------+
|  1 | mysql从入门到放弃       | mysql很好很强大...      | NULL    |
+----+-------------------------+-------------------------+---------+
1 row in set (0.00 sec)

mysql> select * from tb_article2;
+----+-------------------------+-------------------------+---------------------+
| id | title                   | description             | addtime             |
+----+-------------------------+-------------------------+---------------------+
|  1 | mysql从入门到放弃       | mysql很好很强大...      | 2023-09-24 01:46:14 |
+----+-------------------------+-------------------------+---------------------+
1 row in set (0.00 sec)

如果超过2038年,那么就不要使用timestamp。

入学时间:2017年,那可以使用year(4);

你可能感兴趣的:(mysql)