Obtener nombres de tabla usando la instrucción SELECT en MySQL


En MySQL, sé que puedo listar las tablas en una base de datos con:

SHOW TABLES

Sin Embargo, quiero insertar estos nombres de tabla en otra tabla, por ejemplo:

INSERT INTO metadata(table_name) SHOW TABLES /* does not work */

Hay una manera de obtener los nombres de las tablas usando una instrucción SELECT estándar, algo así como:

INSERT INTO metadata(table_name) SELECT name FROM table_names /* what should table_names be? */
 178
Author: Mark Robinson, 2011-12-01

11 answers

Para obtener el nombre de todas las tablas use:

SELECT table_name FROM information_schema.tables;

Para obtener el nombre de las tablas de una base de datos específica use:

SELECT table_name FROM information_schema.tables where table_schema='your_database_name';

Ahora, para responder a la pregunta original, use esta consulta:

INSERT INTO table_name
    SELECT table_name FROM information_schema.tables
        WHERE table_schema = 'your_database_name';

Para más detalles ver: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html

 225
Author: Murilo Garcia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-19 00:53:15

Intenta:

select * from information_schema.tables

Véase: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html

 138
Author: Nthalk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-23 13:07:54

Si tenemos varias bases de datos y necesitamos seleccionar todas las tablas para una base de datos en particular, podemos usar TABLE_SCHEMA para definir el nombre de la base de datos como:

select table_name from information_schema.tables where TABLE_SCHEMA='dbname';

 16
Author: Abs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-02 16:38:47

Además de usar la tabla INFORMATION_SCHEMA, para usar MOSTRAR TABLAS para insertar en una tabla, usaría lo siguiente

<?php
 $sql = "SHOW TABLES FROM $dbname";
 $result = mysql_query($sql);
 $arrayCount = 0
 while ($row = mysql_fetch_row($result)) {
  $tableNames[$arrayCount] = $row[0];
  $arrayCount++; //only do this to make sure it starts at index 0
 }
 foreach ($tableNames as &$name {
  $query = "INSERT INTO metadata (table_name) VALUES ('".$name."')";
  mysql_query($query);
 }
?>
 11
Author: James Williams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-30 23:51:15

Echa un vistazo a la tabla TABLES en la base de datos information_schema. Contiene información sobre las tablas en sus otras bases de datos. Pero si está en alojamiento compartido, probablemente no tenga acceso a él.

 8
Author: GolezTrol,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-30 23:30:20

Creo que puede obtener los datos que desea de las TABLAS INFORMATION_SCHEMA.

Puedes encontrar más información aquí: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html

 6
Author: Andrey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-30 23:31:26
SELECT table_name 
FROM information_schema.tables 
WHERE table_schema = 'DATABASE'
 3
Author: Steven Soroka,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-09 16:22:26

MySQL INFORMATION_SCHEMA.TABLES la tabla contiene datos sobre ambas tablas (no temporales sino permanentes) y vistas. La columna TABLE_TYPE define si se trata de un registro para tabla o vista (para tablas TABLE_TYPE='BASE TABLE' y para vistas TABLE_TYPE='VIEW'). Por lo tanto, si desea ver desde sus tablas de esquema (base de datos) solo hay la siguiente consulta:

SELECT *
FROM information_schema.tables
WHERE table_type='BASE TABLE'
AND table_schema='myschema'
 3
Author: sbrbot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-09 16:23:17

Hay otra forma más sencilla de obtener nombres de tabla

SHOW TABLES FROM <database_name>
 0
Author: Smith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-10 01:27:44

Creo que puede ser útil señalar que si desea seleccionar tablas que contienen palabras específicas, puede hacerlo fácilmente utilizando SELECT (en lugar de SHOW). La siguiente consulta reduce fácilmente la búsqueda a tablas que contienen "palabra clave"

SELECT *
FROM information_schema.tables
WHERE table_name like "%keyword%"
 0
Author: Robert Sinclair,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-09 16:26:33

Para insertar, actualizar y eliminar haga lo siguiente:

$teste = array('LOW_PRIORITY', 'DELAYED', 'HIGH_PRIORITY', 'IGNORE', 'INTO', 'INSERT', 'UPDATE', 'DELETE', 'QUICK', 'FROM');
$teste1 = array("\t", "\n", "\r", "\0", "\x0B");
$strsql = trim(str_ireplace($teste1, ' ', str_ireplace($teste, '', $strsql)));
$nomeTabela = substr($strsql, 0, strpos($strsql, ' '));

print($nomeTabela);
exit;
 -3
Author: Eduardo Krp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-31 00:20:04