本文介绍了MariaDB不会使用大VARCHAR作为主键创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在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个 bytes . (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个 byte )
  • utf8:255个字符(utf8是真实" utf8的3个字节的子集)
  • utf8mb4:191个字符(允许使用4字节utf8编码.
  • latin1: 765 characters (+2 bytes for length; each character is 1 byte)
  • utf8: 255 characters (utf8 is the 3-byte subset of the 'real' utf8)
  • utf8mb4: 191 characters (4-byte utf8 encodings are allowed.

对于较新的版本,限制约为该限制的4倍.

For newer versions, the limits are about 4 times that.

如果确实需要大于可用限制的PRIMARY KEYUNIQUE 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作为主键创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 09:41