如何在mysql表中获取列的大小

时间:2014-02-14 06:22:22

标签: mysql sql

我试图找出表格中一列的确切大小。 PHPMyAdmin不显示列的大小,只显示表的大小。

我能以任何方式获得列的大小吗?

三江源

3 个答案:

答案 0 :(得分:19)

如果您想从table = TABLE_NAME中找出column = COLUMN_NAME的大小,您可以随时运行如下查询:

SELECT sum(char_length(COLUMN_NAME)) 
FROM TABLE_NAME;

返回的大小以字节为单位。如果你想要它在kb,你可以把它除以1024,如下:

SELECT sum(char_length(COLUMN_NAME))/1024 
FROM TABLE_NAME;

答案 1 :(得分:15)

SELECT column_name, 
       character_maximum_length 
FROM   information_schema.columns 
WHERE  table_schema = Database() 
       AND -- name of your database 
       table_name = 'turno' 
       AND -- name of your table 
       column_name = 'nombreTurno' -- name of the column 

INFORMATION_SCHEMA.COLUMNS

如果您想要整个表格大小,请使用此

SELECT table_name                                                 AS "Tables", 
       Round(( ( data_length + index_length ) / 1024 / 1024 ), 2) "Size in MB" 
FROM   information_schema.tables 
WHERE  table_schema = "$db_name" 
ORDER  BY ( data_length + index_length ) DESC; 

修改

SELECT column_name, 
       character_maximum_length 
FROM   information_schema.columns 
WHERE  table_schema = 'websi_db1' 
       AND table_name = 'thread' 
       AND column_name = 'title' 

Source

答案 2 :(得分:3)

如果您是从脚本执行此操作,则可以在建立连接后运行此命令(因此已选择数据库):

SELECT  character_maximum_length
FROM information_schema.columns
WHERE table_name = ? AND column_name = ?

分别用表名和列名替换?。