0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【MySQL 】便利な大文字のテーブル名、カラム名の検索SQL

Posted at

###困った問題

テーブル名やカラムは英小文字でという暗黙のルールがある。
ところが幾つか大文字のテーブル名があるというのだ。
あるデータベース内にどれくらい大文字のテーブル、カラムがあるか調べる事になった。
最初は担当しているプロジェクト分のみで
いい事になっていた。
何と言っても、データベース内には5000を超えるテーブルと50000近いカラムがあったからだ。
参考サイトをもとに、下記のSQLを作ってみた。

###DB内のテーブルとカラムの検索

####1.ダサイけど応用が効きそうなSQL

use information_schema;
select table_name,column_name from columns where table_schema="データベース名"
and (concat(table_name,column_name) like binary "%A%"
or concat(table_name,column_name) like binary "%B%"
or concat(table_name,column_name) like binary "%C%"
or concat(table_name,column_name) like binary "%D%"
or concat(table_name,column_name) like binary "%E%"
or concat(table_name,column_name) like binary "%F%"
or concat(table_name,column_name) like binary "%G%"
or concat(table_name,column_name) like binary "%H%"
or concat(table_name,column_name) like binary "%I%"
or concat(table_name,column_name) like binary "%J%"
or concat(table_name,column_name) like binary "%K%"
or concat(table_name,column_name) like binary "%L%"
or concat(table_name,column_name) like binary "%M%"
or concat(table_name,column_name) like binary "%N%"
or concat(table_name,column_name) like binary "%O%"
or concat(table_name,column_name) like binary "%P%"
or concat(table_name,column_name) like binary "%Q%"
or concat(table_name,column_name) like binary "%R%"
or concat(table_name,column_name) like binary "%S%"
or concat(table_name,column_name) like binary "%T%"
or concat(table_name,column_name) like binary "%U%"
or concat(table_name,column_name) like binary "%V%"
or concat(table_name,column_name) like binary "%W%"
or concat(table_name,column_name) like binary "%X%"
or concat(table_name,column_name) like binary "%Y%"
or concat(table_name,column_name) like binary "%Z%");

####2.スタイリッシュなSQL

use information_schema;
select table_name,column_name from columns where table_schema="データベース名"
and binary concat(table_name,column_name) != binary lower(concat(table_name,column_name))

実際に出力されたのは、2つのテーブルの10カラムのみで、目視では100%発見できない。
この威力は凄い。

###参考URL

MySQLでテーブル一覧とカラム一覧を同時に取ってくる

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?