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

    1. mysql> create table tb_article1 (
    2. -> id int unsigned not null auto_increment,
    3. -> title varchar(80),
    4. -> description varchar(255),
    5. -> addtime datetime,
    6. -> primary key(id)
    7. -> )engine=innodb default charset=utf8;
    8. Query OK, 0 rows affected (0.02 sec)
    9. mysql>
    10. mysql> create table tb_article2 (
    11. -> id int unsigned not null auto_increment,
    12. -> title varchar(80),
    13. -> description varchar(255),
    14. -> addtime timestamp,
    15. -> primary key(id)
    16. -> )engine=innodb default charset=utf8;
    17. Query OK, 0 rows affected (0.00 sec)
    18. mysql>
    19. mysql> desc tb_article1;
    20. +-------------+------------------+------+-----+---------+----------------+
    21. | Field | Type | Null | Key | Default | Extra |
    22. +-------------+------------------+------+-----+---------+----------------+
    23. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
    24. | title | varchar(80) | YES | | NULL | |
    25. | description | varchar(255) | YES | | NULL | |
    26. | addtime | datetime | YES | | NULL | |
    27. +-------------+------------------+------+-----+---------+----------------+
    28. 4 rows in set (0.00 sec)
    29. mysql> desc tb_article2;
    30. +-------------+------------------+------+-----+-------------------+-----------------------------+
    31. | Field | Type | Null | Key | Default | Extra |
    32. +-------------+------------------+------+-----+-------------------+-----------------------------+
    33. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
    34. | title | varchar(80) | YES | | NULL | |
    35. | description | varchar(255) | YES | | NULL | |
    36. | addtime | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
    37. +-------------+------------------+------+-----+-------------------+-----------------------------+
    38. 4 rows in set (0.00 sec)

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

    1. mysql> insert into tb_article1 values (null,'mysql从入门到放弃','mysql很好很强大...',null);
    2. Query OK, 1 row affected (0.00 sec)
    3. mysql> insert into tb_article2 values (null,'mysql从入门到放弃','mysql很好很强大...',null);
    4. Query OK, 1 row affected (0.00 sec)
    5. mysql>
    6. mysql> select * from tb_article1;
    7. +----+-------------------------+-------------------------+---------+
    8. | id | title | description | addtime |
    9. +----+-------------------------+-------------------------+---------+
    10. | 1 | mysql从入门到放弃 | mysql很好很强大... | NULL |
    11. +----+-------------------------+-------------------------+---------+
    12. 1 row in set (0.00 sec)
    13. mysql> select * from tb_article2;
    14. +----+-------------------------+-------------------------+---------------------+
    15. | id | title | description | addtime |
    16. +----+-------------------------+-------------------------+---------------------+
    17. | 1 | mysql从入门到放弃 | mysql很好很强大... | 2023-09-24 01:46:14 |
    18. +----+-------------------------+-------------------------+---------------------+
    19. 1 row in set (0.00 sec)

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

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

  • 相关阅读:
    Leetcode338. 比特位计数
    更改 npm的默认缓存地址
    SpringMVC之JSON返回&异常处理机制
    关于《考研数学高分公式》系列的后续及一点说明
    计算机导论第十周课后作业
    FastChat
    切水果游戏开发1
    使用 Footprint Analytics, 快速搭建区块链数据应用
    信息收集——ip信息(DNS解析、CDN)
    vector使用的注意事项
  • 原文地址:https://blog.csdn.net/chang_chunhua/article/details/133234747