How to list a sample of all tables in a Mysql database?

Asked

Viewed 4,598 times

4

I need to sample all tables in a Mysql database, for example through a SELECT * ... LIMIT 10.

I already have the code that returns all tables of the current database:

select table_name from information_schema.tables where table_schema = database();

The following (incorrect) query illustrates what I wish to do:

Select * from ( 
    select table_name from information_schema.tables where table_schema = database()
 ) limit 10;

, which would function as:

Select * from tabela1 limit 10;
Select * from tabela2 limit 10;
...
Select * from tabelaN limit 10;

How can I do this in a single query?

I found examples that speak of cursors, but I’m not sure how I could apply them in this case.

I saw a very similar example in SQL Server using cursors, but I couldn’t adapt it to Mysql.

  • I’m not sure, but I think with only one Procedure or Function.

  • I think you can do with a common select yes.

1 answer

3


With the following query you will get what you need:

Select concat("SELECT * FROM ", table_name, " LIMIT 10;") 
FROM information_schema.tables 
WHERE table_schema = database()

It will return all the querys you wish to run, listing all the tables and contacting the SELECT * FROM <tabela>

To execute these querys, it is possible to create a procedure that will execute the querys returned by the previous query. Maybe there is a better way to structure this procedure (parameterize the limits and the cursor query for example), but the way it is works :)

DELIMITER $$

CREATE PROCEDURE `tablesPreview`()
BEGIN
    DECLARE done BOOL DEFAULT FALSE;
    DECLARE queryToExecute varchar(255);
    DECLARE tablesCursor
        CURSOR FOR
        SELECT concat("SELECT * FROM ", table_name, " LIMIT 10;") 
        FROM information_schema.tables 
        WHERE table_schema = database();

    DECLARE
        CONTINUE HANDLER FOR
        SQLSTATE '02000'
            SET done = TRUE;

    OPEN tablesCursor;

    myLoop: LOOP
        FETCH tablesCursor INTO queryToExecute;

        IF done THEN
            CLOSE tablesCursor;
            LEAVE myLoop;
        END IF;

    set @query = (SELECT queryToExecute);

    PREPARE tablePreviewQuery FROM @query;
    EXECUTE tablePreviewQuery;

    END LOOP;

END
  • Thanks @gmsantos! This query is the first part of what I want. What I want is the result of the execution of all of them. There is some compact way to bring not the list of Selects, but the results of all of them?

  • 1

    I did a lot of research on this and I couldn’t find anything so simple. As the columns of the tables can be different it makes it difficult UNION ALL to execute the query result.

  • Are you using any backend language? With php I can easily display the result of these querys.

  • In my case there is no need for a UNION ALL, because I don’t need it to be in one recordset. The result I hope is exactly what I get when running the list of SELECT, and have it saved in a Procedure, p ex.. sp_amostra_bd. The result is for display in Mysql Workbench itself, so I can’t use another language to help.

  • @Odysseus I managed to implement in a logic process for execution.

  • Perfect, exactly what I was looking for! I didn’t think it would result in a Procedure so extensive, if I knew it would be so laborious!... But now it’s quite simple, just one CALL tablesPreview; and that’s it. Thank you so much! Ah, I’m new here at Stackoverflow. There is something else I can do, besides setting your answer as useful and accepted (arrow up and mark "V" green)?

  • @Odysseus is basically what you can do. What you can do more is offer a gratification, only you need to have more reputation.

  • Pity, I’m still playing, with 28 points... Thank you so much for the time spent in helping me!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.