0

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?

user8240761
  • 975
  • 1
  • 10
  • 15
  • google search "mysql character set" -> 1st result: https://dev.mysql.com/doc/refman/8.0/en/charset.html – Georg Richter Feb 26 '23 at 09:17
  • @Georg Richter I saw that page before asking this question, but I am not 100% sure it is what I'm looking for, so you're saying I should use `SET NAMES 'utf8mb4';`? – user8240761 Feb 26 '23 at 09:36
  • Related: [UTF-8 all the way through](https://stackoverflow.com/questions/279170/) – JosefZ Feb 26 '23 at 10:52

2 Answers2

1

Look at system variables of your MySQL server and definition of database and tables:

mysql> SHOW VARIABLES like '%char%';

mysql> show create database <your_database_name>;

mysql> show create table <your table, like "students">;

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.

Dimitry
  • 128
  • 1
  • 1
0

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 want utf8mb4).

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 when SELECTing.

If you have specific symptoms, see Trouble with UTF-8 characters; what I see is not what I stored

Rick James
  • 135,179
  • 13
  • 127
  • 222