MySQL 5.6 create view with unicode character set

不想你离开。 提交于 2021-01-28 14:00:38

问题


MySQL 5.6. I can't get a string constant within a view to populate correctly against a database with default UCS2 character set. Works fine on 5.7.

I've created a minimally reproducible example, below.

DROP SCHEMA IF EXISTS test3;
CREATE SCHEMA test3 CHARACTER SET ucs2;
CONNECT test3;

CREATE TABLE testtable (
testname VARCHAR(15)
);

INSERT INTO testTable( testname ) VALUES ('foo');
INSERT INTO testTable( testname ) VALUES ('bar');

CREATE OR REPLACE VIEW testview AS
SELECT * FROM testtable
WHERE testname = 'foo';

SELECT * FROM testview;

^^^ This select statement returns no results.

MySQL [test3]> show create view testview \G
*************************** 1. row ***************************
                View: testview
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost`
SQL SECURITY DEFINER VIEW `testview` AS select `testtable`.`testname` AS 
`testname` from `testtable` where (`testtable`.`testname` = '\0\0\0f\0\0\0o\0\0\0o')
character_set_client: utf8
collation_connection: utf8_general_ci

What is that, utf32??

The following does work, but I don't want to write the collation directly into the statement, as this needs to be portable code and the syntax looks non-standard:

CREATE OR REPLACE VIEW testview AS
SELECT * FROM testtable
WHERE testname = 'foo' COLLATE utf8_general_ci;

I have tried setting the client, connection, and server character sets to ucs2 and utf16 but this changed nothing. Likewise with the collations to *_general_ci.

Any ideas?

Edit:

MySQL [test3]> show variables like "char%";
+--------------------------+------------------------------------------------------------+
| Variable_name            | Value                                                      |
+--------------------------+------------------------------------------------------------+
| character_set_client     | utf8                                                       |
| character_set_connection | utf8                                                       |
| character_set_database   | ucs2                                                       |
| character_set_filesystem | binary                                                     |
| character_set_results    | utf8                                                       |
| character_set_server     | latin1                                                     |
| character_set_system     | utf8                                                       |
| character_sets_dir       | C:\Program Files\MySQL\mysql-5.6.36-winx64\share\charsets\ |
+--------------------------+------------------------------------------------------------+

回答1:


There is essentially no reason to ever use usc2 or utf16 or utf32 in MySQL tables. Use utf8mb4 only. (Or utf8 if you have an old version of MySQL.)

Please provide SHOW VARIABLES LIKE "char%"; Certain things should not be changed:

mysql> SHOW VARIABLES LIKE "char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     | <--
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       | <--
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

When you created the view, you did not set the charset. I can see that from your SHOW when it said:

character_set_client: utf8


来源:https://stackoverflow.com/questions/46353062/mysql-5-6-create-view-with-unicode-character-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!