Home > Software engineering >  Preview all tables in MySQL 5.5
Preview all tables in MySQL 5.5

Time:07-26

I have a database which is poorly documented and I am attempting to understand what is in each of the 50 tables located in the database. I want to just see the first few rows to get an idea of this.

For a single table I could type SELECT * FROM TABLENAME1 LIMIT 10 and it works, but I am loathe to type this 50 times. Is there a way from the command line I can get output for all tables? Could I store this output somewhere?

I found this which uses the sp_MSforeachtable command to accomplish this (sp_MSforeachtable @command1="select count(*) from ?";) but that gave me the following error:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sp_MSforeachtable @command1="select count(*) from ?"' at line 1

(I'm using MySQL 5.5 on an Ubuntu 18.04 server).

CodePudding user response:

You can also use a little sh script like this:

mysql -uroot -pYOURPW -Dbernd -N -e'SHOW TABLES' |
while read t
do
 echo;
 echo $t;
 echo;
 mysql -uroot pYOURPW -Dbernd  -e"SELECT * FROM $t LIMIT 10";
done
  • Related