Getting table metadata in MySQL

陌路散爱 提交于 2019-11-30 12:08:11

问题


I'm trying to find out how to get the following constraint information from a table in MySQL 5.0:

  • primary key
  • foreign keys and table references
  • unique columns

What is the syntax of the query or queries to do so? I have a feeling I'm close with this, but there is no example.


回答1:


The SHOW COLUMNS command will show you the primary key and unique columns for a table.

As for foreign keys, you could use something like the SHOW CREATE TABLE command which will output the DDL statements needed to replicate the table.




回答2:


For MySQL:

1) get Table/Fields metadata

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;

OR

show fields from 'table_name' 

2) get Foregn Keys referenced table

SELECT `REFERENCED_TABLE_NAME` 
   FROM `information_schema`.`KEY_COLUMN_USAGE`
   WHERE
       `TABLE_NAME` = 'table_name' AND
       `COLUMN_NAME` = 'Column_Name'

3) get indexes (primary and foreign) for a table

show keys from `table_name`

5) get All indexes and referreced table

SELECT *
  FROM `KEY_COLUMN_USAGE`
  WHERE
      `TABLE_NAME` = 'table_name' AND
      `TABLE_SCHEMA` = 'schema_name'

OR

SELECT *
  FROM `REFERENTIAL_CONSTRAINTS`
  WHERE
      `TABLE_NAME` = 'table_name' AND
      `CONSTRAINT_SCHEMA` = 'schema_name'

6) get STORED PROCEDURES

SELECT * 
  FROM `ROUTINES`
  WHERE
     `ROUTINE_SCHEMA` = 'schema_name'

7) get TRIGGERS

SELECT * 
  FROM `TRIGGERS`
  WHERE 
     `TRIGGER_SCHEMA` = 'schema_name'

8) get EVENTS

SELECT * 
  FROM `EVENTS`
  WHERE 
     `EVENT_SCHEMA` = 'schema_name'

9) get VIEWS

SELECT *
  FROM `VIEWS`
  WHERE
      `TABLE_NAME` = 'table_name' AND
      `TABLE_SCHEMA` = 'schema_name'



回答3:


Use

 show fields from table_name
 show keys from table_name

to get primary keys, foreign keys, unique, etc.

to get the table referenced by a foreign key use:

 SELECT `REFERENCED_TABLE_NAME` 
 FROM `information_schema`.`KEY_COLUMN_USAGE` 
 WHERE 
     `TABLE_NAME` = '[table_containing_foreign_key]' AND 
     `COLUMN_NAME` = '[foreign_key]'

substituting [table_containing_foreign_key] and [foreign_key] with your values




回答4:


You should try it and see. INFORMATION_SCHEMA is part of some standard and is supported in a (mostly) similar way in other databases; this standard should be documented - you can look for that doc.

But mainly the way would be to create a bunch of test tables, and then have a look at INFORMATION_SCHEMA to see what's there.




回答5:


use the following to get the same using 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]';


来源:https://stackoverflow.com/questions/510410/getting-table-metadata-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!