15

Is there a mySQL query to search all tables within a database?

If not can you search all tables within a database from the mySQL workbench GUI?

From phpmyadmin there's a search panel you can use to select all tables to search through. I find this super effective since magento, the ecommerce package I'm working with has hundreds of tables and different product details are in different tables.

enter image description here

user784637
  • 15,392
  • 32
  • 93
  • 156
  • 2
    Possible duplicate of [Search in all fields from every table of a MySQL database](http://stackoverflow.com/questions/639531/search-in-all-fields-from-every-table-of-a-mysql-database) – Steve Chambers Sep 19 '16 at 14:00
  • I recommend add line SET sql_notes = 0$$ before DROP PROCEDURE IF EXISTS get_table $$ Otherwise it will show warning `PROCEDURE` does not exists. –  May 25 '18 at 06:11

5 Answers5

17

Alternatively, if your database is not that huge, you can make a dump and make your search in the .sql generated file.

ôkio
  • 1,772
  • 1
  • 15
  • 16
  • This indeed was my preferred way, directly from command line (so not logged in using sql> prompt). More on this here: https://dev.mysql.com/doc/refman/8.0/en/mysqldump-sql-format.html And then you can easily search using grep "search terms" ~/backup.sql – moojen Sep 12 '19 at 14:32
14

If you are using MySQL Workbench, you can do this by doing right click on the DB Schema you want to search into, and then "Search Table Data...".

In there you can select the "Search using REXEXP" option, and then type your text of search as usual. It will provide the DB rows matching your specific text.

You will need to check the "Search columns of all types" box as well.

evaldeslacasa
  • 582
  • 6
  • 17
13

If you want to do it purely in MySQL, without the help of any programming language, you could use this:

## Table for storing resultant output

CREATE TABLE `temp_details` (
 `t_schema` varchar(45) NOT NULL,
 `t_table` varchar(45) NOT NULL,
 `t_field` varchar(45) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

## Procedure for search in all fields of all databases
DELIMITER $$
#Script to loop through all tables using Information_Schema
DROP PROCEDURE IF EXISTS get_table $$
CREATE PROCEDURE get_table(in_search varchar(50))
 READS SQL DATA
BEGIN
 DECLARE trunc_cmd VARCHAR(50);
 DECLARE search_string VARCHAR(250);

 DECLARE db,tbl,clmn CHAR(50);
 DECLARE done INT DEFAULT 0;
 DECLARE COUNTER INT;

 DECLARE table_cur CURSOR FOR
 SELECT concat('SELECT COUNT(*) INTO @CNT_VALUE FROM `',table_schema,'`.`',table_name,'` WHERE `', column_name,'` REGEXP ''',in_search,''';')
 ,table_schema,table_name,column_name
 FROM information_schema.COLUMNS
 WHERE TABLE_SCHEMA NOT IN ('information_schema','test','mysql');

 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

 #Truncating table for refill the data for new search.
 PREPARE trunc_cmd FROM "TRUNCATE TABLE temp_details;";
 EXECUTE trunc_cmd ;

 OPEN table_cur;
 table_loop:LOOP
 FETCH table_cur INTO search_string,db,tbl,clmn;

 #Executing the search
 SET @search_string = search_string;
 SELECT search_string;
 PREPARE search_string FROM @search_string;
 EXECUTE search_string;


 SET COUNTER = @CNT_VALUE;
 SELECT COUNTER;

 IF COUNTER>0 THEN
 # Inserting required results from search to table
 INSERT INTO temp_details VALUES(db,tbl,clmn);
 END IF;

 IF done=1 THEN
 LEAVE table_loop;
 END IF;
 END LOOP;
 CLOSE table_cur;

 #Finally Show Results
 SELECT * FROM temp_details;
END $$
DELIMITER ;

Source: http://forge.mysql.com/tools/tool.php?id=232

entropid
  • 6,130
  • 5
  • 32
  • 45
  • Old post but I was looking for something like this too but have not used MySQL functions or procedures before to any extent. The OP was looking for something to search all tables within a database but this procedure seems to search all databases. How can it be limited to just the one in which it resides? – DonP Jan 20 '18 at 18:34
  • It works little different, bacause it returns array with search_string = query (query in LOOP). But when I comment lines: SELECT search_string; and SELECT COUNTER; , then it works well as expected and procedure return right content. Its maybe due DB version, my is MariaDB 10.1.31. – step May 28 '18 at 09:54
0

In MySQL Workbench you can use the Table Data Search feature. It can search across multiple tables and/or multiple databases.

Philip Olson
  • 4,662
  • 1
  • 24
  • 20
0

Search string in all tables on a database is a complex task. Normally you don't need to use exactly all tables and results are complex to read without a specific layout (tree of tables with matches or the like)

SQL Workbench/J offers a GUI and a command-line version to do such task:

More info:

NOTE: Search with JDBC driver uses a lot of memory if it is not configured properly. SQL Workbench/J warns about that and although online documentation is a bit outdated, the sources of documentation (doc/xml/db-problems.xml) explain how to fix it for different BBDD:

Here an extract for Postgres:

The PostgreSQL JDBC driver defaults to buffer the results obtained from the database in memory before returning them to the application. This means that when retrieving data, &wb-productname; uses (for a short amount of time) twice as much memory as really needed. This also means that WbExport or WbCopy will effectively read the entire result into memory before writing it into the output file. For large exports, this is usually not wanted. This behavior of the driver can be changed so that the driver uses cursor based retrieval. To do this, the connection profile must disable the "Autocommit" option and must define a default fetch size that is greater than zero. A recommended value is e.g. 10, it might be that higher numbers give a better performance. The number defined for the fetch size, defines the number of rows the driver keeps in its internal buffer before requesting more rows from the backend.

Andhi Irawan
  • 456
  • 8
  • 15
albfan
  • 12,542
  • 4
  • 61
  • 80