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

发布于 2024-11-08 07:28:14 字数 299 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

舂唻埖巳落 2024-11-15 07:28:14

您需要了解数据库/表/列级别的字符集/排序规则设置。列级设置优先于其他设置。因此,我包含了可用于在数据库的每个级别执行这些更改的命令。


检查您当前的配置(数据库):

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;

陌路终见情 2024-11-15 07:28:14

在 django 中,您必须编写自己的迁移:

./manage.py makemigrations --empty app_name

并使用这些 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;'),
    ]

In django you must write your own migration:

./manage.py makemigrations --empty app_name

And fill empty migration with these sql command like this:

# -*- 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;'),
    ]
李白 2024-11-15 07:28:14
ALTER DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
ALTER DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
橘亓 2024-11-15 07:28:14

请注意,如果您确实只想更改一列的排序规则(我不知道为什么会这样做,但谁知道),那么这是更改名为 TEXT 列的语法ITEMS 表中的描述 为 UTF-8,二进制,非空:

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

本身没有区分大小写的 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 called DESCRIPTION in the ITEMS table to UTF-8, binary, non-null:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文