{"id":2332,"date":"2013-05-14T00:45:54","date_gmt":"2013-05-14T08:45:54","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=2332"},"modified":"2023-10-23T03:25:18","modified_gmt":"2023-10-23T10:25:18","slug":"mysql-character-set","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-character-set\/","title":{"rendered":"MySQL Character Sets"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, we&#8217;ll explore MySQL character sets and their importance in how MySQL stores text data.<\/p>\n\n\n\n<p>In MySQL, the <strong>character set<\/strong> and <strong><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-collation\/\">collation<\/a><\/strong> are essential concepts that define how data is stored and sorted in text columns. <\/p>\n\n\n\n<p>Understanding character sets and collations are crucial for designing databases that can handle different languages and text requirements. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL Character Sets<\/h2>\n\n\n\n<p>A character set is a collection of characters with a unique encoding. It defines the set of characters that can be used in a text column, such as letters, numbers, symbols, and special characters. <\/p>\n\n\n\n<p>MySQL supports various character sets, and the choice of character set determines <strong>the range of characters <\/strong>that can be stored in a column. <\/p>\n\n\n\n<p>Common character sets include <code>utf8<\/code>, <code>utf8mb4<\/code>, <code>latin1<\/code>, <code>utf16<\/code>, and many others. To list all character sets in the current MySQL server, you use the following statement:<\/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\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-built_in\">CHARACTER<\/span> <span class=\"hljs-keyword\">SET<\/span>;<\/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>Here&#8217;s the partial output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">+----------+---------------------------------+---------------------+--------+\n| Charset  | Description                     | <span class=\"hljs-keyword\">Default<\/span> collation   | Maxlen |\n+----------+---------------------------------+---------------------+--------+\n| armscii8 | ARMSCII<span class=\"hljs-number\">-8<\/span> Armenian              | armscii8_general_ci |      <span class=\"hljs-number\">1<\/span> |\n| ascii    | US ASCII                        | ascii_general_ci    |      <span class=\"hljs-number\">1<\/span> |\n| big5     | Big5 Traditional Chinese        | big5_chinese_ci     |      <span class=\"hljs-number\">2<\/span> |\n| binary   | Binary pseudo charset           | binary              |      <span class=\"hljs-number\">1<\/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\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Maxlen<\/code> column specifies the number of bytes that a character in a character set holds. <\/p>\n\n\n\n<p>Some character sets contain single-byte characters e.g., <code>latin1<\/code> , <code>latin2<\/code> , <code>cp850<\/code> , etc., whereas other character sets contain multi-byte characters.<\/p>\n\n\n\n<p>The default character sets are <code>utf8mb4<\/code> and&nbsp;<code>utf8mb4_0900_ai_ci<\/code>. However, you can change them accordingly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Converting between different character sets<\/h2>\n\n\n\n<p>MySQL provides two functions that allow you to convert strings between different character sets: <code>CONVERT<\/code> and <code>CAST<\/code>.<\/p>\n\n\n\n<p>The syntax of the <code>CONVERT<\/code> function is as follows:<\/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\">CONVERT(expression USING character_set_name)<\/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>CAST<\/code> function is similar to the <code>CONVERT<\/code> function. It converts a string to a different character set:<\/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\">CAST(string AS character_type CHARACTER <span class=\"hljs-keyword\">SET<\/span> character_set_name)<\/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>Take a look at the following example of using the <code>CAST<\/code> function:<\/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-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">CAST<\/span>(_latin1<span class=\"hljs-string\">'MySQL character set'<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-built_in\">CHAR<\/span> <span class=\"hljs-built_in\">CHARACTER<\/span> <span class=\"hljs-keyword\">SET<\/span> utf8);<\/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\">Setting character sets for client connections<\/h2>\n\n\n\n<p>To configure a character set for a client connection, you can do one of the following ways:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using the SET NAMES statement<\/h3>\n\n\n\n<p>Issue the <code>SET NAME<\/code> &nbsp;statement after the client connected to the MySQL database server. For example, to set a Unicode character set <code>utf8mb4<\/code>, you use the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">NAMES<\/span> <span class=\"hljs-string\">'utf8mb4'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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<h3 class=\"wp-block-heading\">2) Using &#8211;default-character-set option<\/h3>\n\n\n\n<p>If the application supports the <code>--default-character-set<\/code> &nbsp;option, you can use it to set the character set. <\/p>\n\n\n\n<p>For example, mysql client tool supports <code>--default-character-set<\/code> option&nbsp;and you can set it up in the configuration file as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">&#91;mysql]\ndefault-character-set=utf8mb4<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">3) Using the charset in connection strings<\/h3>\n\n\n\n<p>Some MySQL connectors allow you to specify a character set. For example, if you use <a href=\"https:\/\/www.phptutorial.net\/php-pdo\/\">PHP PDO<\/a>, you can set the character set in the data source name as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$dsn =<span class=\"hljs-string\">\"mysql:host=$host;dbname=$db;charset=utf8mb4\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Regardless of which way you use it, make sure that the character set used by the application matches the character set stored in the MySQL database server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Character sets define how MySQL stores data in text columns.<\/li>\n\n\n\n<li>Use the <code>SHOW CHARACTER SET<\/code> statement to list all character sets.<\/li>\n\n\n\n<li>Use the <code>CONVERT()<\/code> or <code>CAST()<\/code> function to convert between character sets.<\/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=\"2332\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-character-set\/\"\n\t\t\t\tdata-post-title=\"MySQL Character Sets\"\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=\"2332\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-character-set\/\"\n\t\t\t\tdata-post-title=\"MySQL Character Sets\"\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, we&#8217;ll explore MySQL character sets and their importance in how MySQL stores text data.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":83,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2332","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 Character Sets<\/title>\n<meta name=\"description\" content=\"In this tutorial, we&#039;ll explore MySQL character sets and their importance in how MySQL stores text 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-character-set\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL Character Sets\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we&#039;ll explore MySQL character sets and their importance in how MySQL stores text data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-character-set\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-23T10:25:18+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-character-set\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-character-set\\\/\",\"name\":\"MySQL Character Sets\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"datePublished\":\"2013-05-14T08:45:54+00:00\",\"dateModified\":\"2023-10-23T10:25:18+00:00\",\"description\":\"In this tutorial, we'll explore MySQL character sets and their importance in how MySQL stores text data.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-character-set\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-character-set\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-character-set\\\/#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 Character Sets\"}]},{\"@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 Character Sets","description":"In this tutorial, we'll explore MySQL character sets and their importance in how MySQL stores text 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-character-set\/","og_locale":"en_US","og_type":"article","og_title":"MySQL Character Sets","og_description":"In this tutorial, we'll explore MySQL character sets and their importance in how MySQL stores text data.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-character-set\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-10-23T10:25:18+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-character-set\/","url":"https:\/\/www.mysqltutorial.org\/mysql-character-set\/","name":"MySQL Character Sets","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"datePublished":"2013-05-14T08:45:54+00:00","dateModified":"2023-10-23T10:25:18+00:00","description":"In this tutorial, we'll explore MySQL character sets and their importance in how MySQL stores text data.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-character-set\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-character-set\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-character-set\/#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 Character Sets"}]},{"@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\/2332","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=2332"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2332\/revisions"}],"predecessor-version":[{"id":11594,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2332\/revisions\/11594"}],"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=2332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}