Rules for Combining Character Sets and Collations
Expressions with different character sets and collations are processed based on certain priorities to determine the collations used for character string comparison and the character sets of the expressions.
Collation Priority
The priorities of different expressions in descending order are as follows:
-
Expressions with collation conflicts (for example, two character strings with different collations).
-
Columns of data types that support collation, user-defined variables, stored procedure parameters, and CASE expressions.
-
Specific system functions (such as version() and opengauss_version() function expressions).
-
If a data type of an expression does not support collation, this expression has the lowest priority.
When the collations of two expressions are different, the collation of the expression with the highest priority is used.
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
m_db=# CREATE TABLE t_utf8(c1 varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin); m_db=# INSERT INTO t_utf8 VALUES('STRING'); -- If the utf8mb4_bin collation is used for comparison, the result is false. m_db=# SELECT c1 = 'string' AS result FROM t_utf8; result -------- f (1 row) -- If the utf8mb4_general_ci collation is used for comparison, the result is true. m_db=# SELECT c1 = 'string' COLLATE utf8mb4_general_ci AS result FROM t_utf8; result -------- t (1 row) -- The CASE expression is at the same level as the c1 column. Even if the expression contains an explicit collation, the collation of the c1 column is still used for comparison. The two collations are different. "same level" is displayed. m_db=# SELECT CASE 'string' COLLATE utf8mb4_general_ci WHEN c1 THEN 'different level' ELSE 'same level' END AS result FROM t_utf8; result ------------ same level (1 row) -- The IN subquery has the same level as the c1 column. Even if the expression contains an explicit collation, the collation of c1 is still used for comparison. The two collations are different. m_db=# SELECT c1 FROM t_utf8 WHERE c1 in (SELECT 'string' COLLATE utf8mb4_general_ci); c1 ---- (0 rows) -- Drop the table. m_db=# DROP TABLE t_utf8; |
If the collations of two expressions with the same priority are different, the following processing method is used:
- If the two character sets are the same, the collation suffixed with _bin is preferred.
- If the two character sets are the same, the default collation is not used.
- If the preceding conditions are not met, the two expressions are marked as a collation conflict, and the collations are marked as invalid.
-
If a conflict occurs because the COLLATE syntax specifies different collations for the same character set, an exception is displayed.
- If the two conflicting collations are supported by M-compatible, an exception message is displayed.
- If an invalid collation is used for sorting operations (such as > and <), an exception is displayed.
- During equality comparison of character strings, if the collation is invalid, the character strings are compared as binary values.
-
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
m_db=# CREATE TABLE t_utf8mb4_charset( c_utf8_bin varchar(16) character set utf8mb4 collate utf8mb4_bin, c_utf8_uni varchar(16) character set utf8mb4 collate utf8mb4_unicode_ci, c_utf8_gen varchar(16) character set utf8mb4 collate utf8mb4_general_ci); m_db=# INSERT INTO t_utf8mb4_charset VALUES('STRING', 'String', 'string'); -- The utf8mb4_bin collation is preferentially used for comparison. The result is false. m_db=# SELECT c_utf8_bin = c_utf8_uni FROM t_utf8mb4_charset; ?column? ---------- f (1 row) -- If the collation conflicts, an exception message is displayed. m_db=# SELECT c_utf8_uni = c_utf8_gen FROM t_utf8mb4_charset; ERROR: Collation mismatch between collations "utf8mb4_unicode_ci" and "utf8mb4_general_ci". LINE 1: SELECT c_utf8_uni = c_utf8_gen FROM t_utf8mb4_charset; ^ -- If the explicitly specified collation conflicts, an exception message is displayed. m_db=# SELECT c_utf8_uni COLLATE utf8mb4_unicode_ci = c_utf8_gen COLLATE utf8mb4_general_ci FROM t_utf8mb4_charset; ERROR: collation mismatch between explicit collations "utf8mb4_unicode_ci" and "utf8mb4_general_ci" LINE 1: ..._utf8_uni COLLATE utf8mb4_unicode_ci = c_utf8_gen COLLATE ut... ^ -- Drop the table. m_db=# DROP TABLE t_utf8mb4_charset; |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot
