{"id":11697,"date":"2023-10-27T19:16:29","date_gmt":"2023-10-28T02:16:29","guid":{"rendered":"https:\/\/www.mysqltutorial.org\/?page_id=11697"},"modified":"2024-01-29T20:28:16","modified_gmt":"2024-01-30T03:28:16","slug":"mysql-binary","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/","title":{"rendered":"MySQL BINARY Data Type"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about MySQL <code>BINARY<\/code> data type and how to use it to store fixed-length binary data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL BINARY data type<\/h2>\n\n\n\n<p>The <code>BINARY<\/code> data type is used to store fixed-length binary data. For example, you can use <code>BINARY<\/code> data type for columns that store hashes and checksums such as <code>SHA-256<\/code> because these values have a fixed length.<\/p>\n\n\n\n<p>To declare a column that uses the <code>BINARY<\/code> data type, you specify the maximum length of binary data it can hold:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">column_name BINARY(size);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the <code>size<\/code> specifies the number of bytes that the column name can store. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Right-padding with 0x00 (zero bytes)<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">When you insert a binary value<\/a> whose length is less than the specified length for the <code>BINARY<\/code> column, MySQL will automatically pad the value with zero bytes (0x00) on the right side to reach the defined <code>size<\/code> length.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">No trailing byte removal for retrievals<\/h3>\n\n\n\n<p>When you retrieve a value from the <code>BINARY<\/code> column, MySQL does not remove any trailing zero bytes that were padded during insertion. <\/p>\n\n\n\n<p>In other words, if you inserted a binary value and it was right-padded with zero bytes, those zero bytes will be present when you retrieve the data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">All bytes are significant in comparisons<\/h3>\n\n\n\n<p>When comparing <code>BINARY<\/code> value in the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/\">WHERE<\/a><\/code> clause, <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\">ORDER<\/a><\/code> clause, or <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-distinct\/\">DISTINCT<\/a><\/code>, MySQL considers all bytes. <\/p>\n\n\n\n<p>This means that even trailing zero bytes can affect the comparison results, and MySQL will not consider two <code>BINARY<\/code> values are equal unless all of their bytes match.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">0x00 and space differ in comparisons<\/h3>\n\n\n\n<p>MySQL treats the zero bytes (0x00) and the space character (0x20) differently in comparisons. <\/p>\n\n\n\n<p>If you have a <code>BINARY<\/code> column with values that contain zero bytes and space characters, MySQL will not consider these values to be equal. <\/p>\n\n\n\n<p>Additionally, MySQL places null bytes before space characters in sorting operations (e.g., <code>ORDER<\/code> <code>BY<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL BINARY data type example<\/h2>\n\n\n\n<p>We&#8217;ll take an example of using the <code>BINARY<\/code> data type to store <code>SHA<\/code>-256 hashes.<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a table<\/a> to store the <code>SHA-256<\/code> hashes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> binary_demo(\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    <span class=\"hljs-keyword\">data<\/span> <span class=\"hljs-built_in\">BINARY<\/span>(<span class=\"hljs-number\">32<\/span>) <span class=\"hljs-comment\">-- 32 bytes for SHA-256<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>binary_demo<\/code> has two columns:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>id: An <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-auto_increment\/\">auto-incremented<\/a> <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-primary-key\/\">primary key<\/a> column.<\/li>\n\n\n\n<li>data: A <code>BINARY<\/code> column with a fixed size of 32 bytes to store <code>SHA-256<\/code> hashes.<\/li>\n<\/ul>\n\n\n\n<p>Second, insert a <code>SHA-256<\/code> hash into the table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> binary_demo(<span class=\"hljs-keyword\">data<\/span>) \n<span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-keyword\">UNHEX<\/span>(<span class=\"hljs-keyword\">SHA2<\/span>(<span class=\"hljs-string\">'Hello'<\/span>, <span class=\"hljs-number\">256<\/span>)));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>SHA2('Hello', 256)<\/code> computes the <code>SHA<\/code>-256 hash of the string &#8216;Hello&#8217;. <\/p>\n\n\n\n<p>The <code>UNHEX()<\/code> function converts the hexadecimal representation of the <code>SHA<\/code>-256 hash into binary data before inserting it into the <code>BINARY<\/code> column.<\/p>\n\n\n\n<p>Third, retrieve data from the <code>BINARY<\/code> column and convert the data back to its hexadecimal using the <code>HEX()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">HEX<\/span>(<span class=\"hljs-keyword\">data<\/span>) \n<span class=\"hljs-keyword\">FROM<\/span> binary_demo <span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">id<\/span> = <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">+<span class=\"hljs-comment\">----+--------------------------------------------------------------------+<\/span>\n| id | data                                                               |\n+<span class=\"hljs-comment\">----+--------------------------------------------------------------------+<\/span>\n|  1 | 0x185F8DB32271FE25F561A6FC938B2E264306EC304EDA518007D1764826381969 |\n+<span class=\"hljs-comment\">----+--------------------------------------------------------------------+<\/span>\n1 row in <span class=\"hljs-keyword\">set<\/span> (<span class=\"hljs-number\">0.00<\/span> sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>BINARY<\/code> data type to store fixed-length binary data such as hashes or <code>UUID<\/code>. <\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"11697\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/\"\n\t\t\t\tdata-post-title=\"MySQL BINARY Data Type\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"11697\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/\"\n\t\t\t\tdata-post-title=\"MySQL BINARY Data Type\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about MySQL BINARY data type and how to use it to store fixed-length binary data.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":78,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-11697","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL BINARY Data Type<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn about MySQL BINARY data type and how to use it to store fixed-length binary data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL BINARY Data Type\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn about MySQL BINARY data type and how to use it to store fixed-length binary data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-30T03:28:16+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-binary\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-binary\\\/\",\"name\":\"MySQL BINARY Data Type\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"datePublished\":\"2023-10-28T02:16:29+00:00\",\"dateModified\":\"2024-01-30T03:28:16+00:00\",\"description\":\"In this tutorial, you will learn about MySQL BINARY data type and how to use it to store fixed-length binary data.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-binary\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-binary\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-binary\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL BINARY Data Type\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL BINARY Data Type","description":"In this tutorial, you will learn about MySQL BINARY data type and how to use it to store fixed-length binary data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/","og_locale":"en_US","og_type":"article","og_title":"MySQL BINARY Data Type","og_description":"In this tutorial, you will learn about MySQL BINARY data type and how to use it to store fixed-length binary data.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-30T03:28:16+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/","url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/","name":"MySQL BINARY Data Type","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"datePublished":"2023-10-28T02:16:29+00:00","dateModified":"2024-01-30T03:28:16+00:00","description":"In this tutorial, you will learn about MySQL BINARY data type and how to use it to store fixed-length binary data.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-binary\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL BINARY Data Type"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/11697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=11697"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/11697\/revisions"}],"predecessor-version":[{"id":14602,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/11697\/revisions\/14602"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=11697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}