iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📝

Frequently Used but Always Forgotten: MySQL INFORMATION_SCHEMA Search Queries

に公開

I often search using MySQL's INFORMATION_SCHEMA.COLUMNS and similar, but I can never remember them, so here is a memo out of frustration.

No. 1: Search for tables with similar column names

-- Search for tables with similar column names
SELECT
  table_name
, column_name
FROM
  INFORMATION_SCHEMA.COLUMNS 
WHERE
    COLUMN_NAME like "%hoge%" -- Column name to search for. LIKE is also possible
AND TABLE_SCHEMA = "myDatabaseName" -- Target database name
; 

No. 2: Search for tables with similar table names

-- Search for tables with similar table names
SELECT
    *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='myDatabaseName' and TABLE_NAME like '%fuga%'

No. 3: Query to find tables with large capacity

-- Query to find tables with large capacity
SELECT  
    table_name, engine, table_rows AS tbl_rows,
    avg_row_length AS rlen,  
    floor((data_length+index_length)/1024/1024) AS `TotalCapacityMb`,
    floor((data_length)/1024/1024) AS `DataCapacityMb`,
    floor((index_length)/1024/1024) AS `IndexCapacityMb`
FROM 
    INFORMATION_SCHEMA.TABLES 
WHERE
    table_schema=database()  
ORDER BY
    (data_length+index_length) DESC;  

Discussion