张梅 合肥网站建设,响应式网站设计思路,生活分类网站建设,兼职 网站 小程序 建设前言
最近在做数据迁移的时候#xff0c;使用的是云平台自带的同步工具#xff0c;在预检查阶段#xff0c;当时报错 explicit_defaults_for_timestamp 参数在目标端为 off 建议修改 on#xff0c;有什么风险呢#xff1f;在此记录下。
测试对比
MySQL 默认情况下 expl…前言
最近在做数据迁移的时候使用的是云平台自带的同步工具在预检查阶段当时报错 explicit_defaults_for_timestamp 参数在目标端为 off 建议修改 on有什么风险呢在此记录下。
测试对比
MySQL 默认情况下 explicit_defaults_for_timestamp 0 我们对比一下看看。
explicit_defaults_for_timestamp 0
在 explicit_defaults_for_timestamp 参数等于 0 的状态下如果第一个 timestamp 类型的字段如果没有设置 null 将会自动加上 not null 和默认值。如果设置了默认值则会自动加上 not null其他类型的字段是不会的。
create table test03 (a_time timestamp ,b_time timestamp null ,c_time timestamp default 2024-01-01 00:00:00,name varchar(2) default a);show create table test03; CREATE TABLE test03 (a_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,b_time timestamp NULL DEFAULT NULL,c_time timestamp NOT NULL DEFAULT 2024-01-01 00:00:00,name varchar(2) DEFAULT a
) ENGINEInnoDB DEFAULT CHARSETlatin1然后我们尝试插入一个记录
insert into test03 values (null, null, null, null);a_timeb_timec_timename2024-09-05 14:37:12NULL2024-09-05 14:37:12NULL
可以看到在 explicit_defaults_for_timestamp 等于 0 的时候不仅会影响表结构还会影响写入。虽然 timestamp 是 not null 我们写入 null 后变为了 CURRENT_TIMESTAMP。
如果此时将参数设置为 1执行相同的 SQL 语句则会报错
# 设置参数为 1
set global explicit_defaults_for_timestamp 1;# 插入相同的数据
insert into test03 values (null, null, null, null);[23000][1048] Column ‘a_time’ cannot be null explicit_defaults_for_timestamp 1
此时在 explicit_defaults_for_timestamp 等于 1 的条件下执行一个刚才的建表语句
create table test04 (a_time timestamp ,b_time timestamp null ,c_time timestamp default 2024-01-01 00:00:00,d_time timestamp not null );show create table test04; CREATE TABLE test04 (a_time timestamp NULL DEFAULT NULL,b_time timestamp NULL DEFAULT NULL,c_time timestamp NULL DEFAULT 2024-01-01 00:00:00,d_time timestamp NOT NULL
) ENGINEInnoDB DEFAULT CHARSETlatin1尝试插入数据
insert into test04 values (null, null, null, null);a_timeb_timec_timenameNULLNULLNULLNULL
从上面可以看到参数开启的情况下MySQL 默认会为 timestamp 为 NULL 的字段添加 default null 属性。而且 MySQL 没有为第一个 timestamp 设置默认值当我写入 null 后则按照 null 来存储。
另外如果 timestamp 设置为 not null 那么写入 null 时会报错如果 sql_mode 中不包含 SQL_MODE 的话则存储为 ‘0000-00-00 00:00:00’ 并抛出一个异常。
总结
生产环境 timestamp 字段一般都会设置为如下样式而且需要 timestamp 字段为 null 的业务场景很少。
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 更新时间,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 更新时间,如果生产环境要修改这个参数从 0 调整到 1 需要关注的就是 insert 语句有没有直接图省事插入 null 表示当前时间如果有这种不规范的语法那么调整该参数后可能会报错。
云平台一般不会给用户 super 账号所以在迁移过程中可能无法设置 session 级别的参数此时建议跳过该校验不修改全局级别的参数。 文章转载自: http://www.morning.xppj.cn.gov.cn.xppj.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.pbmg.cn.gov.cn.pbmg.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.hkcjx.cn.gov.cn.hkcjx.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn http://www.morning.mgmyt.cn.gov.cn.mgmyt.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.zhoer.com.gov.cn.zhoer.com http://www.morning.kbyp.cn.gov.cn.kbyp.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.mrfr.cn.gov.cn.mrfr.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.trkl.cn.gov.cn.trkl.cn http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.rhzzf.cn.gov.cn.rhzzf.cn http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.mfmbn.cn.gov.cn.mfmbn.cn http://www.morning.hytqt.cn.gov.cn.hytqt.cn http://www.morning.zqwp.cn.gov.cn.zqwp.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.dygsz.cn.gov.cn.dygsz.cn http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.zwznz.cn.gov.cn.zwznz.cn http://www.morning.nqlnd.cn.gov.cn.nqlnd.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.zwzlf.cn.gov.cn.zwzlf.cn http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.kzrg.cn.gov.cn.kzrg.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.fbxlj.cn.gov.cn.fbxlj.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.fykqh.cn.gov.cn.fykqh.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.llyjx.cn.gov.cn.llyjx.cn http://www.morning.yhljc.cn.gov.cn.yhljc.cn http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.kcrw.cn.gov.cn.kcrw.cn http://www.morning.rnqbn.cn.gov.cn.rnqbn.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.yhxhq.cn.gov.cn.yhxhq.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.wqkfm.cn.gov.cn.wqkfm.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.tcylt.cn.gov.cn.tcylt.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.wynnb.cn.gov.cn.wynnb.cn http://www.morning.ffbl.cn.gov.cn.ffbl.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.mjxgs.cn.gov.cn.mjxgs.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.elmtw.cn.gov.cn.elmtw.cn