necesito una consulta sql que me muestra uno o todos los campos de una tabla que son llave foranea preferiblemente que funcione con mysql ya intente con esto SELECT *FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = 'NombreTabla'
y no funciona
1 respuesta
SELECT table_name,
column_name,
constraint_name,
referenced_table_name,
referenced_column_name
FROM information_schema.key_column_usage
WHERE referenced_table_name = 'tablaNombre';
Por ejemplo con la database de ejemplo Sakila de MySQL:
SELECT table_name,
column_name,
constraint_name,
referenced_table_name,
referenced_column_name
FROM information_schema.key_column_usage
WHERE referenced_table_name = 'actor';
Resultado:
+------------+-------------+---------------------+-----------------------+------------------------+
| TABLE_NAME | COLUMN_NAME | CONSTRAINT_NAME | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+------------+-------------+---------------------+-----------------------+------------------------+
| film_actor | actor_id | fk_film_actor_actor | actor | actor_id |
+------------+-------------+---------------------+-----------------------+------------------------+
Saludos ✌