MySQL时间戳字段默认CURRENT_TIMESTAMP不适用于现有行

时间:2018-04-03 09:54:42

标签: mysql timestamp

在服务器中,我有MySQL版本:

  

61年1月5日

它有一个名为test的表,包含10列和10K行。 现在我决定添加一个新列

  

ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP on UPDATE CURRENT_TIMESTAMP;

添加了新列ts。但问题是对于现有行,这个新列(ts)的值是00:000:000不是当前时间。

PS:当我添加新行或更新任何现有行时,ts将使用当前时间戳更新。

  

为什么默认的current_timestamp / now()不适用于现有行?

编辑: 我可以运行一个简单的更新SQL来将现有行更新为当前时间。但我是数据库中的新手,我正在尝试知道是否可以使用默认值更新现有行。

1 个答案:

答案 0 :(得分:2)

MySQL表中的UPDATE yourTable SET ts = CURRENT_TIMESTAMP; 子句仅涉及将 new 记录添加到表中时发生的情况。没有遗留行为,DBMS返回到现有记录并应用某些默认值。在这种情况下,如果您希望现有记录包含当前时间戳,则可以进行全面更新:

ts

在此之后,当您添加新记录但未指定CURRENT_TIMESTAMP列的值时,系统会为其分配let data = [ { "configData":[ { "repoSettings":[ { "BaseURL":"45df16eb-55f9-48a9-9df8-428984524d7b", "CurrentBranch":"Publish_1522323990", "CurrentHeadSHA":"85c599a6ab3ecf8bfb469479da", "RepositoryId":1528, "RepositoryName":"45df16eb-55f94524d7b" } ] }, { "pageSettings":[ { "PageCss":[ ], "PageExternalCss":[ ], "PageExternalJs":[ ], "PageLayout":"default", "PageMetaInfo":[ ], "PageMetacharset":"UTF-8", "PageName":"index.html", "PageSEODescription":"", "PageSEOKeywords":"", "PageSEOTitle":"", "PageScripts":[ ], "PageStyles":[ ], "partials":[ { "Header":"default" }, { "Footer":"default" } ] }, { "PageCss":[ ], "PageExternalCss":[ ], "PageExternalJs":[ ], "PageLayout":"default", "PageMetaInfo":[ ], "PageMetacharset":"UTF-8", "PageName":"about.html", "PageSEODescription":"", "PageSEOKeywords":"", "PageSEOTitle":"", "PageScripts":[ ], "PageStyles":[ ], "partials":[ { "Header":"default" }, { "Footer":"default" }, { "Sidebar":"default" } ] }, { "PageCss":[ ], "PageExternalCss":[ ], "PageExternalJs":[ ], "PageLayout":"default", "PageMetaInfo":[ ], "PageMetacharset":"UTF-8", "PageName":"about.html", "PageSEODescription":"", "PageSEOKeywords":"", "PageSEOTitle":"", "PageScripts":[ ], "PageStyles":[ ], "partials":[ { "Header":"default" }, { "Footer":"default" }, { "Sidebar":"default" }, { "MenuBar":"default" } ] }, { "PageCss":[ ], "PageExternalCss":[ ], "PageExternalJs":[ ], "PageLayout":"default", "PageMetaInfo":[ ], "PageMetacharset":"UTF-8", "PageName":"about.html", "PageSEODescription":"", "PageSEOKeywords":"", "PageSEOTitle":"", "PageScripts":[ ], "PageStyles":[ ], "partials":[ { "Sidebar":"default" }, { "MenuBar":"default" } ] } ] } ] } ] function find(key, value) { let matchingIndex = []; data[0].configData[1].pageSettings.forEach((i, settingIndex) => { i.partials.forEach(j => { let matching = Object.keys(j).filter(k => k === key && j[k] === value); if(matching && matching.length) { matchingIndex.push(settingIndex); }; }) }) console.log('Given values ('+key+','+value+') are found in setting index: '+ matchingIndex); } find('Header','default'); find('Sidebar','default')