mysqlクエリーテーブルのフィールド/カラム名;クエリー・フィールド/カラム名が存在するテーブル

2994 ワード

クエリー・テーブルのフィールド/カラム名
  • showの方法で
  • SHOW COLUMNS from database_name.table_name;

    mysql> show columns from actor;
    +-------------+----------------------+------+-----+-------------------+-----------------------------+
    | Field       | Type                 | Null | Key | Default           | Extra                       |
    +-------------+----------------------+------+-----+-------------------+-----------------------------+
    | actor_id    | smallint(5) unsigned | NO   | PRI | NULL              | auto_increment              |
    | first_name  | varchar(45)          | NO   |     | NULL              |                             |
    | last_name   | varchar(45)          | NO   | MUL | NULL              |                             |
    | last_update | timestamp            | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
    +-------------+----------------------+------+-----+-------------------+-----------------------------+
    4 rows in set (0.00 sec)
  • 多くの場合、カラム名、すなわち上記の例のfieldをクエリーするだけで、具体的な情報は必要ありません.
  • SELECT
        COLUMN_NAME
    FROM
        information_schema.COLUMNS
    WHERE
        table_schema = 'database_name'
        AND table_name = 'table_name';

    mysql> select column_name from information_schema.COLUMNS
        -> where table_schema = 'sakila' and table_name = 'actor';
    +-------------+
    | COLUMN_NAME |
    +-------------+
    | actor_id    |
    | first_name  |
    | last_name   |
    | last_update |
    +-------------+
    4 rows in set (0.00 sec)
    
    

    クエリー・フィールド/カラム名が存在するテーブル
    依然としてinformation_を使用schemaというデータベースのCOLUMNSという表.
    SELECT 
        TABLE_NAME 
    FROM
        information_schema.COLUMNS
    WHERE
        column_name = 'XXXX';

    クエリー範囲はノード上のすべてのdatabaseであり、データベースを指定すると、上記のようにwhere条件にtable_を追加します.schemaの内容.
    --        
    mysql> select table_name, table_schema  from information_schema.columns where column_name = 'id';
    +----------------------+--------------------+
    | TABLE_NAME           | TABLE_SCHEMA       |
    +----------------------+--------------------+
    | COLLATIONS           | information_schema |
    | INNODB_FOREIGN       | information_schema |
    | INNODB_FOREIGN_COLS  | information_schema |
    | PROCESSLIST          | information_schema |
    | slave_relay_log_info | mysql              |
    | slave_worker_info    | mysql              |
    | customer_list        | sakila             |
    | staff_list           | sakila             |
    | employee             | test               |
    | test_alter           | test               |
    +----------------------+--------------------+
    10 rows in set (0.01 sec)
    
    --      
    mysql> select table_name from information_schema.columns 
        -> where column_name = 'id' and table_schema = 'sakila' ;
    +---------------+
    | TABLE_NAME    |
    +---------------+
    | customer_list |
    | staff_list    |
    +---------------+
    2 rows in set (0.00 sec)

     
    以上です.