How to find all the tables in MySQL with specific column names in them?

后端 未结 11 1606
天命终不由人
天命终不由人 2020-11-22 11:09

I have 2-3 different column names that I want to look up in the entire DB and list out all tables which have those columns. Any easy script?

11条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 11:37

    In version that do not have information_schema (older versions, or some ndb's) you can dump the table structure and search the column manually.

    mysqldump -h$host -u$user -p$pass --compact --no-data --all-databases > some_file.sql
    

    Now search the column name in some_file.sql using your preferred text editor, or use some nifty awk scripts.


    And a simple sed script to find the column, just replace COLUMN_NAME with your's:

    sed -n '/^USE/{h};/^CREATE/{H;x;s/\nCREATE.*\n/\n/;x};/COLUMN_NAME/{x;p};' 

    You can pipe the dump directly in sed but that's trivial.

提交回复
热议问题