Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting table metadata in MySQL

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.

like image 468
VirtuosiMedia Avatar asked Feb 04 '09 07:02

VirtuosiMedia


People also ask

How do I find the metadata of a SQL database?

T-SQL DB_ID() metadata function databases and sys. sysdatabases table with a brand new Id and other relevant information like version, filename etc. There is a metadata function, DB_ID() that helps to retrieve this number.

Where is the metadata stored in MySQL?

To satisfy both requirements, MySQL stores metadata in a Unicode character set, namely UTF-8. This does not cause any disruption if you never use accented or non-Latin characters. But if you do, you should be aware that metadata is in UTF-8.


3 Answers

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'
like image 157
Nelson Santos Avatar answered Sep 25 '22 19:09

Nelson Santos


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.

like image 29
fluffels Avatar answered Sep 22 '22 19:09

fluffels


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

like image 38
jx12345 Avatar answered Sep 25 '22 19:09

jx12345