{"id":1750,"date":"2014-03-31T00:00:00","date_gmt":"2014-03-30T22:00:00","guid":{"rendered":"https:\/\/wwwneu.strehle.de\/tim\/weblog\/archives\/2014\/03\/31\/1707\/"},"modified":"2014-03-31T00:00:00","modified_gmt":"2014-03-30T22:00:00","slug":"1707","status":"publish","type":"post","link":"https:\/\/www.strehle.de\/tim\/weblog\/archives\/2014\/03\/31\/1707\/","title":{"rendered":"Trailing spaces in SQL \u2013 NO PAD vs PADSPACE in MySQL and Oracle"},"content":{"rendered":"<p>Last week, a customer reported a problem with DC-X \u2013 some linked metadata seemed broken. It turned out that slightly buggy custom code had written DCX_PUBINFO.PUB_DOC_ID = &#8218;doc123 &#8218; (note the trailing space) into the MySQL database, while the referenced column DCX_DOCUMENT.DOC_ID contained &#8218;doc123&#8216; (without the space).<\/p>\n<p>This came as a surprise to us: We didn\u2019t expect InnoDB\u2019s referential integrity to allow different values in a foreign key relation. But experiments showed that MySQL in fact ignores appended spaces (rtrim) when comparing values with \u201c=\u201d!<\/p>\n<p>Here\u2019s a test case if you\u2019d like to reproduce it:<\/p>\n<p>create table T (V varchar(255) not null); insert into T (V) values (&#8218;a&#8216;); select * from T where V = &#8218;a &#8218;;<\/p>\n<p>On MySQL, the SELECT statement returns the row we just inserted. On Oracle, it doesn\u2019t \u2013 which seems to make a lot more sense.<\/p>\n<p>The first Stack Overflow post I found, <a href=\"http:\/\/stackoverflow.com\/questions\/14703349\/mysql-disable-auto-trim\">MySQL disable Auto-Trim<\/a>, suggested that this was somehow acceptable, SQL-standardized behaviour. Weird. The SQL 92 standard seems to recommend MySQL\u2019s padding \/ trimming (PADSPACE) and describes a NO PAD opt-out (that MySQL doesn\u2019t offer).<\/p>\n<p>Another post, <a href=\"http:\/\/stackoverflow.com\/questions\/12135075\/mysql-treatment-of-of\">MySQL treatment of &#8218; &#8218;<\/a>, was more informative \u2013 apparently LIKE behaves differently:<\/p>\n<p>select * from T where V like &#8218;a &#8218;;<\/p>\n<p>And MySQL has a \u201cbinary\u201d workaround for SELECT with \u201c=\u201d:<\/p>\n<p>select * from T where binary V = &#8218;a &#8218;;<\/p>\n<p>For the full background, and a comparison of different RDBMS, read the PostgreSQL discussion <a href=\"http:\/\/postgresql.1045698.n5.nabble.com\/String-comparison-and-the-SQL-standard-td5740721.html\">String comparison and the SQL standard<\/a>. According to this, MySQL and SQL Server always ignore appended spaces as described above. Oracle and PostgreSQL, on the other hand, do what we\u2019d expect the database to do and don\u2019t ignore them \u2013 as long as you use VARCHAR not CHAR.<\/p>\n<p>We\u2019re learning something new every day\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week, a customer reported a problem with DC-X \u2013 some linked metadata seemed broken. It turned out that slightly buggy custom code had written DCX_PUBINFO.PUB_DOC_ID = &#8218;doc123 &#8218; (note the trailing space) into the MySQL database, while the referenced column DCX_DOCUMENT.DOC_ID contained &#8218;doc123&#8216; (without the space). This came as a surprise to us: We [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_share_on_mastodon":"0"},"categories":[1],"tags":[],"class_list":["post-1750","post","type-post","status-publish","format-standard","hentry","category-weblog"],"share_on_mastodon":{"url":"","error":""},"_links":{"self":[{"href":"https:\/\/www.strehle.de\/tim\/wp-json\/wp\/v2\/posts\/1750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strehle.de\/tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strehle.de\/tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strehle.de\/tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strehle.de\/tim\/wp-json\/wp\/v2\/comments?post=1750"}],"version-history":[{"count":0,"href":"https:\/\/www.strehle.de\/tim\/wp-json\/wp\/v2\/posts\/1750\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strehle.de\/tim\/wp-json\/wp\/v2\/media?parent=1750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strehle.de\/tim\/wp-json\/wp\/v2\/categories?post=1750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strehle.de\/tim\/wp-json\/wp\/v2\/tags?post=1750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}