当前位置:  开发笔记 > 后端 > 正文

在MySQL中获取表元数据

如何解决《在MySQL中获取表元数据》经验,为你挑选了3个好方法。

我试图找出如何从MySQL 5.0中的表中获取以下约束信息:

首要的关键

外键和表引用

独特的列

查询或查询的语法是什么?我有一种感觉,我很接近这一点,但没有例子.



1> 小智..:

对于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'



2> fluffels..:

该SHOW COLUMNS命令将显示一个表的主键和唯一列.

至于外键,你可以使用类似SHOW CREATE TABLE命令的东西,它将输出复制表所需的DDL语句.



3> jx12345..:

使用

 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]与你的价值观

推荐阅读
coco2冰冰
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有