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);

相关推荐
有个傻瓜8 小时前
手机号明明查不到,注册却提示“已存在“:MyBatis-Plus 逻辑删除与唯一索引冲突排错复盘与五种方案实测
mysql·mybatis-plus·故障排查·唯一索引·逻辑删除
FungLeo9 小时前
FengCMS · PHP+MySQL 升级实录 · 留言模块的六项问题:未审核进公开列表、旧回复覆盖、批量删除恢复
android·mysql·php·工程实践·fengcms
stormsha9 小时前
中文路径让 MySQL 起不来:Failed to find valid data directory 与 Illegal byte sequence
运维·数据库·mysql
stormsha9 小时前
MySQL 报 `data directory has files in it`:初始化失败一次,之后就永久起不来
运维·数据库·mysql
FungLeo10 小时前
FengCMS · PHP+MySQL 升级实录·安全的网页安装器:一次性密钥、HTTPS+CSRF、安装锁、不删表
mysql·安全·php·安装部署·fengcms
真上帝的左手12 小时前
4. 关系型数据库-MySQL-原生集成 AI能力
数据库·人工智能·mysql·ai
计算机–阿拉升学长14 小时前
基于SpringBoot的番茄种植水肥一体化管理系统 — 系统设计与实现
java·spring boot·后端·mysql·vue·javaweb
计算机毕设定制辅导-无忧学长15 小时前
《基于SpringBoot的肩周炎健康管理系统设计与实现》
java·vue.js·spring boot·mysql·肩周炎健康管理系统
梅雅达编程笔记15 小时前
开发机能跑,服务器上跑不起来:MYD 考试系统配置设计与上线清单
mysql·nginx·flask·gunicorn·毕设项目·部署上线
Escalating_xu1 天前
【MySQL】事务管理(上):从 ACID 到提交回滚,把一次转账真正做完整
android·数据库·mysql