{"id":336,"date":"2007-01-16T20:49:45","date_gmt":"2007-01-16T11:49:45","guid":{"rendered":"https:\/\/regex.info\/blog\/2007-01-16\/336"},"modified":"2007-01-16T20:49:45","modified_gmt":"2007-01-16T11:49:45","slug":"wishing-for-an-in-place-replace-in-mysql","status":"publish","type":"post","link":"https:\/\/regex.info\/blog\/2007-01-16\/336","title":{"rendered":"Wishing for an in-place REPLACE in MySQL"},"content":{"rendered":"\n<p>( If you're not familiar with <a href=\"http:\/\/www.mysql.com\">MySQL<\/a>, this post will be of no interest. )<\/p>\n\n<p>\n\nI like that MySQL has its <a class='quiet' style='font-variant: small-caps'\nhref=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/replace.html\">replace<\/a>\ncommand, which acts like an <a class='quiet' style='font-variant:\nsmall-caps'\nhref=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/insert.html\">insert<\/a> except\nthat if the newly-inserted data would cause <span class='nobr'>a conflict<\/span> with one or more <a\nclass='quiet' style='font-variant: small-caps'\nhref=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/constraint-primary-key.html\">unique<\/a>\nkeys, one or more pre-existing rows are automatically <a class='quiet'\nstyle='font-variant: small-caps'\nhref=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/delete.html\">delete<\/a>ed to\nmake way for the new data.\n\n<\/p>\n   \n<p>\n\nHowever, it seems that most times that <span class='nobr'>I want<\/span> to use <span\nstyle='font-variant:small-caps'>replace<\/span>, I <b>really<\/b> want\nsemantics along the lines of &#8220;<a\nhref=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/update.html\" class='quiet'\nstyle='font-variant: small-caps'>update<\/a> if already there; <span\nstyle='font-variant:small-caps'>insert<\/span> if not.&#8221; The only\ndifference is in the value given to unmentioned fields: in normal <span\nstyle='font-variant:small-caps'>replace<\/span> semantics, unmentioned\nfields get the default value for the field (the one created, explicitly or\nimplicitly, at the time of table creation). What <span class='nobr'>I want<\/span> is that fields that\n<span class='nobr'>I don't<\/span> give <span class='nobr'>a value<\/span> for on the <span\nstyle='font-variant:small-caps'>replace<\/span> line to get the value that\nhad been there before (if the record was already there, and if not, then\nfall back to the default for that field).\n\n<\/p><p>\n\nThis seems a totally natural (and useful!) extension, so it's beyond me why\nit's not there. <span class='nobr'>I looked<\/span> around the docs, but couldn't find it.\n\n<\/p><p>\n\nAs for the syntax, <span class='nobr'>I can imagine<\/span> either an <span\nstyle='font-variant:small-caps'>in place<\/span> option to <span\nstyle='font-variant:small-caps'>replace<\/span>, or an <span\nstyle='font-variant:small-caps'>or insert<\/span> option to <span\nstyle='font-variant:small-caps'>update<\/span>.\n\n<\/p><p>\n\nWhat do you think, Monty?\n\n<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>( If you're not familiar with MySQL, this post will be of no interest. )<\/p> <p> I like that MySQL has its replace command, which acts like an insert except that if the newly-inserted data would cause a conflict with one or more unique keys, one or more pre-existing rows are automatically deleteed to make way for the new data. <\/p> <p> However, it seems that most times that I want to use replace, I <b>really<\/b> want semantics along the lines of \"update if already there; insert if not.\" The only difference is in the value given to unmentioned fields: [...]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/posts\/336"}],"collection":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":0,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"wp:attachment":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}