Here are some neat database commands I recently came across while I was trying to get statistics and status information on some mysql and postgres databases.

Postgres

This will give you a list of all of the databases in the server.*select datname from pg_database;

* If you don’t know how to connect to postgres from the command line, don’t fear (well fear a little). What I had to do was su to the user that is a DB user, and then run /usr/local/pgsql/bin/psql This launched a command line client (without needing a password), that let me query the server. I can’t provide a better understanding then that, as this is the first time I’ve ever interacted with a postgres server.

This tells you how big a particular database is.SELECT pg_size_pretty(pg_database_size('xythosdocs'));
The output is like this: pg_size_pretty
----------------
2834 MB
(1 row)

So, you have a nice new database table, and you forgot (or couldn’t) add comments to the columns when you created it. I recently ran into this problem, and my usual internet searches didn’t turn up much. One would expect a statement similar to this:ALTER COLUMN `columnname` ADD COMMENT 'a comment'
However, that statement doesn’t exist.

If you have gotten to my post, you probably realize that there is no easy way to add comments (unless you have a nice application that does it for you). The real statement is something along the lines of this:ALTER TABLE `courses` CHANGE `courseid` `courseid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'a new comment'

Which means you need to know the table definition, and restate it every time you want to change the comment. If you are doing this in bulk, that is a pain in the butt.

So, I wrote this little script that will take a create table definition, and turn it into Alter Table commands, that will add comments to each of your columns.

All you need to do is run ‘SHOW CREATE TABLE `tablename`’ on your database table, and insert that definition into my script here, and it will spit out a series of Alter Table calls with a space for a comment appended to the end.

The script is very basic at this point, and expects the formatting as shown in the example. The GUI I use, DbVisualizer, as well as the mysql command line client spit this out. So hopefully yours will to.

If you find this useful, or run into any problems with the script, please let me know.