Panoramica sui Set di Caratteri e le Collation

Cosa sono i Set di Caratteri e le Collation

Un Set di Caratteri (character set) è un insieme di caratteri, mentre una Collation è un insieme di regole da usare per comparare e ordinare un dato set di caratteri.

Per esempio, un sottoinsieme di un character set potrebbe consistere nelle lettere A, B e C. La Collation di default potrebbe stabilire che l'ordine di queste lettere è A, B, C.

Se si considerano caratteri sia maiuscoli sia minuscoli, la questione diventa più complessa. Una collation binaria considera diversi i caratteri A e a, e ne stabilisce l'ordine. Una collation case-insensitive (che quindi non fa differenza tra maiuscole e minuscole) considera equivalenti A e a, mentre la collation dell'elenco telefonico tedesco considera equivalenti i caratteri ue e ü.

Un character set può avere più collation, ma ogni collation può essere associata ad un solo set di caratteri. In MariaDB, il nome del character set fa sempre parte del nome della collation. Per esempio, la collation latin1_german1_ci su applica solo al set di caratteri latin1. Ogni character set ha anche una collation di default. La collation di default di latin1 è latin1_swedish_ci.

Ad esempio, per default, il carattere y si trova tra x e z, mentre in lituano si trova tra i e k. Allo stesso modo, l'ordine dell'elenco telefonico tedesco è diverso dall'ordine del dizionario tedesco, perciò possiamo dire che essi condividono lo stesso set di caratteri, ma la collation è differente.

Viewing character sets and collations

In MariaDB, the default character set is latin1, and the default collation is latin1_swedish_ci. You can view a full list of character sets and collations supported by MariaDB at Supported Character Sets and Collations, or see what's supported on your server with the SHOW CHARACTER SET and SHOW COLLATION commands.

By default, A comes before Z, so the following evaluates to true:

 SELECT "A" < "Z";
+-----------+
| "A" < "Z" |
+-----------+
|         1 |
+-----------+

By default, comparisons are case-insensitive:

SELECT "A" < "a", "A" = "a";
+-----------+-----------+
| "A" < "a" | "A" = "a" |
+-----------+-----------+
|         0 |         1 |
+-----------+-----------+

Changing character sets and collations

Character sets and collations can be set from the server level right down to the column level, as well as for client-server communication.

For example, ue and ü are by default evaluated differently.

SELECT 'Mueller' = 'Müller';
+----------------------+
| 'Müller' = 'Mueller' |
+----------------------+
|                    0 |
+----------------------+

By using the collation_connection system variable to change the connection character set to latin1_german2_ci, or German phone book, the same two characters will evaluate as equivalent.

SET collation_connection = latin1_german2_ci;

SELECT 'Mueller' = 'Müller';
+-----------------------+
| 'Mueller' = 'Müller'  |
+-----------------------+
|                     1 |
+-----------------------+

See Setting Character Sets and Collations for more.

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.