sql >> Base de Datos >  >> RDS >> Mysql

Obtener metadatos de tablas en MySQL

Para MySQL:

1) obtener metadatos de tablas/campos

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;

O

show fields from 'table_name' 

2) obtener la tabla de referencia de claves foráneas

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

3) obtener índices (primarios y externos) para una tabla

show keys from `table_name`

5) obtener todos los índices y la tabla de referencia

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

O

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

6) obtener PROCEDIMIENTOS ALMACENADOS

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

7) obtener DISPARADORES

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

8) obtener EVENTOS

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

9) obtener VISTAS

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