当前位置:网站首页>(transfer) mysql: error 1071 (42000): specified key was too long; max key length is 767 bytes
(transfer) mysql: error 1071 (42000): specified key was too long; max key length is 767 bytes
2022-06-29 09:13:00 【Dream condenses zhe Xue】
Today's test encountered such a problem :
mysql> use test; create table test(a varchar(512) primary key, b varchar(1024));
Database changed
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
Related tests :
http://kklinux.com/bbs/archiver/tid-2543.html
drop table if exists test;
create table test(test varchar(767) primary key)charset=latin5;
-- success
drop table if exists test;
create table test(test varchar(768) primary key)charset=latin5;
-- error
-- ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
drop table if exists test;
create table test(test varchar(383) primary key)charset=GBK;
-- success
drop table if exists test;
create table test(test varchar(384) primary key)charset=GBK;
-- error
-- ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
drop table if exists test;
create table test(test varchar(255) primary key)charset=UTF8;
-- success
drop table if exists test;
create table test(test varchar(256) primary key)charset=UTF8;
-- error
-- ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
reason :
MySQL Of varchar The primary key only supports no more than 768 Bytes perhaps 768/2=384 Two bytes perhaps 768/3=256 Three byte fields
and GBK It's double byte ,UTF-8 It's three bytes .
Variable is set :
mysql> show variables like '%char%';
+--------------------------+--------------------------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /opt/schooner/ac_3.1/mysql/share/mysql/charsets/ |
+--------------------------+--------------------------------------------------+
8 rows in set (0.00 sec)
So what are the rules of the character set ? Find an article like this :
http://www.blogjava.net/wldandan/archive/2007/09/04/142669.html
Some time ago , Has been mysql The character set of , Today, I would like to sum up the knowledge in this regard .
MySQL The character set of supports (Character Set Support) There are two aspects :
Character set (Character set) And sort them (Collation).
Support for character sets is refined to four levels :
The server (server), database (database), Data sheet (table) And connections (connection).
1.MySQL Default character set
MySQL The specification of character set can be refined to a database , A watch , A column of , What character set should be used .
however , Conventional The program does not use such complex configuration when creating databases and data tables , They use the default configuration , that , Where does the default configuration come from ?
(1) compile MySQL when , Specifies a default character set , This character set is latin1;
(2) install MySQL when , Can be found in the configuration file (my.ini) Specify a default character set in , If not specified , This value inherits from the value specified at compile time ;
(3) start-up mysqld when , You can specify a default character set in the command line arguments , If not specified , This value is inherited from the configuration file , here character_set_server Set to the default character set ;
(4) When creating a new database , Unless otherwise specified , The character set of this database is set to... By default character_set_server ;
(5) When a database is selected ,character_set_database Set as the default character set for this database ;
(6) When creating a table in this database , The default character set of the table is set to character_set_database , The default character set of this database ;
(7) When setting a column in a table , Unless otherwise specified , Otherwise, the default character set of this column is the default character set of the table ;
Just to summarize , If nothing is changed , Then all fields of all tables in all databases are used latin1 Storage , But if we install MySQL, It's common to choose multilingual support , in other words , The setup program will automatically put... In the configuration file default_character_set Set to UTF-8, This ensures that by default , All fields of all tables in all databases use UTF-8 Storage .
2. View the default character set ( By default ,mysql The character set of is latin1(ISO_8859_1)
Usually , To view the character set and sorting mode of the system, you can use the following two commands :
mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | D:"mysql-5.0.37"share"charsets" |
+--------------------------+---------------------------------+
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3. Modify the default character set
(1) The easiest way to modify it , Is to modify mysql Of my.ini Character set key values in the file ,
Such as default-character-set = utf8
character_set_server = utf8
After modification , restart mysql Service for ,service mysql restart
Use mysql> SHOW VARIABLES LIKE 'character%'; see , Found that the database code has been changed to utf8
+--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | D:"mysql-5.0.37"share"charsets" |
+--------------------------+---------------------------------+
(2) There is also a way to modify the character set , Is the use of mysql The order of
mysql> SET character_set_client = utf8 ;
mysql> SET character_set_connection = utf8 ;
mysql> SET character_set_database = utf8 ;
mysql> SET character_set_results = utf8 ;
mysql> SET character_set_server = utf8 ;
mysql> SET collation_connection = utf8 ;
mysql> SET collation_database = utf8 ;
mysql> SET collation_server = utf8 ;
Generally, even if the default character set of the table is set to utf8 And through UTF-8 Send query by encoding , You will find that the code stored in the database is still garbled . That's the problem connection On connection layer . The solution is to execute the following sentence before sending the query :
SET NAMES 'utf8';
It is equivalent to the following three sentences of instructions :
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
summary :
therefore , What database version to use , Whether it's 3.x, still 4.0.x still 4.1.x, It doesn't really matter to us , There are two important things :
1) Set the database code correctly .MySQL4.0 The following versions of the character set always default ISO8859-1,MySQL4.1 When installing, you will be given the option of . If you are going to use UTF- 8, When you create a database, you must specify UTF-8( You can also change it after you create it ,4.1 The above versions can also specify the character set of the table separately )
2) Set the database correctly connection code . After setting the database code , When connecting to the database , You should specify connection The coding , For example, use jdbc When the connection , Specify the connection as utf8 The way .
边栏推荐
- Uniapp wechat applet reports an error typeerror: cannot read property 'call' of undefined
- MySQL row column conversion example
- Using scan in redistemplate
- 修改exif信息
- Figure timers in SOC (I): what timers are there in the system?
- Product manager certification enrollment brochure (NPDP) in July 2022
- 实例报错IOPub data rate exceeded
- 记微信小程序分享代码片段
- verilog 移位操作符
- Let's make a summary
猜你喜欢

Heavyweight released "FISCO bcos application landing guide"

Unity C# 网络学习(十二)——Protobuf生成协议

Activemq消息组件发布订阅ReDelivery消息重新投递

Compare homekit, MI family, and zhiting family cloud edition for what scene based experiences

微信小程序判断url的文件格式

闭关修炼(二十四)浅入了解跨域问题

手写 redux-thunk

实例报错IOPub data rate exceeded

First electric shock, so you are such a dragon lizard community | dragon lizard developer said that issue 8

图片格式-webp
随机推荐
cokkie和session的区别
Working for many years, recalling life -- three years in high school
(III) encoder self attention mask
ThinkPHP 6 使用 mongoDB
什么是超融合?与传统架构有什么区别?.
JS获取图片或base64的宽高等基本信息
闭关修炼(二十一)Servlet生命周期、service方法源码分析、线程安全问题
The final of the sixth season of 2022 perfect children's model Hefei division came to a successful conclusion
Verilog reduction operator
随心玩玩(三)Mirai框架QQ机器人
verilog 移位操作符
Is it safe for the top ten securities companies to open accounts? Is it reliable?
PAT (Basic Level) Practice (中文)1003 我要通过! (20分) C语言实现
微信小程序跳转公众号图文内容
MySQL virtual column
(转)MySQL: ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
Development tips - Image Resource Management
cmd进入虚拟机
jar包和war包
2022年7月产品经理认证招生简章(NPDP)