MYSQL entry 5: MYSQL character set _ MySQL

Source: Internet
Author: User
MYSQL Getting started: MYSQL character set bitsCN.com

MYSQL entry 5: MYSQL character set

Related links:

MYSQL: Basic operations

Http: // database/201212/173868 .html

MYSQL 2: use regular expressions to search

Http: // database/201212/173869 .html

MYSQL 3: full text search

Http: // database/201212/173873 .html

MYSQL entry 4: MYSQL data types

Http: // database/201212/175536 .html

MySQL Character Set Support has two aspects: Character set and Collation ).

The support for character sets is refined to four levels: server, database, table, and connection ).

1. default MySQL character set

MySQL can refine the character set designation to a database, a table, and a column.

However, traditional programs do not use such complex configurations when creating databases and data tables. they use the default configuration. So where does the default configuration come from?

(1) when compiling MySQL, a default character set is specified, which is latin1;

(2) when installing MySQL, you can specify a default character set in the configuration file (my. ini). If this character set is not specified, this value inherits from the one specified during compilation;

(3) when mysqld is started, you can specify a default character set in the command line parameters. if not, this value inherits from the configuration in the configuration file, character_set_server is set to the default character set;

(4) when creating a new database, unless explicitly specified, the character set of this database is set to character_set_server by default;

(5) when a database is selected, character_set_database is set to the default character set of the database;

(6) when creating a table in the database, the default character set of the table is set to character_set_database, which is the default character set of the database;

(7) when setting a column in a table, unless explicitly specified, the default character set in this column is the default character set of the table;

Simply put, if you do not modify anything, all the columns of all tables in all databases will be stored in latin1. However, if you install MySQL, you will generally choose multi-language support, that is, the installer automatically sets default_character_set in the configuration file as a UTF-8, which ensures that all columns of all tables in all databases are stored in UTF-8 by default.

II. View character sets

1. View MySQL database servers and database character sets

Mysql> show variables like '% char % ';

+ -------------------------- + ------------------------------------------ +

| Variable_name | Value |

+ -------------------------- + ------------------------------------------ +

| Character_set_client | latin1 | -- client character set

| Character_set_connection | latin1 |

| Character_set_database | latin1 | -- Database character set

| Character_set_filesystem | binary |

| Character_set_results | latin1 |

| Character_set_server | latin1 | -- Server character set

| Character_set_system | utf8 |

| Character_sets_dir | D:/APMServ5.2.6/MySQL5.1/share/charsets/|

+ -------------------------- + ------------------------------------------ +

2. view the character set of the MySQL data table

Mysql> show table status from test like '% test % ';

+ --------------- + -------- + --------- + ------------ ++ ------------------- +

| Name | Engine | Version | Row_format | Collation |

+ --------------- + -------- + --------- + ------------ ++ ------------------- +

| Test_char | MyISAM | 10 | Fixed | hebrew_general_ci |

| Test_inn | InnoDB | 10 | Compact | latin1_swedish_ci |

| Test_inn2 | InnoDB | 10 | Compact | utf8_general_ci |

| Test_priority | MyISAM | 10 | Dynamic | latin1_swedish_ci |

| Test_trans | InnoDB | 10 | Compact | latin1_swedish_ci |

| Test_view | NULL |

+ --------------- + -------- + --------- + ------------ ++ ------------------- +

3. view the character set of the MySQL data column (column)

Mysql> show full columns from v9_picture;

+ ------------- + ----------------------- + ---------------- +

| Field | Type | Collation |

+ ------------- + ----------------------- + ---------------- +

| Id | mediumint (8) unsigned | NULL |

| Catid | smallint (5) unsigned | NULL |

| Typeid | smallint (5) unsigned | NULL |

| Title | char (80) | gbk_chinese_ci |

| Style | char (24) | gbk_chinese_ci |

| Thumb | char (100) | gbk_chinese_ci |

| Keywords | char (40) | gbk_chinese_ci |

| Description | char (255) | gbk_chinese_ci |

| Posids | tinyint (1) unsigned | NULL |

| Url | char (100) | gbk_chinese_ci |

| Listorder | tinyint (3) unsigned | NULL |

| Status | tinyint (2) unsigned | NULL |

| Sysadd | tinyint (1) unsigned | NULL |

| Islink | tinyint (1) unsigned | NULL |

| Username | char (20) | gbk_chinese_ci |

| Inputtime | int (10) unsigned | NULL |

| Updatetime | int (10) unsigned | NULL |

+ ------------- + ----------------------- + ---------------- +

3. set and modify character sets

1. create a database. specify the character set of the database.

Mysql> create database mydb character set gb2312;

Mysql> use mydb

Mysql> show variables like 'character _ set_database ';

+ ------------------------ + -------- +

| Variable_name | Value |

+ ------------------------ + -------- +

| Character_set_database | gb2312 |

+ ------------------------ + -------- +

2. modify the character set of the database

(1) modifying through SQL

Mysql> alter database mydb character set utf8;

Mysql> show variables like 'character _ set_database ';

+ ------------------------ + ------- +

| Variable_name | Value |

+ ------------------------ + ------- +

| Character_set_database | utf8 |

+ ------------------------ + ------- +

(2) modify the configuration file

Modify the content of the file $ MYSQL_HOME/data/mydb/db. opt.

Default-character-set = utf8

Default-collation = utf8_general_ci

Is

Default-character-set = latin1

Default-collation = latin1_swedish_ci

It takes effect after MYSQL is restarted.

3. modifying character sets through SQL

Mysql> set character_set_client = utf8;

Mysql> set character_set_connection = utf8;

Mysql> set character_set_database = utf8;

Mysql> set character_set_results = utf8;

Mysql> set character_set_server = utf8;

Mysql> set character_set_system = utf8;

Mysql> set collation_connection = utf8;

Mysql> set collation_database = utf8;

Mysql> set collation_server = utf8;

BitsCN.com
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.