I was recently getting up to speed on a few new applications and needed to understand the database schema. I found myself repeatedly using the MySQL show tables and desc tables command line queries. After doing this a few times I got a little frustrated so I wrote this handy little shell script that will generate HTML output of an entire database schema to db_schema.html on your desktop.
Example Output - db_schema.html
This script uses the mysql command line “SHOW TABLES” and “DESC <TABLE>” commands.
For those not familiar with the “SHOW tables” command here’s what the output looks like-
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
For those not familiar with the “DESC Table” command here’s what the output looks like.
mysql> desc user;
+-----------------------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+-----------------------------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum('N','Y') | NO | | N | |
This script assumes that:
- the mysql username is root
- there is no password
- the db name is dbname
if [ -f ~/Desktop/db_schema.html ]; then
rm ~/Desktop/db_schema.html
fi
for i in `mysql -B -N -uroot dbname -e ’show tables;’`; do
echo "<a href=’#$i’>$i</a><br>" >> /tmp/$$.nav.html
echo "<a name=’$i’></a>" >> /tmp/$$.tables.html
echo "<h2>$i</h2>" >> /tmp/$$.tables.html
mysql –html –column-names -uroot dbname -e "desc $i;" >> /tmp/$$.tables.html
echo -n >> /tmp/$$.tables.html
done;
cat /tmp/$$.nav.html /tmp/$$.tables.html > ~/Desktop/db_schema.html
This entry was posted in Development, Linux Admin and tagged bash, desc table, describe table, mysql, mysql command line, shell script, show tables. Bookmark the
permalink. Comments are closed, but you can leave a trackback:
Trackback URL.
MySQL Show or Desc(ribe) All Tables at Once
I was recently getting up to speed on a few new applications and needed to understand the database schema. I found myself repeatedly using the MySQL show tables and desc tables command line queries. After doing this a few times I got a little frustrated so I wrote this handy little shell script that will generate HTML output of an entire database schema to db_schema.html on your desktop.
Example Output - db_schema.html
This script uses the mysql command line “SHOW TABLES” and “DESC <TABLE>” commands.
For those not familiar with the “SHOW tables” command here’s what the output looks like-
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
For those not familiar with the “DESC Table” command here’s what the output looks like.
mysql> desc user;
+-----------------------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+-----------------------------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum('N','Y') | NO | | N | |
This script assumes that:
if [ -f ~/Desktop/db_schema.html ]; then
rm ~/Desktop/db_schema.html
fi
for i in `mysql -B -N -uroot dbname -e ’show tables;’`; do
echo "<a href=’#$i’>$i</a><br>" >> /tmp/$$.nav.html
echo "<a name=’$i’></a>" >> /tmp/$$.tables.html
echo "<h2>$i</h2>" >> /tmp/$$.tables.html
mysql –html –column-names -uroot dbname -e "desc $i;" >> /tmp/$$.tables.html
echo -n >> /tmp/$$.tables.html
done;
cat /tmp/$$.nav.html /tmp/$$.tables.html > ~/Desktop/db_schema.html