Alix Axel
Alix Axel

Reputation: 154673

MySQL and UTF-8

In MySQL, what is the difference between doing:

SET NAMES 'utf8'

And:

SET CHARACTER SET 'utf8'

I've taken a look at Connection Character Sets and Collations MySQL documentation page but I'm still a bit confused... Do both commands need to be issued in order to make MySQL UTF-8 aware? Or is SET NAMES enough?

Upvotes: 4

Views: 703

Answers (2)

OMG Ponies
OMG Ponies

Reputation: 332731

SET NAMES

SET NAMES indicates what character set the client will use to send SQL statements to the server. That means that SET NAMES 'cp1251' tells the server “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client.

SET CHARACTER SET

SET CHARACTER SET is similar to SET NAMES, but sets character_set_connection and collation_connection to character_set_database and collation_database. A SET CHARACTER SET x statement is equivalent to these three statements:

SET character_set_client = x;
SET character_set_results = x;
SET collation_connection = @@collation_database;

Do both commands need to be issued in order to make MySQL UTF-8 aware? Or is SET NAMES enough?

SET NAMES is enough.

Upvotes: 3

user131077
user131077

Reputation:

SET NAMES has encoding for connection, and SET CHARACTER SET is encoding for table. Using both command is highly recomment, to avoid terrible charter problems...

Upvotes: 0

Related Questions