答案 0 :(得分:13)
对于MySQL:
1)获取表格/字段元数据
SELECT table_schema, table_name, column_name, ordinal_position, data_type,
numeric_precision, column_type, column_default, is_nullable, column_comment
FROM information_schema.columns
WHERE (table_schema='schema_name' and table_name = 'table_name')
order by ordinal_position;
或强>
show fields from 'table_name'
2)获取Foregn Keys引用表
SELECT `REFERENCED_TABLE_NAME`
FROM `information_schema`.`KEY_COLUMN_USAGE`
WHERE
`TABLE_NAME` = 'table_name' AND
`COLUMN_NAME` = 'Column_Name'
3)获取表的索引(主要和外来)
show keys from `table_name`
5)获取所有索引和引用表
SELECT *
FROM `KEY_COLUMN_USAGE`
WHERE
`TABLE_NAME` = 'table_name' AND
`TABLE_SCHEMA` = 'schema_name'
或强>
SELECT *
FROM `REFERENTIAL_CONSTRAINTS`
WHERE
`TABLE_NAME` = 'table_name' AND
`CONSTRAINT_SCHEMA` = 'schema_name'
6)获取存储过程
SELECT *
FROM `ROUTINES`
WHERE
`ROUTINE_SCHEMA` = 'schema_name'
7)获取TRIGGERS
SELECT *
FROM `TRIGGERS`
WHERE
`TRIGGER_SCHEMA` = 'schema_name'
8)获取活动
SELECT *
FROM `EVENTS`
WHERE
`EVENT_SCHEMA` = 'schema_name'
9)获取VIEWS
SELECT *
FROM `VIEWS`
WHERE
`TABLE_NAME` = 'table_name' AND
`TABLE_SCHEMA` = 'schema_name'
答案 1 :(得分:9)
SHOW COLUMNS命令将显示表的主键和唯一列。
对于外键,可以使用类似SHOW CREATE TABLE命令的内容,它将输出复制表所需的DDL语句。
答案 2 :(得分:8)
使用
show fields from table_name
show keys from table_name
获取主键,外键,唯一等
获取外键引用的表:
SELECT `REFERENCED_TABLE_NAME`
FROM `information_schema`.`KEY_COLUMN_USAGE`
WHERE
`TABLE_NAME` = '[table_containing_foreign_key]' AND
`COLUMN_NAME` = '[foreign_key]'
用您的值替换[table_containing_foreign_key]
和[foreign_key]
答案 3 :(得分:2)
你应该试试看看。 INFORMATION_SCHEMA是某些标准的一部分,在其他数据库中以(大部分)类似的方式支持;应记录此标准 - 您可以查找该文档。
但主要的方法是创建一堆测试表,然后看一下INFORMATION_SCHEMA,看看有什么。
答案 4 :(得分:1)
使用以下命令使用Select Query:
SELECT table_schema, table_name, column_name, ordinal_position, data_type, numeric_precision, column_type FROM information_schema.columns WHERE table_name = '[TABLE_NAME]';