获取数据库名、表名、字段名

-- 获取所有数据库名
SELECT `SCHEMA_NAME`
FROM `information_schema`.`SCHEMATA`;


-- 获取指定数据库的所有表名
SELECT `TABLE_NAME`
FROM `information_schema`.`TABLES`
WHERE `TABLE_SCHEMA` = '数据库名';


-- 获取指定数据库的所有字段名
SELECT `TABLE_SCHEMA`,
       `TABLE_NAME`,
       `COLUMN_NAME`,
       `DATA_TYPE`,
       `IS_NULLABLE`,
       `COLUMN_DEFAULT`
FROM `information_schema`.`COLUMNS`
WHERE `TABLE_SCHEMA` = '数据库名';


-- 获取指定表的所有字段名
SELECT `TABLE_SCHEMA`,
       `TABLE_NAME`,
       `COLUMN_NAME`,
       `DATA_TYPE`,
       `IS_NULLABLE`,
       `COLUMN_DEFAULT`
FROM `information_schema`.`COLUMNS`
WHERE `TABLE_SCHEMA` = '数据库名'
  AND `TABLE_NAME` = '表名';

Copyright © 2025 码农人生. All Rights Reserved