基本上我想从当前连接的数据库中获取表名和每个表的字段名,没有别的。
这可能吗?
我知道SHOW TABLES FROM my_database
会为您提供表名,而SHOW COLUMNS FROM my_table
会为您提供字段,但这至少是[1 x#个表]查询,我可以获得更多我想要的信息: )
答案 0 :(得分:16)
INFORMATION_SCHEMA.COLUMNS表符合您的要求。
SELECT table_name, column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'YourDBName'
ORDER BY table_name, ordinal_position
答案 1 :(得分:4)
SELECT *
FROM information_schema.tables t
JOIN information_schema.columns c ON t.TABLE_NAME = c.TABLE_NAME
AND t.TABLE_CATALOG=c.TABLE_CATALOG
AND t.TABLE_SCHEMA=c.TABLE_SCHEMA
适用于SQLSERVER 2005. MySQL的列名可能不同(我假设你正在使用它),但概念是相同的。
答案 2 :(得分:4)
SELECT t.name AS tblName,
SCHEMA_NAME(schema_id) AS [schemaName],
c.name AS colName
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
ORDER BY tblName;
答案 3 :(得分:1)
“显示数据库”,“显示表格”和“描述表格”是MySql中我所知道的最好,最快的方法。
但他们 特定于MySql。
如果你想:
a)查询数据库架构的可移植方式
... AND ...
b)对查询进行更精细的控制,然后查看INFORMATION_SCHEMA:
http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
语法:
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name'
[AND table_name LIKE 'wild']
答案 4 :(得分:0)
经过几次试验后,我创建了这个sql代码来查看表格中的列。
SELECT
TABLE_NAME as table_name,
COLUMN_NAME as column_name,
COLUMN_TYPE as data_type,
COLUMN_DEFAULT as default_value,
IS_NULLABLE as nullable,
COLUMN_KEY as constraints,
EXTRA as constraints2,
CHARACTER_SET_NAME as charset,
COLLATION_NAME as collation
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = '<YOUR_DATABASE_NAME>'
AND TABLE_NAME='<YOUR_TABLE_NAME>'
ORDER BY table_name, ordinal_position