mysql1170错误(42000)BLOB/TEXT Column Used in Key Specification Without a KeyLength_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2419 | 回复: 0   主题: mysql1170错误(42000)BLOB/TEXT Column Used in Key Specification Without a KeyLength        下一篇 
d87025284
注册用户
等级:新兵
经验:62
发帖:59
精华:0
注册:2011-10-31
状态:离线
发送短消息息给d87025284 加好友    发送短消息息给d87025284 发消息
发表于: IP:您无权察看 2015-4-20 9:35:05 | [全部帖] [楼主帖] 楼主

以下是本人翻译国外的网站的解决方法

When creating a new table or altering an existing table with primary key,unique constraints and indexes ,or when defining
a new index with altering table manipulation statement in MySQL databases,the following error may occur and prohibit the command from completing:Error(42000)BLOB/TEXT Column Used in Key Specification Without a Key Length


在MySQL数据库中,当创建新表或者更改已存在表,这个表存在主键,并且是unique唯一性约束和索引约束时,或者是在定义一个索引来更改数据表的操作语句的时候,下面的错误信息很可能会出现,并且经过当前操作命令的完成。错误信息为BLOB或者TEXT字段使用了未指定键值长度的键

the error happens because MySQL can index only the first N chars of a BLOB or TEXT column .So the error mainly happens when
there is a field or column type of TEXT or BLOB or those belongs to TEXT,BLOB suchu as TINYTEXT,MEDIUMTEXT,LONGTEXT ,TINYBLOB,MEDIUMBLOB and LONGBLOB that you are trying to make as primary key or index.With full BLOB or TEXT without the length value,MySQL is unable to guarantee the uniqueness of the column as it's of variable and dynamic size.So,when using BLOB or TEXT types as index,the values of the N must be supplied so that MySQL can determine the key length.However ,MySQL doesn't support limit on TEXT/BLOB .TEXT(88) simply won't work.


错误发生的原因是因为MySQL只能将BLOB/TEXT类型字段设置索引为BLOB/TEXT数据的钱N个字符,因此错误常常发生在字段被定义为TEXT/BLOB类型或者和TEXT/BLOB同质的数据类型,如TINYTEXT,MEDIUMTEXT,LONGTEXT ,TINYBLOB,MEDIUMBLOB 和LONGBLOB,并且当前操作是将这个字段设置成主键或者是索引的操作。在未指定TEXT/BLOB‘键长’的情况下,字段是变动的并且是动态的大小所以MySQL不能够保证字段的唯一性。因此当使用TEXT/BLOB类型字段做为索引时,N的值必须提供出来才可以让MySQL决定键长,但是MySQL不支持在TEXT/BLOB限制,TEXT(88)是不行的。

the error will also popup when you try to convert a table column from non-TEXT and non-BLOB type such as VARCHAR and ENUM into TEXT or BLOB type,with the column already been defined as unique constraints or index ,the alter table commmand will fail.


当你试图将数据表中的一个非TEXT或者非BLOB类型如VARCHAR或ENUM的字段转换成TEXT/BLOB,同时这个字段已经被定义了unique限制或者是索引,这个错误也会弹出,并且更改数据表的命令会执行失败

the solution to the problem is to remove the index or unique constraint from the TEXT/BLOB column ,or set another field as primary key .If you can't do that ,and wanting to place a limit on the TEXT/BLOB column,try to user VARCHAR type and place a limit of length on it.Default,VARCHAR is limited to a maximum of 255 characters and its limit must be specified within a bracket right after is declaration ,i.e VARCHAR(200) will limit to 200 characters long only.


解决方案是将unique限制和索引从TEXT/BLOB字段中移除,或者是设置另一个字段为主键,如果你不愿意这样做并且想在TEXT/BLOB上加限制,那么你可以尝试将这个字段更改为VARCHAR类型,同时给他一个限制长度,默认VARCHAR最多可以限定在255个字符,并且限制要在声明类型的右边指明,如VARCHAR(200)将会限制仅仅200个字符

Sometimes,even though you don't use TEXT orBLOB related type in you table ,the error 1170 may also appear.It happens in situation such as when you specify VARCHAR column as primary key ,but wrongly set its length or characters size.VARCHAR can only accept up to 256 characters but something like VARCHAR(512) will force MySQL auto-convert the VARCHAR(512) to a SMALLTEXT type ,which subsequently fail with error 1170 on key length if the column is used as primary key or unique or index.To solve this problem ,specify a figure less than 256 as the size of the VARCHAR field


有时候,即使你在数据表中不使用TEXT/BLOB类型或者同质类型,error1170 也会出现,这个问题出现在当你设置一个VARCHAR字段为主键,但是却错误的设置了长度或者字符数,事实上,VARCHAR只能接受最大为256个字符串,但是你错误的设置成VARCHAR(512)等一些错误的设置,这些错误的设置会强制MySQL自动将VARCHAR(512)等转换成SMALLINT类型,同时这个字段被设置成primary key ,unique限制或者index索引等,然后执行操作就出现error 1170错误,解决问题的方法,为VARCHAR域指定小于256的长度。

今天在写mysql建表语句的时候,发生了这样的错误:BLOB/TEXT column 'name' used in key specification without a key length。查阅资料后才知道,原来Mysql数据库对于BLOB/TEXT这样类型的数据结构只能索引前N个字符。所以这样的数据类型不能作为主键,也不能是UNIQUE的。所以要换成VARCH,但是VARCHAR类型的大小也不能大于255,当VARCHAR类型的字段大小如果大于255的时候也会转换成小的TEXT来处理。所以也同样有问题。

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论