简体   繁体   English

如何更改MySQL列的排序规则类型?

[英]How do you change the collation type for a MySQL column?

I'm having the utf-8 Vs. 我有utf-8 Vs. byte string problems mentioned here: Django headache with simple non-ascii string 这里提到的字节串问题: Django头痛与简单的非ascii字符串

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. 我不关心MySQL列中的区分大小写匹配,我只是总是想要返回UTF-8字符串,因为我发现不可能处理为非ascii文本返回字符列的字节串。

How do I change my MySQL collation type so that UTF-8 strings are always returned through Django? 如何更改我的MySQL排序规则类型,以便始终通过Django返回UTF-8字符串?

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. 因此,我将包含可用于在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;

In django you must write your own migration: 在django中,您必须编写自己的迁移:

./manage.py makemigrations --empty app_name

And fill empty migration with these sql command like this: 并使用这些sql命令填充空迁移,如下所示:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('app', '0008_prev_migration'),
    ]

    operations = [
        migrations.RunSQL('ALTER DATABASE db_name DEFAULT CHARACTER SET utf8;'),
        migrations.RunSQL('ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8;'),
        migrations.RunSQL('ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;'),
    ]

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 called DESCRIPTION in the ITEMS table to UTF-8, binary, non-null: 请注意,如果您确实只想更改一列的排序规则(我想不出您可能会这样,但谁知道)那么这就是将ITEMS表中名为DESCRIPTIONTEXT列更改为UTF-8的语法,二进制,非空:

ALTER TABLE ITEMS CHANGE DESCRIPTION DESCRIPTION TEXT CHARACTER SET utf8
    COLLATE utf8_bin NOT NULL;

There isn't a case-sensitive UTF-8 collation per se but the utf8_bin collation works for most cases. 本身没有区分大小写的UTF-8排序规则utf8_bin排序规则适用于大多数情况。

ALTER DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM