MariaDB won#39;t create table with large VARCHAR as PRIMARY KEY(MariaDB 不会创建以大 VARCHAR 作为 PRIMARY KEY 的表)
问题描述
我试图在 MariaDB 中创建一个表,我希望它是 VARCHAR(767) 和 PRIMARY KEY.我使用了这个命令,但这不是我想要的.
I tried to create a table in MariaDB, I wanted it to be VARCHAR(767) and PRIMARY KEY. I used this command but this is not what I want to.
CREATE TABLE main(username VARCHAR(767) NOT NULL);
这个命令执行了,但是如果我添加PRIMARY KEY就会出现错误.
This command is executed, but if I add PRIMARY KEY the error will be appeared.
CREATE TABLE main(username VARCHAR(767) NOT NULL PRIMARY KEY);
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
推荐答案
VARCHAR(767)
允许 767 个字符.如果默认字符集是 utf8,则需要 3*767 字节.
VARCHAR(767)
allows 767 characters. If the default character set is utf8, that entails 3*767 bytes.
旧版本的索引中单个列的限制为 767 个字节.(而 PRIMARY KEY
是一个索引.)
Older versions have a limit of 767 bytes for an individual column in an index. (And PRIMARY KEY
is an index.)
以下是旧版本中的 varchar 限制:
Here are the varchar limits in older versions:
- latin1:765 个字符(长度+2 个字节;每个字符为 1 个字节)
- utf8:255 个字符(utf8 是真实"utf8 的 3 字节子集)
- utf8mb4:191 个字符(允许 4 字节 utf8 编码.
对于较新的版本,限制约为 4 倍.
For newer versions, the limits are about 4 times that.
如果你真的需要一个大于可用限制的 PRIMARY KEY
或 UNIQUE KEY
,我们可以讨论使用哈希等技巧.但我们需要了解什么类型的它是什么数据,你使用的是什么版本,你需要什么字符集等等.
If you really need a PRIMARY KEY
or UNIQUE KEY
bigger than the available limit, we can discuss tricks using hashes, etc. But we need to understand what type of data it is, what version you are using, what charset you need, etc.
这篇关于MariaDB 不会创建以大 VARCHAR 作为 PRIMARY KEY 的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MariaDB 不会创建以大 VARCHAR 作为 PRIMARY KEY 的表


- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01