指定的密钥太长:最大密钥长度为3072个字节 [英] Specified key was too long: max key lenth is 3072 bytes

查看:380
本文介绍了指定的密钥太长:最大密钥长度为3072个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您的帮助,我已经实例化了所有关系表,但是当我要创建关系表时,它会向我发送错误:1071指定的密钥太长:最大密钥长度为3072字节

I need your help with this, i already have all relation tables instantiated but when im going to create the relationship table it sends me "Error: 1071 Specified key was too long: max key lenth is 3072 bytes"

直到现在我的代码:

CREATE TABLE tablahechos(
id_almacenf VARCHAR(255) NOT NULL,
id_productof VARCHAR(255) NOT NULL,
id_categoriaf VARCHAR(255) NOT NULL,
id_transaccionf VARCHAR(255) NOT NULL,
id_tipo_serviciof VARCHAR(255) NOT NULL,
id_mensajerof VARCHAR(255) NOT NULL,
id_clientef VARCHAR(255) NOT NULL,
id_tiempo_fecha_orden VARCHAR(255) NOT NULL,
id_tiempo_fecha_reserva_entrada VARCHAR(255) NOT NULL,
id_tiempo_fecha_reserva_salida VARCHAR(255) NOT NULL,
id_tiempo_fecha_inventario_entrada VARCHAR(255) NOT NULL,
id_tiempo_fecha_inventario_salida VARCHAR(255) NOT NULL,
id_tiempo_fecha_envio VARCHAR(255) NOT NULL,
id_tiempo_fecha_facturacion VARCHAR(25) NOT NULL,
id_tiempo_fecha_cancelada VARCHAR(255) NOT NULL,
para_regalo VARCHAR(45) NOT NULL,
monto_unitario VARCHAR(255) NOT NULL,
monto_total VARCHAR(45) NOT NULL,
cantidad VARCHAR(255) NOT NULL,
PRIMARY KEY (id_almacenf, id_productof, id_categoriaf, id_transaccionf, 
id_tipo_serviciof, id_mensajerof, id_clientef, id_tiempo_fecha_orden,
id_tiempo_fecha_reserva_entrada, id_tiempo_fecha_reserva_salida,
id_tiempo_fecha_inventario_entrada, id_tiempo_fecha_inventario_salida,
id_tiempo_fecha_envio, id_tiempo_fecha_facturacion, id_tiempo_fecha_cancelada),
CONSTRAINT FK1
  FOREIGN KEY (id_almacenf)
  REFERENCES almacendim (id_almacen),
CONSTRAINT FK2
  FOREIGN KEY (id_productof)
  REFERENCES productodim (id_producto),
CONSTRAINT FK3
  FOREIGN KEY (id_categoriaf)
  REFERENCES categoriadim (id_categoria),
CONSTRAINT FK4
  FOREIGN KEY (id_transaccionf)
  REFERENCES transacciondim (id_transaccion),
CONSTRAINT FK5
  FOREIGN KEY (id_mensajerof)
  REFERENCES mensajerodim (id_mensajero),
CONSTRAINT FK6
  FOREIGN KEY (id_clientef)
  REFERENCES clientedim (id_cliente),
CONSTRAINT FK7
  FOREIGN KEY (id_tiempo_fecha_orden)
    REFERENCES tablahechos (id_tiempo_fecha_orden),
CONSTRAINT FK8
  FOREIGN KEY (id_tiempo_fecha_reserva_entrada)
  REFERENCES tablahechos (id_tiempo_fecha_reserva_entrada),
CONSTRAINT FK9 
  FOREIGN KEY (id_tiempo_fecha_reserva_salida)
  REFERENCES tablahechos (id_tiempo_fecha_reserva_salida),
CONSTRAINT FK10
  FOREIGN KEY (id_tiempo_fecha_inventario_entrada)
  REFERENCES tablahechos (id_tiempo_fecha_inventario_entrada),
CONSTRAINT FK11 
  FOREIGN KEY (id_tiempo_fecha_inventario_salida)
  REFERENCES tablahechos (id_tiempo_fecha_inventario_salida),
CONSTRAINT FK12
  FOREIGN KEY (id_tiempo_fecha_envio)
    REFERENCES tablahechos (id_tiempo_fecha_envio),
CONSTRAINT FK13
  FOREIGN KEY (id_tiempo_fecha_facturacion)
  REFERENCES tablahechos (id_tiempo_fecha_facturacion),
CONSTRAINT FK14
  FOREIGN KEY (id_tiempo_fecha_cancelada) 
  REFERENCES tablahechos (id_tiempo_fecha_cancelada)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我已经将字符集更改为 latin1 并什么也没发生,我知道我的主键太长了,但是一定是那样。我应该怎么办?

i already change the charset to latin1 and nothing happens, i know my primary key is too long but it must be that way. What should i do?

推荐答案

幸运的是,有最大的密钥长度,因为您的主密钥太长了。

Fortunately, there is a maximum key length, because your primary key is way too long.

我强烈建议在表中添加自动递增的主键:

I would strongly suggest adding an auto incremented primary key to the table:

CREATE TABLE tablahechos (
    id_hechos int not null auto_increment primary key,
    id_almacenf VARCHAR(255) NOT NULL,
    . . .

这给您带来了什么呢?这个简化的主键在几个方面都有帮助:

What does this give you? Well, this simplified primary key helps in several ways:


  • 在表中查找一行很容易。

  • 它大大简化了所有外键引用

  • 它提供一种确定行插入顺序的机制。

如果需要要在所有这些列上构建键,您可以将列的长度缩短到32左右。 id列确实是否需要255个字符的长度?因此,您也应该在这些表格中用数字ID替换它们。实际上,您可能应该对所有这些列使用整数id,并使用联接查找有关它们的其他信息。

If you want to build a key on all those columns, you can shorten the length of the columns to something like 32. Do "id" column really need to be 255 characters in length? If so, you should replace them with numeric ids in those tables as well. In fact, you should probably be using integer ids for all these columns, with joins to look up additional information about them.

这篇关于指定的密钥太长:最大密钥长度为3072个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆