MySQL中的数据类型-----日期时间


-- HH:MM:SS [D HH:MM:SS] D表示天数 0~34
-- 测试TIME类型
CREATE TABLE test_time(
a TIME
);
INSERT test_time(a) VALUES('12:23:45');
INSERT test_time(a) VALUES('2 12:23:45');
INSERT test_time(a) VALUES('22:22');
INSERT test_time(a) VALUES('22');
INSERT test_time(a) VALUES('2 22');
-- HHMMSS
INSERT test_time(a) VALUES('121212');
INSERT test_time(a) VALUES('0');
INSERT test_time(a) VALUES(0);
INSERT test_time(a) VALUES('787878');

INSERT test_time(a) VALUES(NOW());
INSERT test_time(a) VALUES(CURRENT_TIME);

-- 测试DATE类型 YYYY-MM-DD YYYYMMDD
CREATE TABLE test_date(
    a DATE
);
INSERT test_date(a) VALUES('2017-03-04');
INSERT test_date(a) VALUES('2017-2-13');
INSERT test_date(a) VALUES('4007-03-23');
INSERT test_date(a) VALUES('40071212');
INSERT test_date(a) VALUES('4007@12@12');
INSERT test_date(a) VALUES('4008#13#13');
INSERT test_date(a) VALUES('4009.8.14');


-- YY-MM-DD YYMMDD
-- 70~99 转换成1970~1999 00~69 2000~2069
INSERT test_date(a) VALUES('780902');
INSERT test_date(a) VALUES('650902');
INSERT test_date(a) VALUES('880902');
INSERT test_date(a) VALUES(NOW());
INSERT test_date(a) VALUES(CURRENT_DATE);

-- 测试DATETIME

CREATE TABLE test_datetime(
a DATETIME
);
INSERT test_datetime(a) VALUES('1004-09-12 13:24:56');
INSERT test_datetime(a) VALUES('720305121212');
INSERT test_datetime(a) VALUES(NOW());
INSERT test_datetime(a) VALUES(DATETIME);

-- 测试TIMESTAMP
CREATE TABLE test_timestamp(
    a TIMESTAMP
);
INSERT test_timestamp(a) VALUES('1978-10-23 12:12:12');
INSERT test_timestamp(a) VALUES('1999/10/01 00:00:00'); --1999/10/01: 时间, 日期的分隔符任意


mysql> INSERT test_timestamp(a) VALUES('1978-10-23 12:12:12');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM test_timestamp;
+---------------------+
| a                   |
+---------------------+
| 1999-10-01 00:00:00 |
| 1978-10-23 12:12:12 |
+---------------------+
2 rows in set (0.00 sec)
 


-- 插入CURRENT_TIMESTAMP
INSERT test_timestamp VALUES(CURRENT_TIMESTAMP);

-- 插入NULL
INSERT test_timestamp VALUES(NULL);

-- 什么也不写 得到当前系统日期和时间
INSERT test_timestamp VALUES();

mysql> SELECT * FROM test_timestamp;
+---------------------+
| a                   |
+---------------------+
| 1999-10-01 00:00:00 |
| 1978-10-23 12:12:12 |
| 2019-09-20 11:09:19 |<--INSERT test_timestamp VALUES(CURRENT_TIMESTAMP);
| 2019-09-20 11:10:15 |<--INSERT test_timestamp VALUES(NULL);
| 2019-09-20 11:12:06 |<--INSERT test_timestamp VALUES();
+---------------------+
5 rows in set (0.00 sec)


-- 测试YEAR

CREATE TABLE test_year(
    a YEAR
);

INSERT test_year(a) VALUES(1901);

-- 00~69 2000~2069 70~99 1970~1999
-- 0 插入的结果是0000
-- '0' 插入的结果是2000

--HH:MM:SS [D HH:MM:SS] D: 表示天数
--测试TIME类型

mysql> CREATE TABLE test_time(a TIME);

mysql> DESC test_time;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| a     | time | YES  |     | NULL    |       |
+-------+------+------+-----+---------+-------+
1 row in set (0.01 sec)
mysql> INSERT test_time(a) VALUES('10:26:28');

mysql> INSERT test_time(a) VALUES('10:26:28');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM test_time;
+----------+
| a        |
+----------+
| 10:26:28 |
+----------+
1 row in set (0.00 sec)

mysql> INSERT test_time(a) VALUES('2 10:26:28');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM test_time;
+----------+
| a        |
+----------+
| 10:26:28 |
| 58:26:28 |-----<<<<-------VALUES('2 10:26:28')  <=> 2*24+10(小时):26(分):28(秒)
+----------+
2 rows in set (0.00 sec)

--在啊VALUES('22')中只写入一个22
mysql> INSERT test_time(a) VALUES('22');
Query OK, 1 row affected (0.02 sec)

mysql> SELECT * FROM test_time;
+----------+
| a        |
+----------+
| 10:26:28 |
| 58:26:28 |
| 00:00:22 |-----<----------只写入一个22的情况
+----------+
3 rows in set (0.00 sec)


mysql> INSERT test_time(a) VALUES(NOW()); --VALUES(NOW()) 插入系统的当前时间
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> SELECT * FROM test_time;
+----------+
| a        |
+----------+
| 10:26:28 |
| 58:26:28 |
| 00:00:22 |
| 10:37:20 |--<<--NOW()
+----------+
4 rows in set (0.00 sec)

--测试DATE类型 YYYY-MM-DD  <=> YYYYMMDD

CREATE TABLE test_date(a DATE);
INSERT test_date(a) VALUES(NOW());

mysql> CREATE TABLE test_date(a DATE);
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT test_date(a) VALUES(NOW());
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> SELECT * FROM test_date;
+------------+
| a          |
+------------+
| 2019-09-20 |
+------------+
1 row in set (0.00 sec)

CREATE TABLE test_date_and_time(d DATE,  t TIME);
INSERT test_date_and_time(d, t) VALUES(NOW(), NOW());


 CREATE TABLE test_date_and_time(d DATE,  t TIME);
Query OK, 0 rows affected (0.01 sec)

mysql> DESC test_date_and_time;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| d     | date | YES  |     | NULL    |       |
| t     | time | YES  |     | NULL    |       |
+-------+------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> INSERT test_date_and_time(d, t) VALUES(NOW(), NOW());
Query OK, 1 row affected, 2 warnings (0.01 sec)

mysql> select * from test_date_and_time;
+------------+----------+
| d          | t        |
+------------+----------+
| 2019-09-20 | 10:46:42 |
+------------+----------+
1 row in set (0.00 sec)

优质内容筛选与推荐>>
1、03-箭头函数
2、通过MES技术居然可以防止制造数据造假?
3、ER图
4、毕业设计总结(二) 系统设计阶段
5、PIC学习5定时器


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn