Let’s say that I want to send the following SQL query to the MySQL server:
SELECT * FROM students;
Which character set will be used to encode the above statement, and which character set will be used to encode the response from the MySQL server? and can you change this character set?
2
Answers
Look at system variables of your MySQL server and definition of database and tables:
By default tables have database charset.
MySQL automatically changes the encoding of strings when entering data into the table and when fetching data from the table. It uses data from system variables, such as character_set_client for this.
The question is not quite right. You need to specify the character set encoding in two places: The bytes on the client and the bytes on the server.
Client encoding
Best: Establish the in/out character set via connection parameters. There you specify the encoding of the client.
Second best is
SET NAMES utf8mb4;
right after connecting (assuming you wantutf8mb4
).Server encoding
The charset on each column (in the database) comes from the column definition. If missing, then the default comes from the table definition. Run
SHOW CREATE TABLE
.If client and server are not the same, MySQL will transcode when
INSERTing
and whenSELECTing
.If you have specific symptoms, see Trouble with UTF-8 characters; what I see is not what I stored