MySQL 列类型-日期时间型

来源:互联网 发布:sql查询字段最大长度 编辑:程序博客网 时间:2024/04/28 19:24

年月日时分秒

Datetime 8字节)

表示的时间范围:

1000~9999

支持不使用分隔表示。

(原因:根据4-2-2-2-2-2来进行判断日期是否合法)

歧义:

20:12:19 表示 2020.12.19 具体时分秒没有设置。

但是不易于程序员进行修改,故还是使用规定的分隔符较好。

89:12:19 表示1989.12.19 因为(1970-2069);也不建议使用。

上述此时,时分秒都为000000;支持0值,表示时间没有规定。

 

 

时间戳

Timestamp4字节)

注意:

储存时时整型,但显示时,是时间类型。

表示的时间范围:

1970.1.1 0:0:0~2038.01.19 03:14:07

在进行查询时,字段Timestamp的变量+0显示,可以看到另一种表示形式。

 

 

年月日

Date3字节)

取值同Datetime.

 

时分秒

Time3字节)

取值范围:

-838:59:59~838:59:59

意义:

1.表示一天中的时间。

2.表示时间间隔。(有上限,35天内)

格式:

Insert into (` HH:MM:SS`

Insert into (`D HH:MM:SS`)

 

 

Year(1字节)

取值范围

1901~2155

 

可以根据所做项目来使用相应的时间戳,而不是使用MySQL中的时间戳。

例如:

做一个php项目,通常保存时间的都是时间戳,(UNIX时间戳)

直接以整型的形式保存在数据库内,不使用mysql中的timestamp;


Create database date;Use date;Create table tbl_date(A datetime,B timestamp,C date,D time,E year);Insert into tbl_date (A) values ('1995:11:04 15:25:52'); /*分隔符表示.*/select * from tbl_date;   /*时间插入成功,同时时间戳自动生成了编译时的当前时间.*/Insert into tbl_date (A) values ('19951101152552');    /*无分隔符表示*/select * from tbl_date;   /*同上*/Insert into tbl_date (A) values ('1995:11:04');    /*只是输入年月日*/select * from tbl_date;   /*时分秒均为0,表示无规定时间*/Insert into tbl_date (B) values ('1995:11:04 15:25:52');    /*手动输入时间戳*/select * from tbl_date;   Insert into tbl_date (B) values ('19951101152552');    /*无分隔符表示*/select * from tbl_date;   /*同上*/Insert into tbl_date (B) values ('2038:24:04 15:25:52');    /*超出时间戳表示时间范围*/Select (b+0) from tbl_date;    /*时间戳的另一种表示形式,整型数表示时间戳*/Insert into tbl_date (C) values('1995:11:04');      /*输入年月日*/select * from tbl_date;Insert into tbl_date (D) values('19:11:04');      /*输入时分秒*/select * from tbl_date;Insert into tbl_date (D) values('5 19:11:04');      /*输入天数+时分秒*/select * from tbl_date;                       /*X天后的经历的时间间隔*/Insert into tbl_date (E) values('1901');      /*输入年份*/select * from tbl_date;                       Insert into tbl_date (E) values('2155');      /*输入年份*/select * from tbl_date;  Insert into tbl_date (E) values('2156');      /*超过年的取值范围*/

 

0 0
原创粉丝点击