`

mysql #1170错误(42000) BLOB/TEXT Column Used in Key Specification Without a Key Le

 
阅读更多
转自:http://blog.csdn.net/BossDarcy/article/details/6209685
http://blog.csdn.net/ruhaitsia/article/details/5628460

mysql  #1170错误(42000) BLOB/TEXT Column Used in Key Specification  Without  a  Key  Length

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

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的长度。

本文英文地址http://www.mydigitallife.info/2007/07/09/mysql-error-1170-42000-blobtext-column-used-in-key-specification-without-a-key-length/

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

相关推荐

    MySQL使用TEXT/BLOB类型的知识点详解

    一、TEXT和BLOB的区别 TEXT和BLOB家族之间仅有的不同是BLOB类型存储的是二进制数据,没有排序规则或字符集,而TEXT类型有字符集或排序规则。说白了如果要储存中文则选择...ERROR 1101 (42000): BLOB/TEXT column 'a_tex

    MySQL 5.6.25 源码包+安装方法 (mysql-5.6.25.tar.gz)

    mysql-5.6.25.tar 包含两个文件: - mysql-5.6.25.tar.gz Generic Linux (Architecture Independent), Compressed TAR Archive MD5: 37664399c91021abe070faa700ecd0ed -install.txt: 安装方法 首先解压mysql-...

    QT-Mysql-Blob.zip_mysql blob_qt blob 文件_qt blob类型_qt的blob

    mysql+Qt 下对blob数据类型操作

    java.sql.SQLSyntaxErrorException:

    java.sql.SQLSyntaxErrorException: The size of BLOB/TEXT data inserted in one transaction is greater than 10% of redo log size….. 解决方式: 从报错信息看是因为innodb_log_file_size太小,至于原因,在 ...

    Java设计模式学习教程与案例源码.zip

    1. [工厂方法](worthed/OriginBlog/blob/master/articles/FactoryMethodPattern.md) 2. [简单工厂](worthed/OriginBlog/blob/master/articles/SimpleFactoryPattern.md) 3. [抽象工厂](worthed/OriginBlog/blob/...

    从mysql数据库中批量下载Blob图片到本地

    语言Java,数据库mysql 批量获取Blob图片下载到本地,同时设置本地保存位置

    基于 Servlet 开发的房屋发布系统源码+项目说明.zip

    【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。...Java / MySQL / HTML / CSS / JavaScript

    一行代码集成时间选择器

    # LeeDatePickerView 一行代码集成时间选择器 ### Git 地址 ... ### 例图 ![例图1](https://github.com/leeboo741/ImageRepository/blob/master/DatePickerImage/1.png) ... 如果发现什么错误,提交一下错误,我会尽快改正

    mysql导出工具(blob字段)

    mysql导出工具,可导出无注释插入语句,表中字段有blob类数据,插入语句前还有锁表操作语句,适合多种场景

    零基础学习python以及当当网百度新闻豆瓣爬虫项目实战源码.zip

    [example-1.py](/blob/master/examples/example-1.py) python语法基础 2. [example-2.py](/blob/master/examples/example-2.py) python控制流与小实例 3. [example-3.py](/blob/master/examples/example-3.py) ...

    小程序直传oss示例小程序直传oss示例

    小程序直传oss示例小程序直传oss示例小程序直传oss示例小程序直传oss示例小程序直传oss示例小程序直传oss示例小程序直传oss示例小程序直传oss示例小程序直传oss示例小程序直传oss示例小程序直传oss示例小程序直传oss...

    oracle中的BLOB(照片)转换到mysql中

    从oracle中的BLOB类型字段中取出照片,转存到mysql数据库中。思路是现将oracle中的照片存储在本地文件夹,在将本地图片上传到mysql中。

    mysql blob to 向 file转换

    mysql数据库 中用blob存储image及file 数据blob的写入与提取 方法概要

    require.js

    //Not using strict: uneven strict support in browsers, #392, and causes //problems with requirejs.exec()/transpiler plugins that may not be strict. /*jslint regexp: true, nomen: true, sloppy: ...

    crypto和base64可用于小程序oss直传阿里云.zip

    此文件专门为微信小程序使用,使用教程https://blog.csdn.net/weixin_42594068/article/details/116521561 crypto模块的目的是为了提供通用的加密和哈希算法。用纯JavaScript代码实现这些功能不是不可能,但速度会...

    jsp操作mysql数据库的blob字段

    jsp操作mysql数据库的blob字段,实现: 1、将磁盘文件存入mysql数据库; 2、将mysql数据库中存入的文件,读出来,输出给浏览器。

    基于SpringBoot+Mybatis+mysql的毕业生就业信息分析系统源码+项目说明(高分毕设).zip

    基于SpringBoot+Mybatis+mysql的毕业生就业信息分析系统源码+项目说明(高分毕设).zip # 基于SpringBoot Mybatis 的毕业生就业分析系统 --- ## 用到的技术: --- - 使用maven构建项目。 - 使用Springboot ...

    mysql 5.7.3 源码包

    Mysql 源码安装操作: 1. 创建用户 # groupadd mysql # useradd -g -r mysql mysql 2. 解压缩tar包 # tar xf mysql-5.7.3-m13.tar.gz # cd mysql-5.7.3-m13 3. 编译安装 # cmake . -DCMAKE_INSTALL_PREFIX=/usr/...

    MySQL中文参考手册.chm

    <br/>1.6 顺应2000年 <br/>1.7 SQL一般信息和教程 <br/>1.8 有用的MySQL相关链接 <br/>2 MySQL 邮件列表及如何提问或报告错误<br/>2.1 MySQL邮件列表 <br/>2.2 提问或报告错误 <br/>2.3 怎样报告错误或问题 <br/>...

Global site tag (gtag.js) - Google Analytics