MySQL Date and Time Types notes

Recently read "MySQL Technology Insider: SQL Programming" and took notes, which is a type of blog notes, share to facilitate their review, it can also help others

First, the space occupied by comparing datetime type

A variety of datetime data type space occupied:

Types of Footprint
DATETIME 8 bytes
DATE 3 bytes
TIMESTAMP 4 bytes
YEAR 1 byte
TIME 3 bytes

Two, DATETIME and Comparative DATE

  • DATETIME occupies 8 bytes, only the date also show the time, date range may be expressed as "1000-01-01 00:00:00" to "9999-12-31 23:59:59"
  • DATE occupies 3 bytes, only the date display, does not display the specific time, date range can be displayed as "1000-01-01" to "9999-12-31"

ok, here specifically to introduce the fractional part of the problem TIMESTAMP seconds

Note: 5.6.4+ version only supports fractional seconds, the previous version is not supported

# 查询MySQL版本
select version();
# 建表验证问题
create table t (a datetime);
# 写数据秒后面加上小数
insert into t select '2019-10-11 17:16:12.55555';
# 查询,发现并没有查出秒之后的小数
select * from t ;
# 使用microsecond,读取秒之后的小数
select microsecond('2019-10-11 17:16:12.55555') ;
# CAST读取,验证了5.7+版本查询时候会出现四舍五入,如下sql得到2019-10-11 17:16:12,而低版本就不会
SELECT CAST('2019-10-11 17:16:12.5555' AS DATETIME) ;
# CAST读取,5.7+版本查询,四舍五入得到,2019-10-11 17:16:12,低版本正常显示,具体哪个版本开始的不知道,我在5.7+版本验证都是会出现四舍五入的情况
select cast('2019-10-11 17:16:12.1234' as datetime) ;
# 5.6.4+版本支持秒的小数部分
# 支持的类型有TIME、DATETIME、TIMESTAMP,写法是type(size),size为小数部分精度,最大为6
# 删表,再验证一下
DROP TABLE t;
# 这里指定精度
CREATE TABLE t (a DATETIME(4));
# 秒后加小数,写数据
INSERT INTO t SELECT '2019-10-11 17:16:12.55555';
# 查询,发现可以正常写数据,不过精度只有4
SELECT * FROM t ;

Three, TIMESTAMP type introduced

TIMESTAMP 4 bytes, displayed range "1970-01-01" UTC to "2038-01-19 03:14:07" UTC
Note: UTC: Coordinated Universal Time, also known as the unified world time, world standard time and coordinated Universal time

note:

  • Updating the table, column type TIMESTAMP may be provided to automatically update the current time
  • As TIMESTAMP date type can set a default value, but does not support DATETIME

Example, to test, to set defaults and automatic update

# 新增一张表
CREATE TABLE t (
  a INT,
  b TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE = INNODB ;
# 写一条数据
INSERT INTO t (a) VALUES (1);
# 查询,发现自动赋默认值时间
SELECT * FROM t;

Here Insert Picture Description

Verification problem automatically update the time

# 修改字段为自动更新(数据有改变时候才会自动更新)
ALTER TABLE t MODIFY COLUMN b TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
# 删一下表数据
DELETE FROM t;
# 写初始化数据
INSERT INTO t SELECT 1,CURRENT_TIMESTAMP;
# 查询,先记录下原来时间
SELECT * FROM t;

Here Insert Picture Description


# 修改数据
UPDATE t SET a =2;
# 如果修改为1,是不会改变时间的
SELECT * FROM t;

Here Insert Picture Description

Four, YEAR and TIME type Comparative

  • YEAR type uses 1 byte to specify the display width for YEAR (2) or YEAR (. 4) is defined when introduced in the book, but in my version 5.7+ mysql authentication, or found only be defined YEAR YEAR (. 4), that is, the new version is not supported YEAR (2) of this form
    for YEAR (4), showing Year for 1901 and 2155; and more than this range, then, mysql also can write, but is assigned to 0000
  • TIME type only 3 bytes, the displayed range of "-838: 59: 59" ~ "838: 59: 59", TIME is greater than 23 hours may be a negative value may be used to save time because time interval

Fifth, the date and time functions

  • NOW, CURRENT_TIMESTAMP function and SYSDATE

MySQL introduce more commonly used NOW, CURRENT_TIMESTAMP function and SYSDATE

For example, with a sleep function, and then sleep function performed before and after comparison, these functions compare the acquired time

SELECT NOW(),CURRENT_TIMESTAMP(),SYSDATE(),SLEEP(2),NOW(),CURRENT_TIMESTAMP(),SYSDATE();

Here Insert Picture Description
Be seen from FIG contrast, NOW () is actually CURRENT_TIMESTAMP () function near Italy, in the example of the use of sleep (2), every two seconds to continue, you can compare that, NOW, CURRENT_TIMESTAMP actually get is the whole sql begin time to perform, whether before or after sleep function is executed, and SYSDATE actually get to perform this function sysdate time to time, not the whole time sql started, so get the sleep function before and after the execution time is different

  • DATE_ADD and function DATE_SUB

DATE_ADD (date, INTERVAL expr type), and DATE_SUB (datte, INTTERVAL expr type), expr can be negative, it can be used for both DATE_ADD Date addition, subtraction date may also be used. type can YEAR, MONTH, DAY, HOUR, MINUTE, SECOND

SELECT NOW()AS NOW,DATE_ADD(NOW(),INTERVAL 1 DAY) AS tomorrow,DATE_SUB(NOW(),INTERVAL 1 DAY);

Here Insert Picture Description

Note: a leap month, if it is a leap month return on the 29th, not the 28th intercalary month returns

SELECT DATE_ADD('2004-2-29',INTERVAL 1 YEAR);
SELECT DATE_ADD('2004-2-29',INTERVAL 4 YEAR);

Here Insert Picture Description
Here Insert Picture Description

  • DATE_FORMAT function
    to_char DATE_FROMAT and function in Oracle type bit, in the print function is a user-defined format data
SELECT DATE_FORMAT(NOW(),'%Y%m%d');

Guess you like

Origin www.cnblogs.com/mzq123/p/11668608.html