当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL--时间戳属性2

MySQL--时间戳属性2

2019年03月04日  | 移动技术网IT编程  | 我要评论

在mysql 5.6版本中引入参数explicit_defaults_for_timestamp设置,该参数会影响timestamp的默认属性。

同时在mysql 5.6版本中中,去除一张表只能有一个timestamp列的限制,允许单表中使用多个时间戳列。

===============================================================================

在mysql 5.6中,当参数explicit_defaults_for_timestamp=off时:

1、timestamp列如果没有明确指定为nll,则默认为not null
2、timestamp列如果明确指定为null,则会增加默认值null
3、表中第一个timestamp列,如果没有声明为null属性、default或者 on update,会自动分配 default current_timestamp和on update current_timestamp 属性。
4、表中第二个timestamp列,如果没有声明为null或者default子句,默认自动分配’0000-00-00 00:00:00′。插入行时没有指明改列的值,该列默认分配’0000-00-00 00:00:00′,且没有警告。

如使用下面脚本创建:
create table tb2004(
id int primary key,
c1 timestamp,
c2 timestamp,
c3 timestamp);
然后show create table tb2004会发现创建脚本为:
create table `tb2004` (
  `id` int(11) not null,
  `c1` timestamp not null default current_timestamp on update current_timestamp,
  `c2` timestamp not null default '0000-00-00 00:00:00',
  `c3` timestamp not null default '0000-00-00 00:00:00',
  primary key (`id`)
) engine=innodb default charset=utf8

 

===============================================================================

在mysql 5.6中,当参数explicit_defaults_for_timestamp=on时:

1、timestamp列如果没有明确指定为not nll则默认为null
2、表中timestamp列声明为not null时,则不会为列自动创建默认值,且该列不能显式插入null值。
3、表中第一个timestamp列不会自动分配 default current_timestamp和on update current_timestamp 属性,需要显式声明。
4、表中第二各timestamp列与第一个timestamp列不会存在特殊设置。

如使用下面脚本创建:
create table tb2004(
id int primary key,
c1 timestamp,
c2 timestamp,
c3 timestamp);
然后show create table tb2004会发现创建脚本为:
create table `tb2004` (
  `id` int(11) not null,
  `c1` timestamp null default null,
  `c2` timestamp null default null,
  `c3` timestamp null default null,
  primary key (`id`)
) engine=innodb default charset=utf8

而如使用下面脚本创建:
create table tb2005(
id int primary key,
c1 timestamp not null,
c2 timestamp not null,
c3 timestamp not null);
然后show create table tb2005会发现创建脚本为:
create table `tb2005` (
  `id` int(11) not null,
  `c1` timestamp not null,
  `c2` timestamp not null,
  `c3` timestamp not null,
  primary key (`id`)
) engine=innodb default charset=utf8

 

=================================================================

在mysql5.7.8及之后版本中,参数sql_mode的默认值为:

only_full_group_by 
strict_trans_tables 
no_zero_in_date 
no_zero_date 
error_for_division_by_zero 
no_auto_create_user 
no_engine_substitution

 

其中no_zero_in_date和no_zero_date会严格限制时间字段和时间戳字段的值:

no_zero_in_date:要求年月日都为0或月日不为0
no_zero_date:要求年月日中任一项不为0

当sql_mode中同时包含no_zero_in_date和no_zero_date时,则不允许向时间字段和时间戳字段插入任何非法日期的值,即不允许插入'0000-00-00 00:00:00'的值。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网