MySQL到底能有多少個字段

今天技術討論羣裏 「一切隨遇而安」同窗看書時出現一個疑問,一個MySQL的表中到底能夠有多少個字段?帶着這個疑問,咱們展開了探討,也接着討論了一個單字段長度的問題。python

1.  官方文檔說明

官方文檔的內容以下,主要意思是字段個數限制達不到理想的4096個,且和字段類型有關,innodb引擎的字段上限是1017,。mysql

 2.  測試表字段數限制

2.1  測試innodb引擎表

因官方文檔介紹了innodb表字段限制是1017,所以能夠寫程序進行模擬。思路以下:sql

a) 建立一張1個 char(1) 類型的innodb表測試

b)   循環往該表新增字段 直至報錯編碼

我使用的是python 腳本進行測試,腳本以下:spa

#!/usr/bin/python
# coding=utf-8
import pymysql as mdb
import os

sor_conn = mdb.connect(host='127.0.0.1',port=3306,user='root',passwd='123456')
sor_cur = sor_conn.cursor()

v_sql_d = "drop table  if exists test.test_c ;"   # 爲了程序重複執行,添加判斷

sor_cur.execute(v_sql_d)
sor_conn.commit()
v_sql_c = "create table test.test_c(c1 char(1))  engine=innodb;"
sor_cur.execute(v_sql_c)
sor_conn.commit()
v_id=2
while v_id<50000:
        v_sql_add_c = " alter table test.test_c  add c%d char(1);"%(v_id)

        try:
                sor_cur.execute(v_sql_add_c)
                sor_conn.commit()
        except mdb.Error,e:
                v_cnt = v_id - 1
                print "Mysql Error %d: %s" % (e.args[0], e.args[1])
                print "MySQL has a limit of %d" %(v_cnt)
                break
        v_id = v_id + 1
sor_conn.close()

運行結果以下:3d

[root@testdb python_pro]# python test_column.py 
Mysql Error 1117: Too many columns
MySQL has a limit of 1017

在SQLyog客戶端手動驗證也是一樣的結果
 

 

所以,官方文檔中介紹的MySQL innodb引擎表最多有1017個字段。code

 

 

2.2  測試MYISAM引擎表

由於MySQL中另外一種MYISAM引擎的表在MySQL5.7版本以前也是很是重要的存儲引擎,只是後續版本使用愈來愈少,可是 仍是有必要測試一番。blog

程序思路與測試innodb是均一致,只是將表的引擎進行修改,以下:utf-8

#!/usr/bin/python
# coding=utf-8
import pymysql as mdb
import os
import datetime
import time

sor_conn = mdb.connect(host='127.0.0.1',port=3306,user='root',passwd='123456')
sor_cur = sor_conn.cursor()

v_sql_d = "drop table  if exists test.test_c ;"

sor_cur.execute(v_sql_d)
sor_conn.commit()
v_sql_c = "create table test.test_c(c1 char(1))engine=MYISAM ;"
sor_cur.execute(v_sql_c)
sor_conn.commit()
v_id=2
while v_id<50000:
        v_sql_add_c = " alter table test.test_c  add c%d char(1);"%(v_id)

        try:
                sor_cur.execute(v_sql_add_c)
                sor_conn.commit()
        except mdb.Error,e:
                v_cnt = v_id - 1
                print "Mysql Error %d: %s" % (e.args[0], e.args[1])
                print "MySQL has a limit of %d" %(v_cnt)
                break
        v_id = v_id + 1
sor_conn.close()

運行結果以下:

[root@testdb python_pro]# python test_column.py 
Mysql Error 1117: Too many columns
MySQL has a limit of 2598

也就是說MySQL中MyISAM引擎表最多能夠存2598個字段。

 

3.  測試字段長度限制

你們都知道的一個知識是在MySQL中一行除了blob及text類的大字段以外,其他字段的長度之和不能超過65535,那麼這個是肯定的麼,所以再次作一次測試。

3.1  測試UTF8字符集

建立一個只有一個字段的表,字段長度爲65535 結果竟然報錯了,提示最大長度只能是21845,也就是65535/3的量,

/*  測試單字段長度 上限*/
CREATE  TABLE  test_c1(
c1 VARCHAR(65535)
) ENGINE=INNODB CHARACTER SET utf8;
/* 執行結果 */
錯誤代碼: 1074
Column length too big for column 'c1' (max = 21845); use BLOB or TEXT instead

可是改成21845依舊報錯,緣由你仔細品(提示varchar)

CREATE  TABLE  test_c1(
c1  VARCHAR(21845) 
) ENGINE=INNODB CHARACTER SET utf8;

/* 執行結果依舊報錯 */
錯誤代碼: 1118
Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

那,在減少一位試試

CREATE  TABLE  test_c1(
c1  VARCHAR(21844) 
) ENGINE=INNODB CHARACTER SET utf8;
/* 終於成功了*/
查詢:create table test_c1( c1 varchar(21844) ) engine=innodb character set utf8

共 0 行受到影響

有圖有真相

 

 3.2  測試latin字符集

由於utf8編碼佔3位,所以最大長度只能是21845(-1),那麼latin字符集是否是就能達到65535了

測試以下

CREATE  TABLE  test_c1(
c1  VARCHAR(65535) 
) ENGINE=INNODB CHARACTER SET latin1
/* 結果依舊失望 */
錯誤代碼: 1118
Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

在想一想上面的狀況,一直減下去,發現65532便可正常(緣由你繼續品就明白了)

CREATE  TABLE  test_c1(
c1  VARCHAR(65532) 
) ENGINE=INNODB CHARACTER SET latin1;
/* 終於成功了 */
<n>查詢:create table test_c1( c1 varchar(65532) ) engine=innodb character set latin1

共 0 行受到影響

給真相

 

3. 小結

實踐出真知,任何人說的知識點都要思考,必要的時候本身檢驗一番。

表字段限制

 

表字段長度限制

 

 在此知識給個匆忙的小結,其中緣由不懂的能夠查看官方文檔,也是詳細的測試,也能夠加羣一塊兒討論。

相關文章
相關標籤/搜索