• 数据库:表操作-数据类型(日期类型)


    日期类型

    DATE TIME DATETIME TIMESTAMP YEAR

    作用:存储用户注册时间,文章发布时间,员工入职时间,出生时间,过期时间等

    YEAR
                YYYY(1901/2155)
    
            DATE
                YYYY-MM-DD(1000-01-01/9999-12-31)
    
            TIME
                HH:MM:SS('-838:59:59'/'838:59:59')
    
            DATETIME
    
                YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59    Y)
    
            TIMESTAMP
    
                YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某时)
    

      验证

    mysql> create table t10(born_year year);
    Query OK, 0 rows affected (0.08 sec)
    
    mysql> insert into t10 values
        -> (1900),
        -> (1901),
        -> (2155),
        -> (2156);
    Query OK, 4 rows affected, 2 warnings (0.07 sec)
    Records: 4  Duplicates: 0  Warnings: 2
    
    mysql> select * from t10;
    +-----------+
    | born_year |
    +-----------+
    |      0000 |
    |      1901 |
    |      2155 |
    |      0000 |
    +-----------+
    4 rows in set (0.03 sec)
    

      

    ============date,time,datetime===========
    
    mysql> create table t11(d date,t time,dt datetime);
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> desc t11;
    +-------+----------+------+-----+---------+-------+
    | Field | Type     | Null | Key | Default | Extra |
    +-------+----------+------+-----+---------+-------+
    | d     | date     | YES  |     | NULL    |       |
    | t     | time     | YES  |     | NULL    |       |
    | dt    | datetime | YES  |     | NULL    |       |
    +-------+----------+------+-----+---------+-------+
    3 rows in set (0.04 sec)
    
    mysql> insert into t11 values(now(),now(),now());
    Query OK, 1 row affected, 1 warning (0.10 sec)
    
    mysql> select * from t11;
    +------------+----------+---------------------+
    | d          | t        | dt                  |
    +------------+----------+---------------------+
    | 2016-11-16 | 15:00:51 | 2016-11-16 15:00:51 |
    +------------+----------+---------------------+
    1 row in set (0.02 sec)
    
    
    ============timestamp===========
    mysql> create table t12(time timestamp);
    Query OK, 0 rows affected (0.12 sec)
    
    mysql> insert into t12 values();
    Query OK, 1 row affected (0.03 sec)
    
    mysql> insert into t12 values(null);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from t12;
    +---------------------+
    | time                |
    +---------------------+
    | 2016-11-16 15:03:07 |
    | 2016-11-16 15:03:28 |
    +---------------------+
    2 rows in set (0.00 sec)
    

      

    ============注意啦,注意啦,注意啦===========
    1. 单独插入时间时,需要以字符串的形式,按照对应的格式插入
    2. 插入年份时,尽量使用4位值
    3. 插入两位年份时,<=69,以20开头,比如50,结果2050;>=70,以19开头,比如71,结果1971
    mysql> create table t13(y year);
    Query OK, 0 rows affected (0.10 sec)
    
    
    mysql> insert into t13 values
        -> (50),
        -> (70);
    Query OK, 2 rows affected (0.04 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> select * from t13;
    +------+
    | y    |
    +------+
    | 2050 |
    | 1970 |
    +------+
    2 rows in set (0.00 sec)
    

      

    ============综合练习===========
    mysql> create table student1(
        -> id int,
        -> name varchar(10),
        -> born_year year,
        -> birth date,
        -> class_time time,
        -> reg_time datetime);
    Query OK, 0 rows affected (0.04 sec)
    
    mysql> insert into student1 values
        -> (1,'alex',"1995","1995-11-11","11:11:11","2017-11-11 11:11:11"),
        -> (2,'egon',"1997","1997-12-12","12:12:12","2017-12-12 12:12:12"),
        -> (3,'wsb',"1998","1998-01-01","13:13:13","2017-01-01 13:13:13");
    Query OK, 3 rows affected (0.04 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from student1;
    +------+------+-----------+------------+------------+---------------------+
    | id   | name | born_year | birth      | class_time | reg_time            |
    +------+------+-----------+------------+------------+---------------------+
    |    1 | alex |      1995 | 1995-11-11 | 11:11:11   | 2017-11-11 11:11:11 |
    |    2 | egon |      1997 | 1997-12-12 | 12:12:12   | 2017-12-12 12:12:12 |
    |    3 | wsb  |      1998 | 1998-01-01 | 13:13:13   | 2017-01-01 13:13:13 |
    +------+------+-----------+------------+------------+---------------------+
    3 rows in set (0.00 sec)
    

      datetime与timestamp的区别

    在实际应用的很多场景中,MySQL的这两种日期类型都能够满足我们的需要,存储精度都为秒,但在某些情况下,会展现出他们各自的优劣。
    下面就来总结一下两种日期类型的区别。
    
    1.DATETIME的日期范围是1001——9999年,TIMESTAMP的时间范围是1970——2038年。
    
    2.DATETIME存储时间与时区无关,TIMESTAMP存储时间与时区有关,显示的值也依赖于时区。在mysql服务器,
    操作系统以及客户端连接都有时区的设置。
    
    3.DATETIME使用8字节的存储空间,TIMESTAMP的存储空间为4字节。因此,TIMESTAMP比DATETIME的空间利用率更高。
    
    4.DATETIME的默认值为null;TIMESTAMP的字段默认不为空(not null),默认值为当前时间(CURRENT_TIMESTAMP),
    如果不做特殊处理,并且update语句中没有指定该列的更新值,则默认更新为当前时间。
    

      

     
  • 相关阅读:
    今天的温度还是有点高.....
    [React] 点击---图片90&#176;旋转
    javascript onclick事件可以调用两个方法吗?
    vue 页面回退mounted函数不执行的问题及解决方法
    vue static和assets的区别
    js实现复制|剪切指定内容到粘贴板--clipboard
    纯前端html导出pdf--分页+不分页--html2canvas+jsPDF
    git常用命令行
    浅谈“观察者模式”那点小事儿
    [Linq] ORM
  • 原文地址:https://www.cnblogs.com/mike-liu/p/9320831.html
Copyright © 2020-2023  润新知