• 日期容器


    YEAR
                YYYY(范围:1901/21552018
    
            DATE
                YYYY-MM-DD(范围:1000-01-01/9999-12-31)例:2018-01-01 
    
            TIME
                HH:MM:SS(范围:'-838:59:59'/'838:59:59')例:12:09:32
    
            DATETIME
    
                YYYY-MM-DD HH:MM:SS(范围:1000-01-01 00:00:00/9999-12-31 23:59:59    Y)例: 2018-01-01 12:09:32
    
            TIMESTAMP
    
                YYYYMMDD HHMMSS(范围:1970-01-01 00:00:00/2037 年某时)
    
    日期类型分类

    日期

    year:
        mysql> create table t10(born_year year); #无论year指定何种宽度,最后都默认是year(4)
        mysql> insert into t10 values  
            -> (1900),
            -> (1901),
            -> (2155),
            -> (2156);
        mysql> select * from t10;
        +-----------+
        | born_year |
        +-----------+
        |      0000 |
        |      1901 |
        |      2155 |
        |      0000 |
        +-----------+
    
    
    date,time,datetime:
        mysql> create table t11(d date,t time,dt datetime);
        mysql> desc t11;
        +-------+----------+------+-----+---------+-------+
        | Field | Type     | Null | Key | Default | Extra |
        +-------+----------+------+-----+---------+-------+
        | d     | date     | YES  |     | NULL    |       |
        | t     | time     | YES  |     | NULL    |       |
        | dt    | datetime | YES  |     | NULL    |       |
        +-------+----------+------+-----+---------+-------+
    
        mysql> insert into t11 values(now(),now(),now());
        mysql> select * from t11;
        +------------+----------+---------------------+
        | d          | t        | dt                  |
        +------------+----------+---------------------+
        | 2017-07-25 | 16:26:54 | 2017-07-25 16:26:54 |
        +------------+----------+---------------------+
    
    
    
    timestamp:
        mysql> create table t12(time timestamp);
        mysql> insert into t12 values();
        mysql> insert into t12 values(null);
        mysql> select * from t12;
        +---------------------+
        | time                |
        +---------------------+
        | 2017-07-25 16:29:17 |
        | 2017-07-25 16:30:01 |
        +---------------------+
    
    
    
    ============注意啦,注意啦,注意啦===========
        1. 单独插入时间时,需要以字符串的形式,按照对应的格式插入
        2. 插入年份时,尽量使用4位值
        3. 插入两位年份时,<=69,以20开头,比如50,  结果2050      
                        >=70,以19开头,比如71,结果1971
        mysql> create table t12(y year);
        mysql> insert into t12 values  
            -> (50),
            -> (71);
        mysql> select * from t12;
        +------+
        | y    |
        +------+
        | 2050 |
        | 1971 |
        +------+
    
    
    
    ============综合练习===========
        mysql> create table student(
            -> id int,
            -> name varchar(20),
            -> born_year year,
            -> birth date,
            -> class_time time,
            -> reg_time datetime);
    
        mysql> insert into student values
            -> (1,'sb1',"1995","1995-11-11","11:11:11","2017-11-11 11:11:11"),
            -> (2,'sb2',"1997","1997-12-12","12:12:12","2017-12-12 12:12:12"),
            -> (3,'sb3',"1998","1998-01-01","13:13:13","2017-01-01 13:13:13");  
    
        mysql> select * from student;
        +------+------+-----------+------------+------------+---------------------+
        | id   | name | born_year | birth      | class_time | reg_time            |
        +------+------+-----------+------------+------------+---------------------+
        |    1 | sb1 |      1995 | 1995-11-11 | 11:11:11   | 2017-11-11 11:11:11 |
        |    2 | sb2 |      1997 | 1997-12-12 | 12:12:12   | 2017-12-12 12:12:12 |
        |    3 | sb3 |      1998 | 1998-01-01 | 13:13:13   | 2017-01-01 13:13:13 |
        +------+------+-----------+------------+------------+---------------------+
    
    日期类型测试
    日期类型测试
    在实际应用的很多场景中,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语句中没有指定该列的更新值,则默认更新为当前时间。
    
    datetime与timestamp的区别
    

      

     1 mysql> create table t1(x datetime not null default now()); # 需要指定传入空值时默认取当前时间
     2 Query OK, 0 rows affected (0.01 sec)
     3 
     4 mysql> create table t2(x timestamp); # 无需任何设置,在传空值的情况下自动传入当前时间
     5 Query OK, 0 rows affected (0.02 sec)
     6 
     7 mysql> insert into t1 values();
     8 Query OK, 1 row affected (0.00 sec)
     9 
    10 mysql> insert into t2 values();
    11 Query OK, 1 row affected (0.00 sec)
    12 
    13 mysql> select * from t1;
    14 +---------------------+
    15 | x                   |
    16 +---------------------+
    17 | 2018-07-07 01:26:14 |
    18 +---------------------+
    19 row in set (0.00 sec)
    20 
    21 mysql> select * from t2;
    22 +---------------------+
    23 | x                   |
    24 +---------------------+
    25 | 2018-07-07 01:26:17 |
    26 +---------------------+
    27 row in set (0.00 sec)
    28 
    29 对上面datetime与timestamp的区别中第四条的验证
    V
    对上面datetime与timestamp的区别中第四条的验证
  • 相关阅读:
    自闭的D7
    D2
    Codeforces Round #531 (Div. 3)
    hello 2019 D
    牛客练习赛36B
    cf954H
    gym102007 E
    Gym 101972
    Gym 101810
    试题 历届试题 青蛙跳杯子(bfs)
  • 原文地址:https://www.cnblogs.com/strawberry-1/p/11436141.html
Copyright © 2020-2023  润新知