我试图找出如何从MySQL 5.0中的表中获取以下约束信息:
首要的关键
外键和表引用
独特的列
查询或查询的语法是什么?我有一种感觉,我很接近这一点,但没有例子.
对于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)获得STORED PROCEDURES
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'
该SHOW COLUMNS命令将显示一个表的主键和唯一列.
至于外键,你可以使用类似SHOW CREATE TABLE命令的东西,它将输出复制表所需的DDL语句.
使用
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]
与你的价值观