Check table columns reached max length

亡梦爱人 提交于 2020-01-24 19:47:30

问题


Just wondering whether there is a way to check columns of a table reached thire maximum length.

With following I was able to get the length of the table column

SELECT MAX(CHARACTER_MAXIMUM_LENGTH) AS LEN, COLUMN_NAME
FROM information_schema.columns
WHERE table_name IN ('TableA')
GROUP BY COLUMN_NAME 
ORDER BY LEN DESC

And then I wanted to check whether those column reached its MAX length or not with data

Below gives the max length a column

SELECT MAX(LENGTH(Col1))
FROM TableA

As it is not possible to use aggregate functions in WHERE clause, how it is possible?

Thanks


回答1:


You can use join to bring the data together:

SELECT MaxLen, ActualLen
FROM (SELECT MAX(CHARACTER_MAXIMUM_LENGTH) AS MaxLen, COLUMN_NAME
      FROM information_schema.columns
      WHERE table_name IN ('TableA') AND COLUMN_NAME = 'COL1'
     ) m CROSS JOIN
     (SELECT MAX(LENGTH(Col1)) as ActualLen
      FROM TableA
     ) c;

I'm not sure what you actually want returned. If you want the rows with the maximum value:

select a.*
from tablea a
where length(col1) = (SELECT MAX(CHARACTER_MAXIMUM_LENGTH) AS MaxLen
                      FROM information_schema.columns
                      WHERE table_name IN ('TableA') AND COLUMN_NAME = 'COL1'
                     );


来源:https://stackoverflow.com/questions/28672731/check-table-columns-reached-max-length

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!