MySQL学习笔记11

MySQL日期类型:

###㈠ 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)

bash 复制代码
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有一个默认值。

bash 复制代码
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);

相关推荐
是程序喵呀15 分钟前
MySQL备份
android·mysql·adb
指尖上跳动的旋律23 分钟前
shell脚本定义特殊字符导致执行mysql文件错误的问题
数据库·mysql
苹果醋34 小时前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
先睡5 小时前
MySQL的架构设计和设计模式
数据库·mysql·设计模式
呼啦啦啦啦啦啦啦啦7 小时前
【MySQL篇】事务的认识以及四大特性
数据库·mysql
溟洵8 小时前
Linux下学【MySQL】表中插入和查询的进阶操作(配实操图和SQL语句通俗易懂)
linux·运维·数据库·后端·sql·mysql
苹果醋314 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
别致的影分身15 小时前
使用C语言连接MySQL
数据库·mysql
过过过呀Glik15 小时前
在 Ubuntu 上安装 MySQL 的详细指南
mysql·ubuntu
Sunyanhui118 小时前
牛客网 SQL36查找后排序
数据库·sql·mysql