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).
-
The NULL expression and the data type of the expression do not support expressions of collations.
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 33 34 35 36 37 38 39 40 |
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) -- Define the collation of the bound parameter ? as collation_connection. m_db=# PREPARE test_collation FROM 'SELECT c1 = ? AS result FROM t_utf8'; -- The collation of the bound parameter is at the same level as that of the string constant. The collation c1 has been determined in the previous step. Therefore, the result is false. m_db=# SET @aa = 'string' COLLATE utf8mb4_general_ci; m_db=# EXECUTE test_collation using @aa; result -------- f (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) |
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 two character sets are different, the BINARY collation is preferred.
- If one character set is Unicode and the other one is not Unicode, a non-Unicode expression needs to be transcoded to a Unicode expression, and the collation of the Unicode expression is 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 a conflict occurs between collations of the same character sets because one collation is not suffixed with _bin, the collation suffixed with _bin is used in the character string concatenation function. If the collation suffixed with _bin is used in scenarios not for character string concatenation, an exception is reported.
-
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 |
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) -- Collation conflict. Binary comparison is performed, and the result is false. 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; ^ -- Conflict of the explicitly specified collation. An exception is reported. 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... ^ |
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
