您好,欢迎访问一九零五行业门户网

MySQL中DATETIME、DATE和TIMESTAMP类型的区别

一、datetime 显示式:yyyy-mm-dd hh:mm:ss 时间范围:[ '1000-01-01 00:00:00'到'9999-12-31 23:59:59'] 二、date 显示式:yyyy-mm-dd 时间范围:['1000-01-01'到'9999-12-31'] 三、timestamp 显示式:yyyy-mm-dd hh:mm:ss 时间范围:[ '1970-01-01 00:00:00'
一、datetime
显示格式:yyyy-mm-dd hh:mm:ss
时间范围:[ '1000-01-01 00:00:00'到'9999-12-31 23:59:59']
二、date
显示格式:yyyy-mm-dd
时间范围:['1000-01-01'到'9999-12-31']
三、timestamp
显示格式:yyyy-mm-dd hh:mm:ss
时间范围:[ '1970-01-01 00:00:00'到'2037-12-31 23:59:59']
timestamp注意点:
timestamp default current_timestamp on update current_timestamp  在创建新记录和修改现有记录的时候都对这个数据列刷新。
timestamp default current_timestamp  在创建新记录的时候把这个字段设置为当前时间,但以后修改时,不再刷新它。
timestamp on update current_timestamp  在创建新记录的时候把这个字段设置为0,以后修改时刷新它。
timestamp default ‘yyyy-mm-dd hh:mm:ss’ on update current_timestamp  在创建新记录的时候把这个字段设置为给定值,以后修改时刷新它
1、timestamp列不为空时,默认值可以为“0000-00-00 00:00:00”,但不能为null。
2、一个表可以存在多个timestamp列,但一个表只有一个timestamp类型的字段可以在默认值或者update部分用current_timestamp,即设置为数据更新而改变为数据库系统当前值。
3、timestamp列的默认值是current_timestamp常量值。当记录数据发生变化的时候,timestamp列会自动将其值设定为current_timestamp。
4、timestamp列创建后的格式是:
alter table `course`
add column `birthday`  timestamp not null default current_timestamp on update current_timestamp;
alter table `course`
add column `birthday`  timestamp not null default '0000-00-00 00:00:00' on update current_timestamp ;
alter table `course`
add column `birthday`  timestamp null after `cname`;
四、日期格式转换
1、字符串转日期
select str_to_date('2013-01-29 13:49:18', '%y-%m-%d %h:%i:%s')
2、日期转字符串
select date_format('2013-01-29 13:49:18', '%y-%m-%d %h:%i:%s')
五、日期的中常用的年月日时分秒星期月份等获取方法
select timestamp('2013-01-29 13:50:27');
select date('2013-01-29 13:50:27');
select year('2013-01-29 13:50:27');
select month(('2013-01-29 13:50:27');
select week('2013-01-29 13:50:27');
select day('2013-01-29 13:50:27');
select time('2013-01-29 13:50:27');
select curtime();
select curdate();
select current_date;
select current_time;
select current_timestamp;
select now()
六、日期的运算:
select date_add('2013-01-29 13:50:27', interval 1 day);
                -> '2013-01-30 13:50:27'
select date_add('2013-01-29 13:50:27', interval 1 hour);
                -> '2013-01-29 14:50:27'
select date_add('2013-01-29 13:50:27', interval 1 month);
               -> '2013-02-28 13:50:27'
其它类似信息

推荐信息