如何更改 MySQL 列的排序规则类型?
我有 utf-8 Vs。这里提到的字节字符串问题: Django 头痛与简单的非 ascii 字符串
我不关心 MySQL 列中区分大小写的匹配,我只是总是希望返回 UTF-8 字符串,因为我发现不可能处理为非 ascii 文本的字符列返回的字节字符串。
如何更改 MySQL 排序规则类型,以便始终通过 Django 返回 UTF-8 字符串?
I'm having the utf-8 Vs. byte string problems mentioned here: Django headache with simple non-ascii string
I don't care about case sensitive matching in the MySQL columns, I just always want UTF-8 strings returned because I find it is impossible to deal with byte strings returned for character columns for non-ascii text.
How do I change my MySQL collation type so that UTF-8 strings are always returned through Django?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要了解数据库/表/列级别的字符集/排序规则设置。列级设置优先于其他设置。因此,我包含了可用于在数据库的每个级别执行这些更改的命令。
检查您当前的配置(数据库):
SHOW CREATE DATABASE db_name;
检查您当前的配置(表):
SHOW TABLE STATUS WHERE name='tbl_name'
检查您当前的配置(列):
SHOW FULL COLUMNS FROM tbl_name;
更改字符集/排序规则(数据库):
ALTER DATABASE db_name DEFAULT CHARACTER SET utf8;
更改字符集/排序规则(表):
ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8;
更改字符集/排序规则(列):
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
You need to be aware of the character-set/collation settings at the database/table/column levels. Column-level settings take precedence over the others. Because of this, I'm including commands you can use to perform these changes at each level of the db.
Inspect your current configuration (database):
SHOW CREATE DATABASE db_name;
Inspect your current configuration (table):
SHOW TABLE STATUS WHERE name='tbl_name'
Inspect your current configuration (columns):
SHOW FULL COLUMNS FROM tbl_name;
Change the character-set/collation (database):
ALTER DATABASE db_name DEFAULT CHARACTER SET utf8;
Change the character-set/collation (table):
ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8;
Change the character-set/collation (columns):
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
在 django 中,您必须编写自己的迁移:
并使用这些 sql 命令填充空迁移,如下所示:
In django you must write your own migration:
And fill empty migration with these sql command like this:
请注意,如果您确实只想更改一列的排序规则(我不知道为什么会这样做,但谁知道),那么这是更改名为
的
为 UTF-8,二进制,非空:TEXT
列的语法ITEMS
表中的描述本身没有区分大小写的 UTF-8 排序规则,但
utf8_bin
排序规则适用于大多数情况。Note that if you really did want to change the collation for just one column (I can't think why you might, but who knows) then this is the syntax to alter a
TEXT
column calledDESCRIPTION
in theITEMS
table to UTF-8, binary, non-null:There isn't a case-sensitive UTF-8 collation per se but the
utf8_bin
collation works for most cases.